Visual Studio 中的容器工具Container Tools in Visual Studio

Visual Studio 中用于使用容器进行开发的工具易于使用,并大大简化生成、调试和部署容器化应用程序的过程。The tools included in Visual Studio for developing with containers are easy to use, and greatly simplify building, debugging, and deployment for containerized applications. 可以将容器用于单个项目,也可以将容器业务流程与 Docker Compose、Service Fabric 或 Kubernetes 结合使用,以便使用容器中的多个服务。You can work with a container for a single project, or use container orchestration with Docker Compose, Service Fabric, or Kubernetes to work with multiple services in containers.

先决条件Prerequisites

Visual Studio 中的 Docker 支持Docker support in Visual Studio

Docker 支持适用于 ASP.NET 项目、ASP.NET Core 项目,以及 .NET Core 和 .NET Framework 控制台项目。Docker support is available for ASP.NET projects, ASP.NET Core projects, and .NET Core and .NET Framework console projects.

Visual Studio 中的 Docker 支持因版本而异,以响应客户需求。The support for Docker in Visual Studio has changed over a number of releases in response to customer needs. 可以向项目添加两个级别的 Docker 支持,并且受支持的选项因项目类型和 Visual Studio 版本而异。There are two levels of Docker support you can add to a project, and the supported options vary by the type of project and the version of Visual Studio. 借助某些受支持的项目类型,如果只想将容器用于单个项目,而不使用业务流程,则可以通过添加 Docker 支持来完成。With some supported project types, if you just want a container for a single project, without using orchestration, you can do that by adding Docker support. 下一级别是容器业务流程支持,该支持可为所选的特定业务流程协调程序添加相应的支持文件。The next level is container orchestration support, which adds appropriate support files for the particular orchestrator you choose.

借助 Visual Studio 2017,可以将 Docker Compose 和 Service Fabric 用作容器业务流程服务。With Visual Studio 2017, you can use Docker Compose and Service Fabric as container orchestration services. 如果安装 Visual Studio Tools for Kubernetes,则也可以使用 Kubernetes。You can also use Kubernetes if you install the Visual Studio Tools for Kubernetes.

备注

如果使用的是 15.8 之前的 Visual Studio 2017 版本或使用的是 .NET Framework 项目模板(而不是 .NET Core),则在添加 Docker 支持时,会使用 Docker Compose 自动添加业务流程支持。If you are using a version of Visual Studio 2017 prior to 15.8, or you are using the .NET Framework project template (not .NET Core), when you add Docker support, orchestration support using Docker Compose is added automatically. 在 Visual Studio 2017 版本 15.0 到 15.7 中通过 Docker Compose 自动添加容器业务流程支持,并且该支持适用于 .NET Framework 项目。Container orchestration support, via Docker Compose, is added automatically in Visual Studio 2017 versions 15.0 to 15.7 and for .NET Framework projects.

先决条件Prerequisites

Visual Studio 中的 Docker 支持Docker support in Visual Studio

Docker 支持适用于 ASP.NET 项目、ASP.NET Core 项目,以及 .NET Core 和 .NET Framework 控制台项目。Docker support is available for ASP.NET projects, ASP.NET Core projects, and .NET Core and .NET Framework console projects.

Visual Studio 中的 Docker 支持因版本而异,以响应客户需求。The support for Docker in Visual Studio has changed over a number of releases in response to customer needs. 可以向项目添加两个级别的 Docker 支持,并且受支持的选项因项目类型和 Visual Studio 版本而异。There are two levels of Docker support you can add to a project, and the supported options vary by the type of project and the version of Visual Studio. 借助某些受支持的项目类型,如果只想将容器用于单个项目,而不使用业务流程,则可以通过添加 Docker 支持来完成。With some supported project types, if you just want a container for a single project, without using orchestration, you can do that by adding Docker support. 下一级别是容器业务流程支持,该支持可为所选的特定业务流程协调程序添加相应的支持文件。The next level is container orchestration support, which adds appropriate support files for the particular orchestrator you choose.

借助 Visual Studio 2019,可以将 Docker Compose、Kubernetes 和 Service Fabric 用作容器业务流程服务。With Visual Studio 2019, you can use Docker Compose, Kubernetes, and Service Fabric as container orchestration services.

备注

如果你使用完整的 .NET Framework 控制台项目模板,则在创建项目后,支持的选项是“添加容器业务流程协调程序支持”,它包括使用 Service Fabric 或 Docker Compose 的选项****。If you are using the full .NET Framework console project template, the supported option is Add Container Orchestrator support after project creation, with options to use Service Fabric or Docker Compose. 对于没有业务流程的单个项目,无法在项目创建时添加支持,也无法添加 Docker 支持****。Adding support at project creation and Add Docker support for a single project without orchestration are not available options.

在 Visual Studio 2019 版本 16.4 及更高版本中,提供了“容器”窗口,你可用它来查看正在运行的容器,浏览可用的映像,查看环境变量、日志和端口映射,检查文件系统,附加调试器,或者在容器环境中打开终端窗口****。In Visual Studio 2019 version 16.4 and later, the Containers window is available, which lets you view running containers, browse available images, view environment variables, logs, and port mappings, inspect the filesystem, attach a debugger, or open a terminal window inside the container environment. Visual Studio 中查看和诊断容器和映像See View and diagnose containers and images in Visual Studio.

添加 Docker 支持Adding Docker support

可以通过在创建新项目时选择“启用 Docker 支持”来在项目创建期间启用 Docker 支持,如以下屏幕截图所示****:You can enable Docker support during project creation by selecting Enable Docker Support when creating a new project, as shown in the following screenshot:

在 Visual Studio 中为新的 ASP.NET Core Web 应用启用 Docker 支持

在 Visual Studio 中为新的 ASP.NET Core Web 应用启用 Docker 支持

备注

对于 .NET Framework 项目(而不是 .NET Core),只有 Windows 容器可用。For .NET Framework projects (not .NET Core), only Windows containers are available.

可以通过在“解决方案资源管理器”中选择“添加” > “Docker 支持”来向现有项目添加 Docker 支持。You can add Docker support to an existing project by selecting Add > Docker Support in Solution Explorer. Add > Docker Support**** 和 Add > Container Orchestrator Support**** 命令位于“解决方案资源管理器”**** 中 ASP.NET Core 项目的项目节点的右键单击菜单(或上下文菜单)上,如以下屏幕截图所示:The Add > Docker Support and Add > Container Orchestrator Support commands are located on the right-click menu (or context menu) of the project node for an ASP.NET Core project in Solution Explorer, as shown in the following screenshot:

Visual Studio 中的“添加 Docker 支持”菜单选项

当添加或启用 Docker 支持时,Visual Studio 会向项目添加以下各项:When you add or enable Docker support, Visual Studio adds the following to the project:

  • Dockerfile** 文件a Dockerfile file
  • .dockerignore 文件a .dockerignore file
  • 对 Microsoft.VisualStudio.Azure.Containers.Tools.Targets 的 NuGet 包引用a NuGet package reference to the Microsoft.VisualStudio.Azure.Containers.Tools.Targets

添加 Docker 支持后,解决方案如下所示:The solution looks like this once you add Docker support:

包含 Dockerfile 和 .dockerignore 文件的解决方案资源管理器的屏幕截图

备注

当按以下屏幕截图所示在项目创建期间为 ASP.NET 项目(.NET Framework,而不是 .NET Core 项目)启用 Docker 支持时,还会添加容器业务流程支持。When you enable Docker support during project creation for a ASP.NET project (.NET Framework, not a .NET Core project) as shown in the following screenshot, container orchestration support is also added.

为 ASP.NET 项目启用 Docker Compose 支持

Docker Compose 支持Docker Compose support

当你想要使用 Docker Compose 撰写多容器解决方案时,请向项目添加容器业务流程支持。When you want to compose a multi-container solution using Docker Compose, add container orchestration support to your projects. 这样就可以同时运行和调试一组容器(整个解决方案或项目组)(如果已在同一个 docker-compose.yml** 文件中定义这些容器)。This lets you run and debug a group of containers (a whole solution or group of projects) at the same time if they're defined in the same docker-compose.yml file.

若要使用 Docker Compose 添加容器业务流程支持,请右键单击“解决方案资源管理器”**** 中的解决方案或项目节点,然后选择“添加”>“容器业务流程支持”****。To add container orchestration support using Docker Compose, right-click on the solution or project node in Solution Explorer, and choose Add > Container Orchestration Support. 然后,选择“Docker Compose”**** 以管理容器。Then choose Docker Compose to manage the containers.

向项目添加容器业务流程支持后,会看到添加到项目的 Dockerfile**(如果尚无)以及添加到“解决方案资源管理器”**** 中的某个解决方案的 docker-compose**** 文件夹,如下所示:After you add container orchestration support to your project, you see a Dockerfile added to the project (if there wasn't one there already) and a docker-compose folder added to the solution in Solution Explorer, as shown here:

Visual Studio 解决方案资源管理器中的 Docker 文件

如果 docker-compose.yml** 已存在,Visual Studio 只需向其添加配置代码所需的行。If docker-compose.yml already exists, Visual Studio just adds the required lines of configuration code to it.

对要使用 Docker Compose 控制的其他项目重复该过程。Repeat the process with the other projects that you want to control using Docker Compose.

Kubernetes 支持Kubernetes support

若要添加 Kubernetes 支持,请安装 Visual Studio Tools for KubernetesTo add Kubernetes support, install the Visual Studio Tools for Kubernetes.

借助 Kubernetes 支持,可以在本地项目和 Azure Kubernetes 服务 (AKS) 中运行的 Kubernetes 群集之间启用连接,从而使用 Visual Studio 修改和调试 AKS 中运行的服务。With Kubernetes support, you can enable a connection between your local project and a Kubernetes cluster running in Azure Kubernetes Service (AKS), and thereby modify and debug your services running in AKS using Visual Studio. 此服务由 Azure Dev Spaces 提供。This service is provided by Azure Dev Spaces. 通过 Azure Dev Spaces,还可以设置名为 dev spaces** 的 Kubernetes 服务的单独分支用于开发目的,因此可以有效地将生产服务与开发中的工作版本隔离,并使每个修改完全不同。Azure Dev Spaces also lets you set up separate branches of your Kubernetes services called dev spaces for development purposes, so you can efficiently isolate production services from working versions in development, and keep distinct modifications cleanly separated from each other.

若要向项目添加 Kubernetes 支持,请在添加容器业务流程支持时选择“Kubernetes/Helm”****。To add Kubernetes support to your projects, choose Kubernetes/Helm when you add container orchestration support. 多个文件添加到项目,其中包括 azds.yaml**(用于配置 Azure Dev Spaces)和 Helm 图表(用于描述 Kubernetes 服务的结构)。Several files are added to your project, including azds.yaml, which configures Azure Dev Spaces, and Helm charts which describe the structure of your Kubernetes services.

Service Fabric 支持Service Fabric support

借助 Visual Studio 中的 Service Fabric 工具,可以开发和调试 Azure Service Fabric、进行本地运行和调试并部署到 Azure。With Service Fabric tools in Visual Studio, you can develop and debug for Azure Service Fabric, run and debug locally, and deploy to Azure.

安装了 Azure 开发工作负载的 Visual Studio 2017 版本 15.9 及更高版本支持使用 Windows 容器和 Service Fabric 业务流程来开发容器化微服务。Visual Studio 2017 version 15.9 and later with the Azure development workload installed supports developing containerized microservices using Windows containers and Service Fabric orchestration.

Visual Studio 2019 支持使用 Windows 容器和 Service Fabric 业务流程来开发容器化微服务。Visual Studio 2019 supports developing containerized microservices using Windows containers and Service Fabric orchestration.

有关详细教程,请参阅教程:将 Windows 容器中的 .NET 应用程序部署到 Azure Service FabricFor a detailed tutorial, see Tutorial: Deploy a .NET application in a Windows container to Azure Service Fabric.

有关 Azure Service Fabric 的详细信息,请参阅 Service FabricFor more information on Azure Service Fabric, see Service Fabric.

持续交付和持续集成 (CI/CD)Continuous delivery and continuous integration (CI/CD)

Visual Studio 与 Azure Pipelines 轻松集成,以便自动完成服务代码和配置更改的持续集成和交付。Visual Studio integrates readily with Azure Pipelines for automated and continuous integration and delivery of changes to your service code and configuration. 若要开始使用,请参阅创建第一个管道To get started, see Create your first pipeline.

有关 Service Fabric 的信息,请参阅教程:使用 Azure DevOps Projects 将 ASP.NET Core 应用部署到 Azure Service FabricFor Service Fabric, see Tutorial: Deploy your ASP.NET Core app to Azure Service Fabric by using Azure DevOps Projects.

有关 Kubernetes 的信息,请参阅将 Docker 容器应用部署到 Azure Kubernetes 服务For Kubernetes, see Deploy a Docker container app to Azure Kubernetes Service.

后续步骤Next steps

有关服务实现以及将 Visual Studio 工具用于容器的更多详细信息,请阅读以下文章:For further details on the services implementation and use of Visual Studio tools for working with containers, read the following articles:

在本地 Docker 容器中调试应用 Debugging apps in a local Docker container

使用 Visual Studio 将 ASP.NET 容器部署到容器注册表Deploy an ASP.NET container to a container registry using Visual Studio