如果调试时存在 Docker HealthChecks,Visual Studio 无法启动容器化 ASP.NET Core

问题描述 投票:0回答:1

我正在构建 2 个使用 Docker 容器化的 ASP.NET Core Web API 服务,

WebApplication1
WebApplication2
WebApplication2
取决于
WebApplication1
,这就是为什么在
WebApplication1
中,我为 docker compose 将为
WebApplication1
实现的 HealthCheck 公开了一个端点:

using System.Net;    

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();

var app = builder.Build();
app.UseRouting();

var containerInfo = app.MapGroup("/health1");
containerInfo.MapGet("/", (HttpContext context) =>
{
    return HttpStatusCode.OK;
});

app.UseAuthorization();
app.MapControllers();
app.Run();

Dockerfile

 # This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:9.0-preview AS base
USER app
WORKDIR /app
EXPOSE 8080

USER root
RUN apt-get update && apt-get install -y curl
USER app

# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:9.0-preview AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["WebApplication1.csproj", "."]
RUN dotnet restore "./WebApplication1.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./WebApplication1.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./WebApplication1.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app

COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

WebApplication2 的启动代码和

Dockerfile
完全相同。这是 Docker Compose 文件:

  services:
  webapplication1:
    image: ${DOCKER_REGISTRY-}webapplication1
    build:
      context: WebApplication1
      dockerfile: Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_HTTP_PORTS=8080      
    ports:
      - 53661:8080
      - 53662:8081
    healthcheck:         
        test: curl http://localhost:8080/health1 || exit 1              
        start_period: 10s
        start_interval: 10s
        interval: 5s
        timeout: 20s
        retries: 5 
  webapplication2:
    image: ${DOCKER_REGISTRY-}webapplication2
    build:
      context: WebApplication2
      dockerfile: Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_HTTP_PORTS=8080
    depends_on:
        webapplication1:    
          condition: service_healthy 
    ports:
      - 63661:8080
      - 63662:8081

注意这一行

test: curl http://localhost:8080/health1 || exit 1
是应该将
WebApplication1
标记为健康的代码,以便
WebApplication2
可以启动。当我调试 docker compose 编排时,它失败了:

3>#12 [webapplication2] resolving provenance for metadata file
3>#12 DONE 0.0s
3> Container WebApplication1  Creating
3> Container WebApplication1  Created
3> Container WebApplication2  Creating
3> Container WebApplication2  Created
3> Container WebApplication1  Starting
3> Container WebApplication1  Started
3> Container WebApplication1  Waiting
3> Container WebApplication1  Error
3>dependency failed to start: container WebApplication1 is unhealthy
3>C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Sdks\Microsoft.Docker.Sdk\build\Microsoft.VisualStudio.Docker.Compose.targets(430,5): error DT1001: dependency failed to start: container WebApplication1 is unhealthy
3>C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Sdks\Microsoft.Docker.Sdk\build\Microsoft.VisualStudio.Docker.Compose.targets(430,5): error DT1001: If the error persists, try restarting Docker Desktop.
3>Done building project "docker-compose.dcproj" -- FAILED.
========== Build: 2 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 07:48 and took 36.168 seconds ==========

enter image description here

跑步

docker inspect WebApplication1 --format  '{{range .State.Health.Log}}{{.End}} | Exit Code: {{.ExitCode}} | {{.Output}}{{end}}'

导致此错误:

curl:(7) 0 毫秒后无法连接到本地主机端口 8080:无法连接到服务器

我在

Program.cs
中的
WebApplication1
中放置了一个断点,在此配置下它不会启动,但如果我注释撰写文件中的运行状况检查代码,它会按预期工作。奇怪的是,如果我执行
docker compose up --build
,它就会正常工作

enter image description here

我的第一个想法是应该在调试模式下使用 Visual Studio Docker 工具配置,其他 StackOverFlow 帖子中建议添加以下内容:

<PropertyGroup>
    <ContainerDevelopmentMode>Regular</ContainerDevelopmentMode>
</PropertyGroup>

但也不起作用。您知道健康检查出了什么问题吗?

我附上了一个工作的

.zip
文件和解决方案,以便可以轻松测试它

谢谢!

docker visual-studio asp.net-core docker-compose asp.net-core-webapi
1个回答
0
投票

为了任何在搜索中找到此内容的人的利益。 Visual Studio Compose 调试体验当前不支持依赖项。正在跟踪修复此问题:如果调试时存在 Docker HealthChecks,Visual Studio 无法启动容器化 ASP.NET Core

© www.soinside.com 2019 - 2024. All rights reserved.