visual-studio - 在 Visual Studio 2019 中启动 Docker-compose 时无法启动调试适配器?

标签 visual-studio docker api docker-compose containers

我有一个 Visual Studio 解决方案,我想在其中运行各种 API 微服务。每个微服务都单独具有所需的 Dockerfile。我想使用 docker-compose 运行项目,所以我添加了容器编排支持。我还修改了 docker-compose.yml 和覆盖文件中的必要内容。然后我将 docker-compose 设置为启动项目。 (设置为启动项目)。但是,当我尝试使用 F5 启动时,调试器没有启动,我收到以下错误消息:
Error message window

One or more errors occured. Failed to launch debug adapter. Additional information may be available in the output window. The operation was canceled.


输出窗口:

The program '' has exited with code -1 (0xffffffff).


在 Docker 桌面上,我可以看到容器,但我的 API 项目的日志是空的。他们不会开始。
(我也启用了虚拟化和 HyperV。)
如何解决这个问题?

最佳答案

根据微软的说法,默认情况下调试器运行在 快速模式 加快 docker 容器的构建过程。在这种模式下,您的 Dockerfile 只是部分构建。
https://docs.microsoft.com/en-us/visualstudio/containers/container-build?view=vs-2019

In Fast mode, Visual Studio calls docker build with an argument that tells Docker to build only the base stage. Visual Studio handles the rest of the process without regard to the contents of the Dockerfile. So, when you modify your Dockerfile, such as to customize the container environment or install additional dependencies, you should put your modifications in the first stage. Any custom steps placed in the Dockerfile's build, publish, or final stages will not be executed.


这意味着如果您在用于运行时的图像之前在 Dockerfile 中引入任何其他图像,它将尝试将此图像用于您的容器。
解决方案
  • 如果您想在 Dockerfile 中保留所有步骤,同时仍然能够调试,请将您的运行时镜像放在文件的顶部,并仍然在需要的地方使用它。例如:
  • FROM mcr.microsoft.com/dotnet/aspnet:3.1 as base 
    ...
    FROM base
    WORKDIR /src
    ...
    
  • 或者,您可以在项目文件中添加一个 PropertyGroup 项,告诉它在 中构建 docker 文件。常规模式 .这会减慢您的构建速度。
  • <PropertyGroup>
       <ContainerDevelopmentMode>Regular</ContainerDevelopmentMode>
    </PropertyGroup>
    
    重现错误
    我通过使用 Docker 支持和容器编排创建一个新的空 .NET 核心项目来验证这一点。
    原始 dockerfile:
    FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
    WORKDIR /src
    COPY ["TestApplication/TestApplication.csproj", "TestApplication/"]
    RUN dotnet restore "TestApplication/TestApplication.csproj"
    COPY . .
    WORKDIR "/src/TestApplication"
    RUN dotnet build "TestApplication.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "TestApplication.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "TestApplication.dll"]
    
    运行良好。在这种情况下,在 Dockerfile 顶部添加不同的图像节点时,调试器会中断:
    FROM node:14-alpine as node-base
    
    FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
    WORKDIR /src
    COPY ["TestApplication/TestApplication.csproj", "TestApplication/"]
    RUN dotnet restore "TestApplication/TestApplication.csproj"
    COPY . .
    WORKDIR "/src/TestApplication"
    RUN dotnet build "TestApplication.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "TestApplication.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "TestApplication.dll"]
    

    关于visual-studio - 在 Visual Studio 2019 中启动 Docker-compose 时无法启动调试适配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66692612/

    相关文章:

    c# - MVC Razor View 没有 Intellisense

    docker - AWS Sagemaker |如何调试 docker 镜像 |我们传递的参数是什么

    apache - 为什么httpd容器在重新启动系统时停止工作?

    api - 如何使用Wikipedia api获取 "all pages with prefix"的结果?

    ios - 从 JSON 填充表

    c# - 如何使用 licenses.licx 文件

    visual-studio - 用于与 Google 文档配合使用的 Visual Studio 插件

    c++ - 如何从命令行在 visual studio 中构建 QT 项目

    networking - 在主机之间共享容器ip和端口

    ruby-on-rails - 渲染 json 中的哈希火箭与冒号 :