visual-studio - 调试不适用于带有 Visual Studio 的 docker compose
问题描述
我在 nginx 反向代理后面有一个 .net core 2.1 api,我在 Visual Studio 中使用 docker compose 设置了该代理。运行时,api 是可访问的(我有一个运行状况检查控制器,我可以调用它来验证),但我无法调试。看起来我的解决方案在构建后没有运行。但是我的容器已经启动并且可以访问。我正在使用视觉工作室 2019。
这是我的文件夹结构:
- ...
- RestApi(项目文件夹文件夹)
- Dockerfile
- 码头工人-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"]
码头工人-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 容器已正确创建,但我无法调试,也没有启动浏览器屏幕。在构建或运行时我没有收到任何错误。如果您需要额外的信息,请询问。
解决方案
我发现出了什么问题。答案是此处发布的问题的解决方案。 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 中工作。
推荐阅读
- java - 当数字在范围内时添加星号
- python - 如何使用 lambda 传递两个参数(参数之一是事件)?
- r - R 组合使用 dplyr 的 pull 创建的多个向量
- google-sheets - 根据团队名称导入“姓名”和“预期回报”列表
- c++ - 自身类中的类变量 C++
- html - 'PREV'按钮不能以多步形式工作 - jQuery
- alexa-skills-kit - 为什么我的话语没有触发意图?
- cordova - 电容器项目中 Cordova 插件的变量名称
- powershell - 如何转义 .ps1 文件中的换行符?
- python - 在多个文本文件中搜索一个字符串并打印该字符串所在的文件名