Home > Software design >  There is no ports exposed in the image for built docker container .NET Core 6.0 (using linux vm)
There is no ports exposed in the image for built docker container .NET Core 6.0 (using linux vm)

Time:12-01

I am trying to run the .NET Core 6.0 docker container for linux but for some reason there are no exposed ports. I have a solution with multiple projects and since I am using Windows 10, I had to publish the .sln and convert it to Linux. I am using this command:

docker build . -t backendapi

and my dockerfile (that exists in the .sln directory):

FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine as build
WORKDIR /app
COPY . .
EXPOSE 7101
RUN dotnet restore
RUN dotnet publish -o /app/published-app

FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine as runtime
WORKDIR /app
COPY --from=build /app/published-app /app
ENTRYPOINT [ "dotnet", "/app/CateringApp.dll" ]
CMD ["./CateringApp", "--urls", "http://0.0.0.0:7101"]

the directory of the published file is

./CateringApp\bin\Debug\net6.0\linux-x64\publish

So the image builds and the container is working (as far as I can understand) but there is nothing on localhost:7101, so I checked the ports in the docker image and I saw this under Ports section: [![enter image description here][1]][1]

and these are the stats for the container [![enter image description here][2]][2]

I am guessing that I need to change my dockerfile but I have no clue what else I can edit. I have tried also to build the image in the publish's folder directory, but that didn't work out at all. [1]: https://i.stack.imgur.com/DBP2h.png [2]: https://i.stack.imgur.com/bH9s8.png

CodePudding user response:

First off, the command reference says to put the path last on docker build. Yours may work, but just to be safe, I'd do

docker build -t backendapi .

Secondly, statements in all but the final stage in a multi-stage Dockerfile aren't included in the final image. You may think of each stage as a separate Dockerfile. So your EXPOSE statement should be in the final stage rather than in the first stage.

Thirdly, EXPOSE doesn't actually do anything. It acts as documentation about which port(s) the image uses. And your case it's most likely not correct, as we'll get to later. So I'd delete the EXPOSE statement.

Fourthly, when you have both an ENTRYPOINT and a CMD statement, they're combined into a single command that is run when the container starts. In your case, the command ends up as dotnet /app/CateringApp.dll ./CateringApp --urls http://0.0.0.0:7101. That might be what you want, but most ASP.NET apps just run the DLL with only what you have in the entrypoint.

Fifthly, the ASP.NET images from Microsoft set the environment variable ASPNETCORE_URLS to http:// :80 making the app listen on port 80 unless you change it.

If you change your Dockerfile to

FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine as build
WORKDIR /app
COPY . .
RUN dotnet restore
RUN dotnet publish -o /app/published-app

FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine as runtime
WORKDIR /app
COPY --from=build /app/published-app /app
ENTRYPOINT [ "dotnet", "/app/CateringApp.dll" ]

You should be able to build and run it with

docker build -t backendapi .
docker run -d -p 8080:80 backendapi

you should then be able to reach your application on http://localhost:8080/

Since your image name has 'API' in it, I'm guessing that you might have based your app on the webapi template. Please note that if you've done that, then Swagger will not be available. By default, Swagger is only available when the application runs in the Development environment and a Docker container is, by default, not Development.

  • Related