Search code examples
dockerdockerfileasp.net-core-8

Docker: ASP.NET Core 8.0 app not accessible outside container


I am struggling to access my ASP.NET Core 8.0 web app (Swagger UI) from outside my container.

Without Docker, it is accessible at the following URL: http://localhost:90/swagger/index.html

I can start the app by executing an .exe file which results in the following output:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:90
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: C:\Users\Username\Documents\Git\WetterApi\WetterApi

The file structure of my already compiled app looks like following:

  • Microsoft.OpenApi.dll
  • Swashbuckle.AspNetCore.Swagger.dll
  • Swashbuckle.AspNetCore.SwaggerGen.dll
  • Swashbuckle.AspNetCore.SwaggerUI.dll
  • WetterApi.deps.json
  • WetterApi.dll
  • WetterApi.exe
  • WetterApi.pdb
  • WetterApi.runtimeconfig.json
  • appsettings.Development.json
  • appsettings.json dockerfile

Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
COPY . .
EXPOSE 90
ENV ASPNETCORE_ENVIRONMENT=Development
ENV DOTNET_RUNNING_IN_CONTAINER=true
ENTRYPOINT ["dotnet","/WetterApi.dll", "--server.urls","<http://+:90>"]

Building Image:

docker build -t wetter_api_test .

Starting:

docker run --rm -it -p 80:90  wetter_api_test

Port 80 as I want to have it available at http://localhost:80/swagger/index.html outside of the container

Output:

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Overriding address(es) 'http://*:8080'. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:90
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /

Curl on the Container Web App:

curl http://localhost:80/swagger/index.html    
curl : The underlying connection was closed: The connection was closed unexpectedly.

Does anyone see what exactly I am overlooking or what I do not understand here?

I am thankful for any hint or solution.

Added the following lines to the Dockerfile:

ENV DOTNET_URLS=http://+:90
ENV ASPNETCORE_URLS=http://+:90
ENV ASPNETCORE_HTTP_PORTS=90

But still, my app is not accessible from outside the container.


  1. Try - Following the Suggestion of Hans Kilian in the comments:

Removed Port Configs in my Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
COPY . .
ENV ASPNETCORE_ENVIRONMENT=Development
ENV DOTNET_RUNNING_IN_CONTAINER=true
ENTRYPOINT ["dotnet","/WetterApi.dll"]

Running the container with:

docker run --rm -it -p 80:8080  wetter_api_test

Result:

curl http://localhost:80/swagger/index.html
curl : The underlying connection was closed: The 
connection was closed unexpectedly.

When checking the Port Mapping via docker ps everything seems fine to me:

CONTAINER ID   IMAGE             COMMAND                  CREATED              STATUS              PORTS                  NAMES
2d179c430f50   wetter_api_test   "dotnet /WetterApi.d…"   About a minute ago   Up About a minute   0.0.0.0:80->8080/tcp   gracious_satoshi 

Solution

  • I suspect there's an issue with your application.

    To try to replicate your issue, I made the following Dockerfile based on yours. I've added a build step that creates a template webapi application and then uses your Dockerfile as the runtime step.

    FROM mcr.microsoft.com/dotnet/sdk:8.0 as builder
    WORKDIR /src
    RUN dotnet new webapi -n WetterApi -o . && \
        dotnet publish -o app
    
    FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
    COPY --from=builder /src/app/* .
    ENV ASPNETCORE_ENVIRONMENT=Development
    ENV DOTNET_RUNNING_IN_CONTAINER=true
    ENTRYPOINT ["dotnet","/WetterApi.dll"]
    

    When I build, run and test it with

    docker build -t test .
    docker run -d -p 80:8080 test
    curl http://localhost:80/swagger/index.html
    

    it works as expected.

    So I don't think there's anything wrong with your Dockerfile or the way you run it.