asp.netdocker.net-corekestrel-http-server

Why does aspnet core start on port 80 from within Docker?


TL;DR: Why does an aspnet core app run on port 80 from within a Docker image, but 5000 outside a docker image.

Elaborate

I went through the aspnet core / docker tutorial found here: https://learn.microsoft.com/en-us/dotnet/core/docker/building-net-docker-images

Half way through the page, I start the application with the following as prescribed:

dotnet run

Among other things, this prints this:

Now Listening on: http://localhost:5000

Great. That is what I expected. The next thing in the tutorial is to start the exact same application from within a Docker image.

docker build -t aspnetapp .
docker run -it --rm -p 5000:80 --name aspnetcore_sample aspnetapp

This results in

Now listening on: http://[::]:80

Wait. Wat? Why is the aspnet core app running on port 80? It was running on port 5000 when I ran it directly from the machine. There were no configuration file changes.

I suspect that it has something to do with the base docker images, but am not yet skilled enough in docker to track this down.


Solution

  • The microsoft/aspnetcore-build container builds on top of the microsoft/aspnetcore container. The dockerhub page for that says:

    A note on ports

    This image sets the ASPNETCORE_URLS environment variable to http://+:80 which means that if you have not explicity set a URL in your application, via app.UseUrl in your Program.cs for example, then your application will be listening on port 80 inside the container.

    So this is the container actively setting the port to 80. You can override it, if you want, by doing this in your Dockerfile:

    ENV ASPNETCORE_URLS=http://+:5000

    Also, it is worth noting that because of the docker command you are using, you will still be able to access the application at http://localhost:5000 whether you are running the application directly or in a container.