Reputation: 39182
I have a plain .NET 5 project with the default weather forecast. Running locally, it behaves as expected and the call to http://localhost:5000/WeatherForecast gives me the data. However, trying to reach the same address when running in Docker container fails. The only difference I can see between the logs is that they listen to different ports. The setup follows roughly the docs on MSDN.
Locally it is 5000/5001.
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
Containerized it is 80.
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:80
That surprises me because I published those two ports as shown in the docs. In the Docker desktop client, I can see port: 5000 mentioned (but not 5001). I tried to access Swagger on port 80 with little success (I removed env.IsDevelopment()
to make sure that both dev and prod work the same way). I tried both create/start and run. Same result. According to this blog, publishing a port, exposes it too, so no additional expose
in the Dockerfile is needed (although I've tested that too).
docker create --publish 5000:5000 --publish 5001:5001 --name dotnet5 dotnet-core-5:webapi
What can I be missing?
The whole Dockerfile looks like this.
from mcr.microsoft.com/dotnet/aspnet:5.0
copy bin/Release/net5.0/publish /app
workdir /app
entrypoint ["dotnet", "WebApi.dll"]
Upvotes: 0
Views: 920
Reputation: 25344
It listens on port 80 in the container because Microsoft set the ASPNETCORE_URLS environment variable in their aspnet Docker images to http://+:80
which overrides your configuration.
To get it to listen on something else, you have to set the ASPNETCORE_URLS variable yourself in your Dockerfile.
To get it to listen on port 5000 and 5001 like you want, you need to add
ENV ASPNETCORE_URLS=http://+:5000;https://+:5001
to your Dockerfile
They set it in the runtime-deps
image, which is the base for the runtime
image, which is the base for the aspnet
image. You can see it here: https://github.com/dotnet/dotnet-docker/blob/d8dc00685a45b7f534e9f68ded50667023ded151/src/runtime-deps/3.1/bullseye-slim/amd64/Dockerfile
Upvotes: 3