我有一个 Visual Studio 解决方案,我想在其中运行各种 API 微服务。每个微服务都单独具有所需的 Dockerfile。我想使用 docker-compose 运行项目,所以我添加了容器编排支持。我还修改了 docker-compose.yml 和覆盖文件中的必要内容。然后我将 docker-compose 设置为启动项目。 (设置为启动项目)。但是,当我尝试使用 F5 启动时,调试器没有启动,我收到以下错误消息:
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 中引入任何其他图像,它将尝试将此图像用于您的容器。
解决方案
FROM mcr.microsoft.com/dotnet/aspnet:3.1 as base
...
FROM base
WORKDIR /src
...
<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/