137

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.

5
  • 6
    In this line docker run -it --rm -p 5000:80 --name aspnetcore_sample aspnetapp you are mapping the internal port (within the docker container) of 5000 to an external port (outside of the container) of 80. Meaning, when you browse to the ip address given to your container, it forwards your request to port 5000 Feb 7, 2018 at 17:14
  • This is explained on the tutorial page that you link to, directly under the code supplied in this section Feb 7, 2018 at 17:15
  • 1
    @JamieTaylor you are right, but that's not what the message "now listening on ..." says. That message is talking about the internal port inside the container, which, as you correctly say, is mapped to the port 5000 outside the container.
    – omajid
    Feb 7, 2018 at 17:20
  • 1
    @JamieTaylor, omajid is right, That only tells me that I am mapping the internal port 80 to the external port 5000. This choice causes more confusion. The question is why is the internal port 80? They should have used a number other than 5000 for the external example to highlight that we are not talking about the same port as the previous example. Feb 7, 2018 at 17:26
  • Since .Net 8 the default port is 8080
    – andrew.fox
    Jan 24 at 21:02

5 Answers 5

190

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.

6
  • Thank you so much! I think it would have been a better example if they used -p 5001:80 to emphasis that we are not talking about the same port as before. Feb 7, 2018 at 17:29
  • @PhillipScottGivens maybe you already know by now, but port 5001 is used to run the app using SSL (https) by default. I understand your initial confusion though, but as the answer points out, ASPNETCORE_URLS dictates the port on which the app listens Feb 8, 2019 at 2:30
  • 3
    Hi, I'm using ENV ASPNETCORE_URLS http://+5000 in my Dockerfile but still see Now listening on: http://[::]:80 when start containers using docker-compose. I see: Step 4/7 : ENV ASPNETCORE_URLS http://+5000 Step 6/7 : EXPOSE 5000. But after I use 'docker-compose up' I see the same: Now listening on: http://[::]:80
    – Rroman
    Jul 31, 2019 at 20:09
  • 1
    @Rroman docker-compose down, remove all images related, and build again Oct 7, 2020 at 15:00
  • 1
    The dockerhub link in the answer ("The dockerhub page for that says") takes me to a page that does not mention ports nor ASPNETCORE_URLS. However, here is a page that contains the same info (but it looks unofficial): hub.docker.com/r/ostusa/aspnetcore Does anyone know if this behavior is still officially documented anywhere?
    – Andrew
    Apr 22, 2022 at 20:59
25

without dockerfile you can set any port out of the docker container. (.NET Core 3.1, .NET 5, .NET 6, .NET 7+) with docker args

docker run -it --rm -p 5000:80 -p 5001:443 -e ASPNETCORE_HTTPS_PORT=https://+:5001
           -e ASPNETCORE_URLS=http://+:5000 --name aspnetcore_sample aspnetapp

more details:

https://github.com/dotnet/dotnet-docker/blob/17c1eec582e84ba9cbea5641cd9cc13fe1a41c39/samples/run-aspnetcore-https-development.md?plain=1#L85

https://github.com/dotnet/dotnet-docker/blob/5926a01d44bd47b6202ba71e30f9faa08fad1aec/samples/run-in-sdk-container.md?plain=1#L109

12

If you are using .NET Core 2.2 or higher, then you should to use another image: mcr.microsoft.com/dotnet/core/aspnet:2.2. In that case specifying ENV ASPNETCORE_URLS=http://+:5000 does not help. You still can force app to listen to port 5000 by using UseUrls("http://*:5000") in Programs.cs file.

1
  • didn't know this, wasted so much time. Thanks Jan 25, 2021 at 21:25
8

Windows Networking Stack Limitation plays hard on Windows Docker Container.Reference Video

docker run -it --rm -p ${host_computer_port}:${container_port} --name ${container_name} ${image_name}

Example of the command:

docker run -it --rm -p 5000:8090 --name dockerwebapp9172020c dockerwebapp9172020

What is above command mean?

Your machine Port (5000) is mapped to container Port (8090). It does not mean that application running in container listening on PORT: 8090. See docker file below on how to map container port to application port.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 8090
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["DockerWebApp/DockerWebApp.csproj", "DockerWebApp/"]
RUN dotnet restore "DockerWebApp/DockerWebApp.csproj"
COPY . .
WORKDIR "/src/DockerWebApp"
RUN dotnet build "DockerWebApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "DockerWebApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV ASPNETCORE_URLS http://*:8090
ENTRYPOINT ["dotnet", "DockerWebApp.dll"]

Testing

Windows Networking Stack Limitation will not allow to run following command directly.

http://localhost:5000

Let's get on with first workaround. Workaround-1 Directly hit Container. (Below command in PowerShell or Command Prompt)

ps c:/>docker inspect f31e8add55af

Find IP Address of a container from "NETWORKS" node at very end and run command in browser.

http://{container IP}:8090

Workaroud-2 From Windows Host

Let's find Your Machine IP address first.

c:\>ipconfig

Once you find your Machine IP, run following command.

http://{Your Machine IP}:5000

since 5000 is mapped to container port(8090) and asp.net core application is also listening to 8090 port within container.

Reference:Windows Containers and Docker: 101

6

Some links in other answers are for older versions, or no longer exist. The below applies to v6.

All the mcr.microsoft.com/dotnet/aspnet images are here. Suppose you are using the alpine version.

The aspnet image is based on the runtime image, as shown here.

The runtime image is based on the runtime-deps image, as shown here.

The runtime-deps image is based on the amd64/alpine image, as shown here (an older version, but with the same structure). And it sets ENV ASPNETCORE_URLS=http://+:80, as shown here, which means the container is listening on port 80.

1
  • This is why! For future reference: in the container windows of VS2022, check the Environment tab for the current setting. Sep 7, 2022 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.