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

asp.netDocker.Net CoreKestrel Http-Server

asp.net Problem Overview


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.

asp.net Solutions


Solution 1 - asp.net

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.

Solution 2 - asp.net

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

Solution 3 - asp.net

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.

Solution 4 - asp.net

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} 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhillip Scott GivensView Question on Stackoverflow
Solution 1 - asp.netomajidView Answer on Stackoverflow
Solution 2 - asp.netMustafa Salih ASLIMView Answer on Stackoverflow
Solution 3 - asp.netRromanView Answer on Stackoverflow
Solution 4 - asp.netHitesh LView Answer on Stackoverflow