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

Phillip Scott Givens picture Phillip Scott Givens · Feb 7, 2018 · Viewed 26.9k times · Source

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://docs.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.

Answer

omajid picture omajid · Feb 7, 2018

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.