ASP.NET Core 8 breaking change to the container port

Photo by Ma He on Unsplash

ASP.NET Core 8 breaking change to the container port

In containerized applications, including ASP.NET Core applications, ports below 1024 (0-1023) are generally considered privileged ports on Unix-based systems. These ports are often restricted for security reasons, requiring special permissions or elevated privileges to bind to them.

The designation of port 80 as a privileged port in this context stems from historical conventions and security practices in Unix-like operating systems. Traditionally, only the root user or users with special privileges (such as sudo access) can bind to ports below 1024. This practice was implemented as a security measure to prevent regular users and applications from inadvertently launching potentially risky services that listen on well-known ports used by essential system services (such as HTTP on port 80).

To align with best practices and enhance the user experience for non-root users without requiring access to privileged ports, the .NET 8 container image is now configured to use a default port of 8080 instead of 80.

Prior to ASP.NET Core 8, the container expected port 80 as the default, allowing access to the app running within it.

For instance:

docker run --rm -it -p 8000:80 myweatherapp

However, starting with .NET 8, if the app running within the container is mapped to port 80, as indicated in the command above, access to the application may not be possible, as the port available within the .NET 8 container is now 8080. To access the application, you need to map to the container port 8080 instead of 80.

For example:

docker run --rm -it -p 8000:8080 myweatherapp

To mitigate this breaking change, you can set the environment variable, ASPNETCORE_HTTP_PORTS to retain the use of port 80 despite the default port change in ASP.NET Core 8 within the container.

docker run --rm -it -p 8000:80 -e ASPNETCORE_HTTP_PORTS=80 myweatherapp

If your application was developed using the older WebHost.CreateDefaultBuilder() method, configure the ASPNETCORE_URLS environment variable (instead of ASPNETCORE_HTTP_PORTS).

For instance:

docker run --rm -it -p 8000:80 -e ASPNETCORE_URLS=http://*:80 myweatherapp