.NET 8.0 WebAPI/Swagger Docker Refused to connect

1k Views Asked by At

I migrated my application to .NET 8.0, ran it locally and it works perfectly.

Then I created an image, the container. As a result, the page that was working before now returns a "1.2.3.4 refused to connect."

Before, when I was in .NET 7.0, the API worked.

My basic DockerFile

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /app
 
COPY *.csproj ./
RUN dotnet restore
 
COPY . ./
RUN dotnet publish -c Release -o out
 
FROM mcr.microsoft.com/dotnet/aspnet:8.0

WORKDIR /app
RUN chmod -R 755 /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "myapi.dll"]

My api runs on port 8860

49de1a3ce47c   myapi   "dotnet myapi.dll"   13 minutes ago   Up 13 minutes          0.0.0.0:8860->80/tcp, :::8860->80/tcp

Logs:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://[::]:8080
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app
1

There are 1 best solutions below

0
On BEST ANSWER

.NET 8 ASP.NET Core Docker images have a breaking change - Default ASP.NET Core port changed from 80 to 8080:

The default ASP.NET Core port configured in .NET container images has been updated from port 80 to 8080. We also added the new ASPNETCORE_HTTP_PORTS environment variable as a simpler alternative to ASPNETCORE_URLS.

Previous behavior

Prior to .NET 8, you could run a container expecting port 80 to be the default port and be able to access the running app.
For example, running the following command allowed you to access the app locally at port 8000, which is mapped to port 80 in the container: docker run --rm -it -p 8000:80 <my-app>

So you need either to change port mapping 8860:8080 or change the port for the container (for example by passing -e ASPNETCORE_HTTP_PORTS=80 argument to docker run or adding ENV ASPNETCORE_HTTP_PORTS=80 after FROM mcr.microsoft.com/dotnet/aspnet:8.0).