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

概述Overview

Docker 是轻型容器引擎,在某些方面类似于虚拟机,可以将其用于托管应用程序和服务。Docker is a lightweight container engine, similar in some ways to a virtual machine, which you can use to host applications and services. 本教程介绍如何使用 Visual Studio 将容器化应用程序发布到 Azure 容器注册表This tutorial walks you through using Visual Studio to publish your containerized application to an Azure Container Registry.

如果没有 Azure 订阅,请在开始之前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

完成本教程:To complete this tutorial:

  • 安装带有“ASP.NET 和 Web 开发”工作负载的最新版本 Visual Studio 2017Install the latest version of Visual Studio 2017with the "ASP.NET and web development" workload
  • 安装带有“ASP.NET 和 Web 开发”工作负载的最新版本 Visual Studio 2019Install the latest version of Visual Studio 2019 with the "ASP.NET and web development" workload

创建 ASP.NET Core Web 应用Create an ASP.NET Core web app

以下步骤将指导你完成创建基本 ASP.NET Core 应用(将在本教程中使用)的过程。The following steps guide you through creating a basic ASP.NET Core app that will be used in this tutorial. 如果你已有一个项目,则可跳过此部分。If you already have a project, you can skip this section.

  1. 在 Visual Studio 菜单中,选择“文件” > “新建” > “项目”。In the Visual Studio menu, select File > New > Project.
  2. 在“新建项目”对话框的“模板”部分中,选择“Visual C#” > “Web”。In the Templates section of the New Project dialog box, select Visual C# > Web.
  3. 选择“ASP.NET Core Web 应用程序” 。Select ASP.NET Core Web Application.
  4. 输入新应用程序的名称(或使用默认名称),然后选择“确定” 。Enter a name for your new application (or use the default name), and then select OK.
  5. 选择“Web 应用程序” 。Select Web Application.
  6. 选择“启用 Docker 支持”复选框 。Select the Enable Docker Support check box.
  7. 选择所需的容器类型(Windows 或 Linux),然后选择“确定” 。Select the type of container you want (Windows or Linux), and then select OK.
  1. 在 Visual Studio“启动”窗口,选择“创建新项目” 。In the Visual Studio start window, select Create a new project.
  2. 选择“ASP.NET Core Web 应用程序”,然后选择“下一步” 。Select ASP.NET Core Web Application, and then select Next.
  3. 输入新应用程序的名称(或使用默认名称),然后选择“创建” 。Enter a name for your new application (or use the default name), and then select Create.
  4. 选择“Web 应用程序” 。Select Web Application.
  5. 通过选择或清除“HTTPS 配置”复选框,选择是否需要 SSL 支持 。Choose whether you want SSL support by selecting or clearing the Configure for HTTPS check box.
  6. 选择“启用 Docker 支持”复选框 。Select the Enable Docker Support check box.
  7. 选择所需的容器类型(Windows 或 Linux),然后选择“创建” 。Select the type of container you want (Windows or Linux), and then select Create.

将容器发布到 Azure 容器注册表Publish your container to Azure Container Registry

  1. 在解决方案资源管理器中右键单击项目,并选择“发布” 。Right-click your project in Solution Explorer and choose Publish.

  2. 在“发布目标”对话框中,选择“容器注册表” 。On the Publish target dialog, select Container Registry.

  3. 选择“新建 Azure 容器注册表”并单击“发布” 。Choose New Azure Container Registry and click Publish.

  4. 在“创建新 Azure 容器注册表”中填写所需的值 。Fill in your desired values in the Create a new Azure Container Registry.

    设置Setting 建议的值Suggested value 描述Description
    DNS 前缀DNS Prefix 全局唯一名称Globally unique name 用于唯一标识容器注册表的名称。Name that uniquely identifies your container registry.
    订阅Subscription 选择订阅Choose your subscription 要使用的 Azure 订阅。The Azure subscription to use.
    资源组Resource Group myResourceGroupmyResourceGroup 要在其中创建容器注册表的资源组的名称。Name of the resource group in which to create your container registry. 选择“新建” 创建新的资源组。Choose New to create a new resource group.
    SKUSKU 标准Standard 容器注册表的服务层Service tier of the container registry
    注册表位置Registry Location 靠近你的位置A location close to you 在你附近或将使用容器注册表的其他服务附近的区域中,选择位置。Choose a Location in a region near you or near other services that will use your container registry.

    Visual Studio 的创建 Azure 容器注册表对话框

  5. 单击“创建” Click Create

将容器发布到 Azure 容器注册表Publish your container to Azure Container Registry

  1. 在解决方案资源管理器中右键单击项目,并选择“发布” 。Right-click your project in Solution Explorer and choose Publish.

  2. 在“发布”对话框中,选择“Docker 容器注册表” 。On the Publish dialog, select Docker Container Registry.

    “‘发布’对话框 - 选择‘Docker 容器注册表’”的屏幕截图

  3. 选择“新建 Azure 容器注册表”。Choose Create New Azure Container Registry.

    “‘发布’对话框 - 选择‘新建 Azure 容器注册表’”的屏幕截图

  4. 在“Azure 容器注册表”屏幕中填写所需的值。Fill in your desired values in the Azure Container Registry screen.

    设置Setting 建议的值Suggested value 描述Description
    DNS 前缀DNS Prefix 全局唯一名称Globally unique name 用于唯一标识容器注册表的名称。Name that uniquely identifies your container registry.
    订阅Subscription 选择订阅Choose your subscription 要使用的 Azure 订阅。The Azure subscription to use.
    资源组Resource Group myResourceGroupmyResourceGroup 要在其中创建容器注册表的资源组的名称。Name of the resource group in which to create your container registry. 选择“新建”创建新的资源组。Choose New to create a new resource group.
    SKUSKU 标准Standard 容器注册表的服务层Service tier of the container registry
    注册表位置Registry Location 靠近你的位置A location close to you 在你附近或将使用容器注册表的其他服务附近的区域中,选择位置。Choose a Location in a region near you or near other services that will use your container registry.

    Visual Studio 的创建 Azure 容器注册表对话框

  5. 单击 “创建”Click Create.

  6. 选择“完成”以完成该过程。Choose Finish to complete the process.

现在可以将容器从注册表中拖放到任何能够运行 Docker 映像的主机上,例如Azure 容器实例You can now pull the container from the registry to any host capable of running Docker images, for example Azure Container Instances.

另请参阅See also

快速入门:使用 Azure CLI 在 Azure 中部署容器实例Quickstart: Deploy a container instance in Azure using the Azure CLI