Swagger在Docker容器中出现 – ERR_EMPTY_RESPONSE

huangapple go评论99阅读模式
英文:

Swagger in Docker Container getting - ERR_EMPTY_RESPONSE

问题

我在尝试访问运行在 Docker 容器中的 ASP.NET Core API 中的 Swagger 时遇到了 ERR_EMPTY_RESPONSE 错误。API 和 Docker 配置似乎是正确的,但我无法访问 Swagger。

当我尝试通过访问 http://localhost:5101/swagger 来访问 Swagger UI 时,在我的浏览器中收到 ERR_EMPTY_RESPONSE 错误。我已经验证了 API 和 Docker 容器正在运行,并且端口映射似乎是正确的。API 本身似乎正常运行,因为我可以直接访问其他端点。

Docker 配置 (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 应用程序 (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
    }
  }
}

ASP.NET Core API 的 Dockerfile:

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"]

更新:在端口部分进行了更改:

  • "8080:8080"
  • "9101:81"

像这样更改后,工作正常。但我想知道原因。

英文:

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:

  • "8080:8080"
  • "9101:81"

changed like this.
but I would like to know reason.

答案1

得分: 0

找到原因:

新行为
从.NET 8开始,如果在容器中映射到端口80而没有明确设置容器中使用的ASP.NET Core端口,任何尝试连接到该映射端口的操作都将失败。

例如,如果运行以下命令,您将无法使用端口9999在本地连接到应用程序。

docker run --rm -it -p 9999:80 <my-app>

相反,将命令更改为在容器内使用端口8080:

docker run --rm -it -p 9999:8080 <my-app>
英文:

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 &lt;my-app&gt;

Instead, change the command to use port 8080 within the container:

docker run --rm -it -p 9999:8080 &lt;my-app&gt;

huangapple
  • 本文由 发表于 2023年8月10日 16:07:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定