教程:创建 Azure 容器注册表并推送容器映像Tutorial: Create an Azure container registry and push a container image

这是三部分教程的第二部分。This is part two of a three-part tutorial. 本教程的第一部分为 Node.js Web 应用程序创建了 Docker 容器映像。Part one of the tutorial created a Docker container image for a Node.js web application. 在本教程中,我们将此映像推送到 Azure 容器注册表。In this tutorial, you push the image to Azure Container Registry. 如果尚未创建容器映像,请返回教程 1 - 创建容器映像If you haven't yet created the container image, return to Tutorial 1 - Create container image.

Azure 容器注册表是你在 Azure 中的专用 Docker 注册表。Azure Container Registry is your private Docker registry in Azure. 本教程(该系列的第二部分)的内容包括:In this tutorial, part two of the series, you:

  • 使用 Azure CLI 创建 Azure 容器注册表实例Create an Azure Container Registry instance with the Azure CLI
  • 为 Azure 容器注册表标记容器映像Tag a container image for your Azure container registry
  • 将映像上传到注册表Upload the image to your registry

下一篇文章(本系列的最后一篇教程)介绍如何将容器从专用注册表部署到 Azure 容器实例。In the next article, the last in the series, you deploy the container from your private registry to Azure Container Instances.

准备阶段Before you begin

必须满足以下要求才能完成本教程:You must satisfy the following requirements to complete this tutorial:

Azure CLI:必须在本地计算机上安装 Azure CLI 2.0.29 或更高版本。Azure CLI: You must have Azure CLI version 2.0.29 or later installed on your local computer. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

Docker:本教程假设读者基本了解核心 Docker 概念,如容器、容器映像和基本的 docker 命令。Docker: This tutorial assumes a basic understanding of core Docker concepts like containers, container images, and basic docker commands. 有关 Docker 和容器的基础知识,请参阅 Docker 概述For a primer on Docker and container basics, see the Docker overview.

Docker:要完成本教程,需在本地安装 Docker。Docker: To complete this tutorial, you need Docker installed locally. Docker 提供用于在 macOSWindowsLinux 上配置 Docker 环境的包。Docker provides packages that configure the Docker environment on macOS, Windows, and Linux.

重要

必须 在本地计算机 上同时安装 Azure CLI 和 Docker 引擎才能完成本教程。You must install both the Azure CLI and Docker Engine on your local computer to complete this tutorial.

创建 Azure 容器注册表Create Azure container registry

在创建容器注册表之前,需要创建一个资源组,以便将容器注册表部署到其中。 Before you create your container registry, you need a resource group to deploy it to. 资源组是在其中部署和管理所有 Azure 资源的逻辑集合。A resource group is a logical collection into which all Azure resources are deployed and managed.

使用 az group create 命令创建资源组。Create a resource group with the az group create command. 以下示例在 chinaeast2 区域中创建名为 myResourceGroup 的资源组:In the following example, a resource group named myResourceGroup is created in the chinaeast2 region:

az group create --name myResourceGroup --location chinaeast2

创建资源组后,使用 az acr create 命令创建 Azure 容器注册表。Once you've created the resource group, create an Azure container registry with the az acr create command. 容器注册表名称在 Azure 中必须唯一,并且必须包含 5-50 个字母数字字符。The container registry name must be unique within Azure, and contain 5-50 alphanumeric characters. <acrName> 替换为注册表的唯一名称:Replace <acrName> with a unique name for your registry:

az acr create --resource-group myResourceGroup --name <acrName> --sku Basic

下面是名为 mycontainerregistry082 的新 Azure 容器注册表的示例输出(此处显示的内容已截断):Here's example output for a new Azure container registry named mycontainerregistry082 (shown here truncated):

$ az acr create --resource-group myResourceGroup --name mycontainerregistry082 --sku Basic
...
{
  "creationDate": "2018-03-16T21:54:47.297875+00:00",
  "id": "/subscriptions/<Subscription ID>/resourceGroups/myResourceGroup/providers/Microsoft.ContainerRegistry/registries/mycontainerregistry082",
  "location": "chinaeast2",
  "loginServer": "mycontainerregistry082.azurecr.cn",
  "name": "mycontainerregistry082",
  "provisioningState": "Succeeded",
  "resourceGroup": "myResourceGroup",
  "sku": {
    "name": "Basic",
    "tier": "Basic"
  },
  "status": null,
  "storageAccount": null,
  "tags": {},
  "type": "Microsoft.ContainerRegistry/registries"
}

本教程的余下部分使用 <acrName> 作为在此步骤中选择的容器注册表名称的占位符。The rest of the tutorial refers to <acrName> as a placeholder for the container registry name that you chose in this step.

登录到容器注册表Log in to container registry

必须先登录到 Azure 容器注册表实例,才能向其推动映像。You must log in to your Azure Container Registry instance before pushing images to it. 使用 az acr login 命令完成此操作。Use the az acr login command to complete the operation. 必须提供创建容器注册表时选择的唯一名称。You must provide the unique name you chose for the container registry when you created it.

az acr login --name <acrName>

该命令在完成后返回 Login SucceededThe command returns Login Succeeded once completed:

$ az acr login --name mycontainerregistry082
Login Succeeded

标记容器映像Tag container image

若要将容器映像推送到 Azure 容器注册表等专用注册表,必须先使用注册表登录服务器的完整名称来标记该映像。To push a container image to a private registry like Azure Container Registry, you must first tag the image with the full name of the registry's login server.

首先,请获取 Azure 容器注册表的完整登录服务器名称。First, get the full login server name for your Azure container registry. 运行以下 az acr show 命令,并将 <acrName> 替换为刚刚创建的注册表名称:Run the following az acr show command, and replace <acrName> with the name of registry you just created:

az acr show --name <acrName> --query loginServer --output table

例如,如果注册表名为 mycontainerregistry082For example, if your registry is named mycontainerregistry082:

$ az acr show --name mycontainerregistry082 --query loginServer --output table
Result
------------------------
mycontainerregistry082.azurecr.cn

现在,使用 docker images 命令显示本地映像的列表:Now, display the list of your local images with the docker images command:

docker images

此时应会显示在前一篇教程中生成的 aci-tutorial-app 映像,以及计算机上包含的其他所有映像:Along with any other images you have on your machine, you should see the aci-tutorial-app image you built in the previous tutorial:

$ docker images
REPOSITORY          TAG       IMAGE ID        CREATED           SIZE
aci-tutorial-app    latest    5c745774dfa9    39 minutes ago    68.1 MB

使用容器注册表的登录服务器标记 aci-tutorial-app 映像 。Tag the aci-tutorial-app image with the login server of your container registry. 此外,请将 :v1 标记添加到映像名称的末尾,指示映像版本号。Also, add the :v1 tag to the end of the image name to indicate the image version number. <acrLoginServer> 替换为前面执行的 az acr show 命令的结果。Replace <acrLoginServer> with the result of the az acr show command you executed earlier.

docker tag aci-tutorial-app <acrLoginServer>/aci-tutorial-app:v1

再次运行 docker images 以验证标记操作:Run docker images again to verify the tagging operation:

$ docker images
REPOSITORY                                            TAG       IMAGE ID        CREATED           SIZE
aci-tutorial-app                                      latest    5c745774dfa9    39 minutes ago    68.1 MB
mycontainerregistry082.azurecr.cn/aci-tutorial-app    v1        5c745774dfa9    7 minutes ago     68.1 MB

向 Azure 容器注册表推送映像Push image to Azure Container Registry

使用专用注册表的完整登录服务器名称标记 aci-tutorial-app 映像后,可以使用 docker push 命令将该映像推送到注册表 。Now that you've tagged the aci-tutorial-app image with the full login server name of your private registry, you can push the image to the registry with the docker push command. <acrLoginServer> 替换为在前面步骤中获取的完整登录服务器名称。Replace <acrLoginServer> with the full login server name you obtained in the earlier step.

docker push <acrLoginServer>/aci-tutorial-app:v1

push 操作应需要几秒钟到几分钟的时间(具体取决于 Internet 连接),输出应如下所示:The push operation should take a few seconds to a few minutes depending on your internet connection, and output is similar to the following:

$ docker push mycontainerregistry082.azurecr.cn/aci-tutorial-app:v1
The push refers to a repository [mycontainerregistry082.azurecr.cn/aci-tutorial-app]
3db9cac20d49: Pushed
13f653351004: Pushed
4cd158165f4d: Pushed
d8fbd47558a8: Pushed
44ab46125c35: Pushed
5bef08742407: Pushed
v1: digest: sha256:ed67fff971da47175856505585dcd92d1270c3b37543e8afd46014d328f05715 size: 1576

列出 Azure 容器注册表中的映像List images in Azure Container Registry

若要验证刚刚推送的映像是否确实在 Azure 容器注册表中,请使用 az acr repository list 命令列出注册表中的映像。To verify that the image you just pushed is indeed in your Azure container registry, list the images in your registry with the az acr repository list command. <acrName> 替换为容器注册表的名称。Replace <acrName> with the name of your container registry.

az acr repository list --name <acrName> --output table

例如:For example:

$ az acr repository list --name mycontainerregistry082 --output table
Result
----------------
aci-tutorial-app

若要查看特定映像的标记,请使用 az acr repository show-tags 命令。 To see the tags for a specific image, use the az acr repository show-tags command.

az acr repository show-tags --name <acrName> --repository aci-tutorial-app --output table

应该会看到与下面类似的输出:You should see output similar to the following:

$ az acr repository show-tags --name mycontainerregistry082 --repository aci-tutorial-app --output table
Result
--------
v1

后续步骤Next steps

本教程中,我们准备了要用于 Azure 容器实例的 Azure 容器注册表,并将容器映像推送到了该注册表。In this tutorial, you prepared an Azure container registry for use with Azure Container Instances, and pushed a container image to the registry. 已完成以下步骤:The following steps were completed:

  • 使用 Azure CLI 创建 Azure 容器注册表实例Created an Azure Container Registry instance with the Azure CLI
  • 为 Azure 容器注册表标记容器映像Tagged a container image for Azure Container Registry
  • 将映像上传到 Azure 容器注册表Uploaded an image to Azure Container Registry

请继续学习下一篇教程,了解如何使用 Azure 容器实例将容器部署到 Azure:Advance to the next tutorial to learn how to deploy the container to Azure using Azure Container Instances: