【问题标题】:Dockerfile doesn't seem to run while debugging in Visual Studio 2019在 Visual Studio 2019 中调试时 Dockerfile 似乎没有运行
【发布时间】:2019-10-14 08:33:30
【问题描述】:

过去几周我一直在开发一个在 Visual Studio 2019 中使用 C# .NET Core 构建的项目。我启用了 Docker 支持并正在使用 docker-compose 启动 2 个容器(它启动了一个 identityserver4 和 webapi )。我为这两个项目创建了 dockerfiles 并创建了一个 docker-compose 文件来启动服务堆栈。

我遇到的问题是,当我在 Visual Studio 调试模式下运行 docker-compose 时,它​​似乎没有运行我的 Dockerfile。在我的 Dockerfile 的最后一步中,我复制了一些文件并执行了一个命令。这些不会运行。但是,当我在命令行中使用 docker build 时,它会执行那些 Dockerfile 命令。

附加了我的 2 个 docker 文件和 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)

【问题讨论】:

  • 这个完全合理的问题可能会更短一点,因为非重现特定的细节更少。

标签: visual-studio docker docker-compose dockerfile


【解决方案1】:

这显然是在 Visual Studio 2019 中作为“快速模式”优化设计的。请参阅容器中的调试文档here

它指出“快速模式”是在 VS 2019 中调试容器时的默认行为。在这种模式下,仅根据 Dockerfile 构建多阶段构建的第一阶段(基础)。然后 VS 处理主机上的其余部分,忽略 Dockerfile,并通过使用卷挂载将输出共享到容器。这意味着在 VS 2019 中使用 Debug 配置时,您添加到其他阶段的任何自定义步骤都将被忽略。(这种不明显且因此可能令人沮丧的优化的原因是容器中的构建比在容器中慢得多本地机器。)请注意,此优化仅在使用调试配置时发生。 Release 配置将使用整个 Dockerfile。

您的选择是:

  1. 将您的自定义步骤放在 Dockerfile 的第一个(基本)步骤中。

  1. 通过像这样编辑项目文件来禁用此优化:

    <PropertyGroup>
       <ContainerDevelopmentMode>Regular</ContainerDevelopmentMode>
    </PropertyGroup>
    

另外请记住,如果可能,它会尝试重用以前构建的容器,因此您可能需要执行 Clean 或 Rebuild 以强制构建创建容器的新版本。

祝你好运!

** 编辑 **

在添加 Container Orchestration Support(在本例中为 Docker Compose)后尝试使用 ContainerDevelopmentMode 标志时似乎存在问题。请参阅此issue。在问题讨论中建议可以在 docker-compose.dcproj 文件中使用此标志,但有一个错误(仍未修复)使该方法无法正常工作。

在我之前的回答中暗示但未明确说明的第三个选项是:

  1. 将您的解决方案配置从调试切换到发布。

这可行,但在您尝试调试应用程序时显然不理想。

【讨论】:

    猜你喜欢
    • 2013-10-21
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    • 2011-03-27
    • 2020-05-11
    相关资源
    最近更新 更多