Search code examples
asp.netwindowsdockerasp.net-core

ASP.NET Core container runs on custom port 8080 but ASP.NET container runs on fixed port 80 in Docker Windows


I made two containers based on following images:

ASP.NET Core
mcr.microsoft.com/dotnet/aspnet:8.0.0-windowsservercore-ltsc2019

ASP.NET
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2016

I run both containers as follows:

docker run -d -p 8080:8080 aspcore
docker run -d -p 9090:9090 asp

ASP.NET Core container runs on port 8080 and I can open it in browser http://localhost:8080 but ASP.NET container doesn't work on http://localhost:9090

docker ps -a shows that ASP.NET core is running on 0.0.0.0:8080->8080/tcp while ASP.NET is running on 80/tcp, 0.0.0.0:9090->9090/tcp

Which means ASP.NET is only accessible via some IP on port 80 for e.g. http://172.20.235.45/. This IP is of Hyper-V virtual adapter.

Even if I build that image with EXPOSE 9090, its behavior is still the same.

In short, ASP.NET Core container works both ways:
http://localhost:8080
http://172.20.235.46:8080

But ASP.NET container is only accessible as:
http://172.20.235.45/

Why is it behaving like this? Is it because of how its base image is built by Microsoft?


Solution

  • From EXPOSE docs:

    The EXPOSE instruction doesn't actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published.

    So specifying EXPOSE 9090 will not change the port the ASP.NET is listening.

    You are free to map "internal" port to any external one so the easiest option would be to just map (publish) the default 80 of the container to the 9090:

    docker run -d -p 9090:80 asp
    

    If you want to change the "internal" port you will need to handle the IIS config which is used as web server in the container.

    Also note that 8080 is the default port for ASP.NET Core images since 8th version.