提问人:Amaury COLMANT 提问时间:11/14/2023 更新时间:11/14/2023 访问量:34
VS2022 调试使用 Docker Compose 卡在健康检查上
VS2022 Debug gets stuck on healthcheck using a Docker Compose
问:
我有一组dockerized的服务,我可以用docker-compose手动运行。在开始新服务之前,我想确保某些服务正常工作。为此,我遵循了Microsoft文档: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-7.0 和我在 docker-compose 中设置了健康检查和条件:
version: '3.4'
networks:
reporting-network:
driver: bridge
services:
reporting.api:
image: ${DOCKER_REGISTRY-}reportingapi
depends_on:
postgres_database:
condition: service_healthy
reporting.manager:
condition: service_healthy
networks:
- reporting-network
healthcheck:
test: curl -f http://localhost:80/health || exit 1
interval: 5s
timeout: 10s
retries: 120
postgres_database:
image: postgres:15
restart: always
networks:
- reporting-network
healthcheck:
test: /usr/bin/pg_isready
interval: 5s
timeout: 10s
retries: 120
reporting.manager:
image: ${DOCKER_REGISTRY-}reportingmanager
depends_on:
postgres_database:
condition: service_healthy
networks:
- reporting-network
restart: always
healthcheck:
test: curl -f http://localhost:5000/health || exit 1
interval: 5s
timeout: 10s
retries: 120
到目前为止一切顺利,手动...当我尝试使用 Visual Studio 2022 进行调试时出现问题。单击此链接,了解 Visual Studio 如何在 docker compose 中集成调试。
事实上,VS 正确地启动了 postgres 服务,它在启动“reporting.manager”服务之前检查其运行状况(运行状况),并检查其运行状况(运行状况:启动)。这就是它被卡住的地方。由于入口点被覆盖,服务尚未启动,因此它无法获得对状态检查的响应,并等待到最后一次尝试。
根据我的测试,只有在完成“up”执行后,它才会引用它在文件中生成的标签来启动服务。docker-compose.vs.debug.g.yml
如何在不阻止通过 Visual Studio 调试的情况下检查运行状况检查?感谢您的帮助。
我手动验证它是否有效。确实如此,并且有充分的理由,入口点定义不会被覆盖,并且服务在检查之前启动。
我试图在最后一步将健康检查定义移动到 dockerfile 中,但 docker-compose 条件似乎不喜欢它。“reporting.api”服务条件找不到“reporting.manager”服务运行状况检查:
FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine AS base
WORKDIR /app
RUN apk --no-cache add curl
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["Reporting.Manager/Reporting.Manager.csproj", "Reporting.Manager/"]
RUN dotnet restore "Reporting.Manager/Reporting.Manager.csproj"
COPY . .
WORKDIR "/src/Reporting.Manager"
RUN dotnet build "Reporting.Manager.csproj" -c Release -o /app/build --no-restore
FROM build AS publish
RUN dotnet publish "Reporting.Manager.csproj" -c Release -o /app/publish /p:UseAppHost=false --no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
HEALTHCHECK --interval=5s --timeout=10s --retries=3 CMD curl -f "http://localhost:5000/health" || exit 1
ENTRYPOINT ["dotnet", "Reporting.Manager.dll"]
答:
看起来这是 VS 用于启用(和加速)调试的优化的结果: https://developercommunity.visualstudio.com/t/docker-compose-hangs-in-visual-studio-when-using-d/1597872#T-N1599631
一个 wrokaround 是您可以将以下代码添加到项目文件中,该文件依赖于某些服务才能正常运行:
<PropertyGroup>
<ContainerDevelopmentMode>Regular</ContainerDevelopmentMode>
</PropertyGroup>
评论