使用 Visual Studio 2019 进行调试时,Dockerfile 似乎无法运行。

7
最近几周我一直在使用 Visual Studio 2019 中 C# .NET Core 构建项目。我启用了 Docker 支持,并使用 docker-compose 来启动两个容器(启动了 IdentityServer4 和 WebAPI)。我为这两个项目创建了 Dockerfile,并创建了一个 docker-compose 文件来启动服务栈。
我遇到的问题是,当我在 Visual Studio 调试模式下运行 docker-compose 时,似乎并没有运行我的 Dockerfile。在 Dockerfile 的最后一步中,我复制了一些文件并执行了一个命令。但是这些都没有运行。然而当我在命令行中使用 docker build 时,它确实执行了这些 Dockerfile 命令。
附上我的 2 个 Dockerfile 和 docker-compose。
Web API Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Api/MyApp.Api.Core/MyApp.Api.Core.csproj", "Api/MyApp.Api.Core/"]
COPY ["Api/MyApp.Api.Base/MyApp.Api.Base.csproj", "Api/MyApp.Api.Base/"]
COPY ["Base/MyApp.Base.Contracts/MyApp.Base.Contracts.csproj", "Base/MyApp.Base.Contracts/"]
COPY ["Base/MyApp.Base.Model/MyApp.Base.Model.csproj", "Base/MyApp.Base.Model/"]
COPY ["Data/MyApp.Data.EntityFramework/MyApp.Data.EntityFramework.csproj", "Data/MyApp.Data.EntityFramework/"]
COPY ["ContactpersoonService.cs/MyApp.Services.csproj", "ContactpersoonService.cs/"]
COPY ["Apps/MyApp.Apps.Settings/MyApp.Apps.Settings.csproj", "Apps/MyApp.Apps.Settings/"]
RUN dotnet restore "Api/MyApp.Api.Core/MyApp.Api.Core.csproj"
COPY . .
WORKDIR "/src/Api/MyApp.Api.Core"
RUN dotnet build "MyApp.Api.Core.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "MyApp.Api.Core.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
COPY Api/MyApp.Api.Core/Security/Certificates /app/Security/Certificates
RUN mkdir -p /usr/local/share/ca-certificates/identityserver
RUN chmod -R 777 /usr/local/share/ca-certificates/identityserver
RUN cp /app/Security/Certificates/* /usr/local/share/ca-certificates/identityserver
RUN update-ca-certificates
ENTRYPOINT ["dotnet", "MyApp.Api.Core.dll"]

IdentityServer Dockerfile:

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

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Api/MyApp.Api.IdentityServer/MyApp.Api.IdentityServer.csproj", "Api/MyApp.Api.IdentityServer/"]
COPY ["Api/MyApp.Api.Base/MyApp.Api.Base.csproj", "Api/MyApp.Api.Base/"]
COPY ["Base/MyApp.Base.Contracts/MyApp.Base.Contracts.csproj", "Base/MyApp.Base.Contracts/"]
COPY ["Base/MyApp.Base.Model/MyApp.Base.Model.csproj", "Base/MyApp.Base.Model/"]
COPY ["Data/MyApp.Data.EntityFramework/MyApp.Data.EntityFramework.csproj", "Data/MyApp.Data.EntityFramework/"]
COPY ["Apps/MyApp.Apps.Settings/MyApp.Apps.Settings.csproj", "Apps/MyApp.Apps.Settings/"]
RUN dotnet restore "Api/MyApp.Api.IdentityServer/MyApp.Api.IdentityServer.csproj"
COPY . .
WORKDIR "/src/Api/MyApp.Api.IdentityServer"
RUN dotnet build "MyApp.Api.IdentityServer.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "MyApp.Api.IdentityServer.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
COPY Api/MyApp.Api.Core/Security/Certificates /app/Security/Certificates
RUN mkdir -p /usr/local/share/ca-certificates/identityserver
RUN chmod -R 777 /usr/local/share/ca-certificates/identityserver
RUN cp /app/Security/Certificates/* /usr/local/share/ca-certificates/identityserver
RUN update-ca-certificates
ENTRYPOINT ["dotnet", "MyApp.Api.IdentityServer.dll"]

docker-compose.yaml:

version: '3.4'

services:
  myapp.api.core:
    image: ${DOCKER_REGISTRY-}myappapicore
    build:
      context: .
      dockerfile: Api/MyApp.Api.Core/Dockerfile
    links:
      - myapp.identity.api:identityserver
    ports:
      - "52008:80"
    volumes:
      - "C:/Projects/User/MyApp.Api/Api/MyApp.Api.Core/Security/Certificates/ca.crt:/usr/local/share/ca-certificates/identityserver/identityserver.crt:ro"
    networks:
      app_net:
       ipv4_address: 192.168.1.200

  myapp.identity.api:
    image: ${DOCKER_REGISTRY-}myappapiidentityserver
    build:
      context: .
      dockerfile: Identity/MyApp.Identity.Api/Dockerfile
    ports:
      - "5000:80"
      - "5001:443"
    volumes:
      - "C:/Projects/User/MyApp.Api/Api/MyApp.Api.IdentityServer/Security/Certificates/ca.crt:/usr/local/share/ca-certificates/identityserver/identityserver.crt:ro"
    networks:
      app_net:
       ipv4_address: 192.168.1.201

networks:
  app_net:
    external: true

我正在使用Visual Studio 2019(16.3.4)。


1
这个完全有道理的问题可以更简短一些,少一些与复制特定细节无关的内容。 - SerG
1个回答

13
这显然是Visual Studio 2019中的“快速模式”优化设计。有关在容器中调试的文档,请参见此处
文档指出,“快速模式”是在VS 2019中调试容器时的默认行为。在此模式下,仅根据Dockerfile构建多阶段构建的第一阶段(基础部分)。然后,VS在主机上处理其余部分,忽略Dockerfile,并通过卷挂载将输出共享到容器。这意味着,在使用VS 2019的Debug配置时,添加到其他阶段的任何自定义步骤都将被忽略。(对于这种不明显、因此可能令人沮丧的优化,给出的原因是在容器中构建比本地机器慢得多。)请注意,此优化仅在使用Debug配置时发生。Release配置将使用整个Dockerfile。
您的选择是:
1. 将自定义步骤放在Dockerfile的第一(基础)步骤中。
或者
  1. Disable this optimization by editing the project file like this:

    <PropertyGroup>
       <ContainerDevelopmentMode>Regular</ContainerDevelopmentMode>
    </PropertyGroup>
    
请注意,它会尝试重用先前构建的容器(如果可能),因此您可能需要执行清理或重新构建以强制构建创建容器的新版本。
祝好运!
**编辑**
在添加容器编排支持(在本例中为Docker Compose)后,尝试使用ContainerDevelopmentMode标志时似乎存在问题。请参见此issue。在问题讨论中建议在docker-compose.dcproj文件上使用此标志,但是有一个错误(仍未修复),使该方法无法正常工作。
第三个选项,暗示了我的先前答案但没有明确说明,将是:
将解决方案配置从Debug切换到Release。
这可以工作,但当您尝试调试应用程序时显然不理想。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接