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:
docker run -d -p 8081:80 dockerwebmvc .http://[::]:8080.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:
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.
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
EXPOSEinstruction 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With