docker compose 与 Visual Studio 的调试不起作用

Tij*_*out 9 debugging nginx visual-studio .net-core docker-compose

我在 nginx 反向代理后面有一个 .net core 2.1 api,我在 Visual Studio 中使用 docker compose 设置了该代理。运行时,API 是可访问的(我有一个可以调用来验证的健康检查控制器),但我无法调试。看起来我的解决方案在构建后没有运行。但我的容器已启动并且可以访问。我正在使用 Visual Studio 2019。

这是我的文件夹结构:

  • ...
  • RestApi(项目文件夹文件夹)
  • Dockerfile
  • docker-compose.yml
  • 反向代理(文件夹)
    • Dockerfile
    • nginx.conf
  • ...

这些是文件(文件夹结构中从上到下):

Dockerfile(用于 API):

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
WORKDIR /app
#EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
WORKDIR /src
COPY ["RestApi/RestApi.csproj", "RestApi/"]
COPY ["Services/Services.csproj", "Services/"]
COPY ["DataServices/DataServices.csproj", "DataServices/"]
COPY ["Entities/Entities.csproj", "Entities/"]
RUN dotnet restore "RestApi/RestApi.csproj"
COPY . .
WORKDIR "/src/RestApi"
RUN dotnet build "RestApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "RestApi.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

ENV ASPNETCORE_URLS http://+:5000
EXPOSE 5000

ENTRYPOINT ["dotnet", "RestApi.dll"]
Run Code Online (Sandbox Code Playgroud)

docker-compose.yml:

version: '2.1'
services:
  restapi:
    build:
      context: ./
      dockerfile: Dockerfile
    expose:
      - "5000"
    #restart: always
  reverseproxy:
    build:
      context: ./ReverseProxy
      dockerfile: Dockerfile
    ports:
      - "80:80"
    #restart: always
    links :
      - restapi
Run Code Online (Sandbox Code Playgroud)

Dockerfile(反向代理):

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
Run Code Online (Sandbox Code Playgroud)

nginx.conf:

worker_processes 4;

events { worker_connections 1024; }

http {
    sendfile on;

    upstream app_servers {
        server RestApi:5000;
        #server 172.17.0.1:5000;
    }

    server {
        listen 80;

        location / {
            proxy_pass         http://app_servers;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

通过 Visual Studio 运行 docker-compose 时,docker 容器已正确创建,但我无法调试并且没有启动浏览器屏幕。我在构建或运行时没有收到任何错误。如果您需要额外信息,请询问。

Tij*_*out 16

我发现出了什么问题。答案是此处发布的问题的解决方案。 https://developercommunity.visualstudio.com/content/problem/552563/debugger-silently-fails-to-attach-to-docker-compos.html

基本上,当 dockerfile 不在相应的 csproj 文件(项目文件)旁边(相邻)时,Visual Studio 将不会附加调试器。这是设计使然,因为可能存在您想要启动但不想调试的容器(反向代理、mysql 数据库等)。因此,当我将 api 的 Dockerfile 移至restapi 文件夹(与 csproj 文件相同的文件夹)并调整 docker-compose.yml 以在该文件夹内查找 dockerfile 时,调试工作在 Visual Studio 中进行。


Jan*_* K. 9

Edit2: Visual Studio(16.11.6+)补丁已发布。你docker-compose enable-v2现在可以。 这个答案现在已经过时了。


对于随机的 google 用户,有一个新问题(2021 年 10 月/11 月),其中docker-compose破坏了 Visual Studio 2019(但不是 VS2022 预览版)调试,其症状与此问题几乎相同。

问题是,将返回到anddocker-compose config的相对路径(它应该返回绝对路径 in和相对路径 in )Build.ContextBuild.DockerfileBuild.ContextBuild.Dockerfile

现在的解决方案是docker-compose disable-v2清理并重建您的解决方案,直到发布新的 docker-compose 或 Visual Studio 补丁。

有关详细信息,请参阅https://github.com/microsoft/DockerTools/issues/311https://github.com/docker/compose/issues/8760

编辑:

下一次 VS 更新将于 [2021]11 月 9 日进行。它将有修复。