备注
本部分介绍如何在选择 Dockerfile 容器生成类型时自定义 Docker 容器。 如果使用 .NET SDK 生成类型,则自定义选项不同,本节中的大部分信息不适用。 相反,请参阅 使用 dotnet publish容器化 .NET 应用。
在“调试”配置中生成时,Visual Studio 执行的一些优化操作有助于提高容器化项目生成过程的性能。 容器化应用的生成过程并不像按照 Dockerfile 中所述的步骤那样简单。 在容器中生成的速度比在本地计算机上生成慢。 因此,在“调试”配置中生成时,Visual Studio 实际上会在本地计算机上生成项目,然后使用卷装载将输出文件夹共享到容器。 启用此优化的构建称为 快速 模式构建。
在 快速 模式下,Visual Studio 调用 docker build
,该参数指示 Docker 仅生成 Dockerfile 中的第一个阶段(通常是 base
阶段)。 可以通过设置 容器工具 MSBuild 属性中所述的 MSBuild 属性 DockerfileFastModeStage
来更改该属性。 Visual Studio 处理进程的其余部分,而不考虑 Dockerfile 的内容。 因此,修改 Dockerfile(例如自定义容器环境或安装其他依赖项)时,应将修改放在第一阶段。 不会执行 Dockerfile 的 build
、publish
或 final
阶段中的任何自定义步骤。
该性能优化通常仅在您使用 调试 配置进行构建时发生。 在 发布 配置中,构建在 Dockerfile 中指定的容器内进行。 可以通过在项目文件中将 ContainerDevelopmentMode
设置为 快速 来为发布配置启用此行为:
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<ContainerDevelopmentMode>Fast</ContainerDevelopmentMode>
</PropertyGroup>
如果要禁用所有配置的性能优化,并按 Dockerfile 指定生成,请将 ContainerDevelopmentMode 属性在项目文件中设置为 正常,如下所示:
<PropertyGroup>
<ContainerDevelopmentMode>Regular</ContainerDevelopmentMode>
</PropertyGroup>
若要还原性能优化,请从项目文件中删除该属性。
启动调试(F5),如果可能,则会重复使用以前启动的容器。 如果不想重复使用以前的容器,可以使用 重新生成 或 Visual Studio 中的 Clean 命令来强制 Visual Studio 使用新的容器。
运行调试器的过程取决于项目和容器操作系统的类型:
方案 | 调试器进程 |
---|---|
.NET Core 应用(Linux 容器) | Visual Studio 下载 vsdbg 并将其映射到容器,然后使用程序和参数(即 dotnet webapp.dll )调用它,然后调试器附加到进程。 |
.NET Core 应用(Windows 容器) | Visual Studio 使用 onecoremsvsmon 并将其映射到容器,将其作为入口点运行,然后 Visual Studio 连接到它并附加到程序。 |
.NET Framework 应用 | Visual Studio 使用 msvsmon 并将其映射到容器,将其作为 Visual Studio 可以连接到容器的入口点的一部分运行,并附加到程序。 这类似于通常在另一台计算机或虚拟机上设置远程调试的方式。 |
有关 vsdbg.exe
的信息,请参阅 Visual Studio 中对 Linux 上的 .NET Core 和 OS X 的Offroad 调试。
修改用于调试和生产的容器镜像
若要修改用于调试和生产的容器映像,请修改 base
阶段。 将自定义项添加到基础阶段部分中的 Dockerfile,通常是 Dockerfile 中的第一部分。 有关 Dockerfile 命令的信息,请参阅 Docker 文档中的 Dockerfile 参考。
# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# <add your commands here>
# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApplication3/WebApplication3.csproj", "WebApplication3/"]
RUN dotnet restore "WebApplication3/WebApplication3.csproj"
COPY . .
WORKDIR "/src/WebApplication3"
RUN dotnet build "WebApplication3.csproj" -c Release -o /app/build
# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
RUN dotnet publish "WebApplication3.csproj" -c Release -o /app/publish
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication3.dll"]
仅修改容器映像以供调试
可以在某些方面自定义容器,以帮助进行调试,例如出于诊断目的安装某些内容,而不会影响生产版本。
若要仅修改容器进行调试,请创建一个阶段,然后使用 MSBuild 属性 DockerfileFastModeStage
告知 Visual Studio 在调试时使用自定义阶段。 有关 Dockerfile 命令的信息,请参阅 Docker 文档中的 Dockerfile 参考。
备注
此处的说明适用于单容器事例。 也可以使用 Docker Compose 对多个容器执行相同的操作,但 Docker Compose 所需的技术略有不同。 例如,阶段由 dockercompose.debug.yml
文件中的设置控制。
在以下示例中,我们将安装包 procps-ng
,但仅在调试模式下安装。 此包提供命令 pidof
,Visual Studio 需要该命令(面向 .NET 5 和更早版本时),但它未包含在此使用的 Mariner 镜像中。 我们用于快速模式调试的阶段是 debug
,这是此处定义的一个自定义阶段。 快速模式阶段不需要从 build
或 publish
阶段进行继承,它可以直接从 base
阶段继承,因为如本文前面提到的,Visual Studio 装载了一个包含运行应用所需所有内容的卷。
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:6.0-cbl-mariner2.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM base AS debug
RUN tdnf install procps-ng -y
# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:6.0-cbl-mariner2.0 AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]
在项目文件中,添加此设置以告知 Visual Studio 在调试时使用自定义阶段 debug
。
<PropertyGroup>
<!-- other property settings -->
<DockerfileFastModeStage>debug</DockerfileFastModeStage>
</PropertyGroup>
使用 AOT 部署自定义调试映像
为了支持本机 AOT 部署,GNU 调试器(GDB)已安装,但仅安装在用于调试的映像上,而不是最终运行时的映像上。 Dockerfile 包括一个构建参数 LAUNCHING_FROM_VS
,它可以是 true
或 false
。 如果 true
,则使用 aotdebug
阶段,即安装 GDB 的位置。 请注意,Visual Studio 仅支持适用于 Linux 容器的原生 AOT 和 GDB。
# These ARGs allow for swapping out the base used to make the final image when debugging from VS
ARG LAUNCHING_FROM_VS
# This sets the base image for final, but only if LAUNCHING_FROM_VS has been defined
ARG FINAL_BASE_IMAGE=${LAUNCHING_FROM_VS:+aotdebug}
# ... (other stages omitted)
# This stage is used as the base for the final stage when launching from VS to support debugging in regular mode (Default when not using the Debug configuration)
FROM base as aotdebug
USER root
# Install GDB to support native debugging
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
gdb
USER app
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM ${FINAL_BASE_IMAGE:-mcr.microsoft.com/dotnet/runtime-deps:8.0} AS final
WORKDIR /app
EXPOSE 8080
COPY --from=publish /app/publish .
ENTRYPOINT ["./WebApplication1"]
可以使用 Dockerfile 中的 aotstage
来自定义调试时使用的映像,这不会影响在未从 Visual Studio 启动时或在生产环境中使用的最终映像。 例如,可以安装仅在调试期间使用的工具。
相关内容
- 在 Visual Studio 中自定义 Docker 容器
- 从命令行生成容器项目