Dockerized .net 8 app doesn't expose port correctly – Docker

by
Ali Hasan
asp.net-core-8 boot2docker

Quick Fix: Update Dockerfile to use ports 8080 and 8081 for accessing the ASP.NET Core app on Docker container.

The Problem:

A .NET 8 web API application is dockerized, but the port exposure seems to be incorrect. Although the application starts successfully within the container, accessing it through the exposed port (in this case, 8081) results in a 404 error. The application runs correctly when executed locally (without Docker), leading to the suspicion that the port mapping or container configuration might be causing the issue.

The Solutions:

Solution 1: Use ports 8080 and 8081 instead of 5000

Previously, .NET Dockerfiles used port 5000 by default, but in .NET 8.0, Microsoft changed this to 8080 for HTTP and 8081 for HTTPS.

  1. Update Dockerfile:

    • Change EXPOSE 5000 to EXPOSE 8080.
  2. Adjust launchSettings.json:

    • In the http and https profiles, update the applicationUrl to use port 8080.

    For example:

    "applicationUrl": "http://localhost:8080"
    
  3. Edit docker-compose.yml:

    • Change the port mapping to use 8081 instead of 8080 for the HTTP port.

    Example:

    ports:
      - "8081:8080"
    

Additional Information:

  • The netstat -anop command output shows that the application is listening on port 5000, which is incorrect for a .NET 8.0 app.
  • Make sure to restart the Docker container after making the changes.

By following these steps, you’ll be able to access your .NET 8.0 app correctly through port 8081.

Solution 2: Set ASPNETCORE_HTTP_PORTS Environment Variable

To correctly expose the application’s port, you can add the following line to your Dockerfile:

ENV ASPNETCORE_HTTP_PORTS 80

This sets the environment variable ASPNETCORE_HTTP_PORTS to 80, which will make your application listen on port 80 inside the container.

With this modification, your application should now be accessible by browsing to http://localhost:8081 in your browser. Make sure that your container is running before attempting to access the application.

Here’s the updated Dockerfile for reference:

FROM mcr.microsoft.com/dotnet/aspnet:8.0

WORKDIR /app

# Copy the built application from the build image
COPY ./bin/Release/net8.0/ /app

# Set the environment variable for ASP.NET Core
ENV ASPNETCORE_HTTP_PORTS 80
ENV ASPNETCORE_URLS=http://+:80

# Set the entry point for the application
ENTRYPOINT ["dotnet", "Finch.Agents.dll"]

Q&A

Why does the dotnet 8.0 app not expose port correctly in docker?

In .NET 8.0, Microsoft has changed default ports to 8080 and 8081 for ASP.NET Core apps.

How can I fix the port exposure issue?

You can set the ASPNETCORE_HTTP_PORTS environment variable to 80 in the Dockerfile.

Video Explanation:

The following video, titled "Dockerize .NET 6 in 10 Minutes - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

.NET 8 Authentication with Identity in a Web API with Bearer Tokens & Cookies ... How To Containerize a .NET App Without a Dockerfile. Julio ...