I'm encountering an ERR_EMPTY_RESPONSE error when trying to access Swagger within a Docker container running an ASP.NET Core API. The API and Docker setup appear to be correct, but I'm unable to access the Swagger.
When I try to access Swagger UI by visiting http://localhost:5101/swagger, I receive an ERR_EMPTY_RESPONSE error in my browser. I have verified that the API and Docker container are running, and the port mapping seems correct. The API itself appears to be functioning properly, as I can access other endpoints directly.
Docker Configuration (docker-compose.yml):
`version: '3.4'
services:
catalog.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "5101:80"
- "9101:81"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
sqldata:
environment:
- SA_PASSWORD=Pass@word
- ACCEPT_EULA=Y
ports:
- "5433:1433"
volumes:
- bond-sqldata:/var/opt/mssql
volumes:
bond-sqldata:
external: false`
ASP.NET Core Application (launchSettings.json):
`{
"profiles": {
"Catalog.API": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5222/"
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"publishAllPorts": true
}
}
}`
Dockerfile for ASP.NET Core API:
`FROM mcr.microsoft.com/dotnet/aspnet:8.0-preview AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:8.0-preview AS build
WORKDIR /src
COPY ["Services/Catalog/Catalog.API/Catalog.API.csproj", "Services/Catalog/Catalog.API/"]
RUN dotnet restore "Services/Catalog/Catalog.API/Catalog.API.csproj"
COPY . .
WORKDIR "/src/Services/Catalog/Catalog.API"
RUN dotnet build "Catalog.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Catalog.API.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Catalog.API.dll"]`
UPD: working after ports:
changed like this. but I would like to know reason.
Found reason:
New behavior Starting with .NET 8, if you map to port 80 in the container without explicitly setting the ASP.NET Core port used in the container, any attempt to connect to that mapped port will fail.
For example, if you run the following command, you'd be unable to connect to the application locally using port 9999.
docker run --rm -it -p 9999:80 <my-app>
Instead, change the command to use port 8080 within the container:
docker run --rm -it -p 9999:8080 <my-app>