Search code examples
dockerasp.net-core-mvc

ASP.NET Core Docker Container Not Accessible via Exposed Port on Host Machine


I'm trying to run an ASP.NET Core MVC web application inside a Docker container. The container seems to start correctly, and the logs show that the application is listening on the expected port http://[::]:8080. However, when I try to access the application from my host machine using http://localhost:8081, I get an ERR_EMPTY_RESPONSE error in the browser.

This is the Dockerfile:

# Use the official .NET Core runtime as the base image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80

# Use the .NET Core SDK for building the project
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["DockerWebMVC.csproj", "./"]
RUN dotnet restore "./DockerWebMVC.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "DockerWebMVC.csproj" -c Release -o /app/build

# Publish the project to get the deployment-ready binaries
FROM build AS publish
RUN dotnet publish "DockerWebMVC.csproj" -c Release -o /app/publish

# Final stage: Create a runtime container image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerWebMVC.dll"]

Here's what I've done so far:

  • Built the Docker image using a Dockerfile that defines the ASP.NET Core project.
  • Ran the container with the following command: docker run -d -p 8081:80 dockerwebmvc .
  • Checked the logs inside the container, which indicate that the application is running on http://[::]:8080.
  • Executed curl http://localhost:8080 inside the container, and it correctly returns the HTML of the application’s homepage.

However, when I attempt to access the application from my host machine via http://localhost:8081, it doesn't work.

Additional details:

  • The Docker network is using the default bridge mode.
  • I checked for potential firewall or port issues, but everything seems to be configured correctly.
  • The application runs fine locally outside of Docker.

Could someone help me identify why I can't access the application from my host machine despite the container running correctly? Any advice on debugging steps or potential issues with the Docker configuration would be appreciated.


Solution

  • Specifically, I used the docker run -d -p 8081:80 dockerwebmvc command to start the container and expected to be able to access the application via http://localhost:8081 on my host machine.

    And

    The logs within the container indicate that the application is running and listening on http://[::]:8080

    Application is running on 8080 port inside the container but you are mapping the 80 one, you need to map the 8080 instead:

    docker run -d -p 8081:8080 .....
    

    Since .NET 8 applications are using 8080 port by default - see the Default ASP.NET Core port changed from 80 to 8080:

    And EXPOSE is used just for documentation purposes and does not actually do something:

    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.