首页 > 解决方案 > 调试不适用于带有 Visual Studio 的 docker compose

问题描述

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

这是我的文件夹结构:

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

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

码头工人-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

Dockerfile(反向代理):

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

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;
        }
    }
}

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

标签: visual-studiodebuggingnginx.net-coredocker-compose

解决方案


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

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


推荐阅读