您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:部署并使用 Azure 容器注册表Tutorial: Deploy and use Azure Container Registry

Azure 容器注册表 (ACR) 是容器映像的专用注册表。Azure Container Registry (ACR) is a private registry for container images. 可以通过专用容器注册表安全地生成和部署应用程序和自定义代码。A private container registry lets you securely build and deploy your applications and custom code. 本教程的第 2 部分(共 7 部分)介绍如何部署 ACR 实例并向其推送容器映像。In this tutorial, part two of seven, you deploy an ACR instance and push a container image to it. 学习如何:You learn how to:

  • 创建 Azure 容器注册表 (ACR) 实例Create an Azure Container Registry (ACR) instance
  • 标记 ACR 的容器映像Tag a container image for ACR
  • 向 ACR 上传映像Upload the image to ACR
  • 查看注册表中的映像View images in your registry

在其他教程中,此 ACR 实例将与 AKS 中的 Kubernetes 群集集成,而应用程序则通过映像进行部署。In additional tutorials, this ACR instance is integrated with a Kubernetes cluster in AKS, and an application is deployed from the image.

开始之前Before you begin

上一教程中,已经为一个 Azure Voting 应用程序示例创建了容器映像。In the previous tutorial, a container image was created for a simple Azure Voting application. 如果尚未创建 Azure Voting 应用映像,请返回到教程 1:创建容器映像If you have not created the Azure Voting app image, return to Tutorial 1 – Create container images.

此教程需要运行 Azure CLI 2.0.53 或更高版本。This tutorial requires that you're running the Azure CLI version 2.0.53 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建 Azure 容器注册表Create an Azure Container Registry

若要创建 Azure 容器注册表,首先需要一个资源组。To create an Azure Container Registry, you first need a resource group. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

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

az group create --name myResourceGroup --location eastus

使用 az acr create 命令创建 Azure 容器注册表实例,并提供你自己的注册表名称。Create an Azure Container Registry instance with the az acr create command and provide your own registry name. 注册表名称在 Azure 中必须唯一,并且包含 5-50 个字母数字字符。The registry name must be unique within Azure, and contain 5-50 alphanumeric characters. 在本教程的剩余部分,请使用 <acrName> 作为容器注册表名称的占位符。In the rest of this tutorial, <acrName> is used as a placeholder for the container registry name. 提供自己的唯一注册表名称。Provide your own unique registry name. “基本”SKU 是一个针对成本优化的入口点,适用于可以对存储和吞吐量进行均衡考虑的开发目的。 The Basic SKU is a cost-optimized entry point for development purposes that provides a balance of storage and throughput.

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

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

若要使用 ACR 实例,必须先登录。To use the ACR instance, you must first log in. 使用 az acr login 命令并提供一个唯一名称,该名称是在上一步提供给容器注册表的。Use the az acr login command and provide the unique name given to the container registry in the previous step.

az acr login --name <acrName>

完成后,该命令会返回“登录成功” 消息。The command returns a Login Succeeded message once completed.

标记容器映像Tag a container image

若要查看当前的本地映像的列表,请使用 docker images 命令:To see a list of your current local images, use the docker images command:

$ docker images

REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
azure-vote-front             latest              4675398c9172        13 minutes ago      694MB
redis                        latest              a1b99da73d05        7 days ago          106MB
tiangolo/uwsgi-nginx-flask   flask               788ca94b2313        9 months ago        694MB

若要将 azure-vote-front 容器映像与 ACR 配合使用,需使用注册表的登录服务器地址对映像进行标记。To use the azure-vote-front container image with ACR, the image needs to be tagged with the login server address of your registry. 在将容器映像推送到映像注册表时,使用此标记进行路由。This tag is used for routing when pushing container images to an image registry.

若要获取登录服务器地址,请使用 az acr list 命令并查询是否存在 loginServer,如下所示:To get the login server address, use the az acr list command and query for the loginServer as follows:

az acr list --resource-group myResourceGroup --query "[].{acrLoginServer:loginServer}" --output table

现在,请使用容器注册表的 acrloginServer 地址标记本地 azure-vote-front 映像。Now, tag your local azure-vote-front image with the acrloginServer address of the container registry. 若要指示映像版本,请将 :v1 添加到映像名称的末尾:To indicate the image version, add :v1 to the end of the image name:

docker tag azure-vote-front <acrLoginServer>/azure-vote-front:v1

若要验证是否已应用标记,请再次运行 docker imagesTo verify the tags are applied, run docker images again. 系统会使用 ACR 实例地址和版本号对映像进行标记。An image is tagged with the ACR instance address and a version number.

$ docker images

REPOSITORY                                           TAG           IMAGE ID            CREATED             SIZE
azure-vote-front                                     latest        eaf2b9c57e5e        8 minutes ago       716 MB
mycontainerregistry.azurecr.io/azure-vote-front      v1            eaf2b9c57e5e        8 minutes ago       716 MB
redis                                                latest        a1b99da73d05        7 days ago          106MB
tiangolo/uwsgi-nginx-flask                           flask         788ca94b2313        8 months ago        694 MB

将映像推送到注册表Push images to registry

生成并标记映像后,将 azure-vote-front 映像推送到 ACR 实例。With your image built and tagged, push the azure-vote-front image to your ACR instance. 使用 docker push 并提供自己的适用于映像名称的 acrLoginServer 地址,如下所示:Use docker push and provide your own acrLoginServer address for the image name as follows:

docker push <acrLoginServer>/azure-vote-front:v1

可能需要数分钟才能将映像推送到 ACR。It may take a few minutes to complete the image push to ACR.

列出注册表中的映像List images in registry

若要返回已推送到 ACR 实例的映像列表,请使用 az acr repository list 命令。To return a list of images that have been pushed to your ACR instance, use the az acr repository list command. 按如下所示提供自己的 <acrName>Provide your own <acrName> as follows:

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

以下示例输出将 azure-vote-front 映像列为在注册表中可用:The following example output lists the azure-vote-front image as available in the registry:

Result
----------------
azure-vote-front

若要查看特定映像的标记,请使用 az acr repository show-tags 命令,如下所示:To see the tags for a specific image, use the az acr repository show-tags command as follows:

az acr repository show-tags --name <acrName> --repository azure-vote-front --output table

以下示例输出显示在上一步标记的 v1 映像:The following example output shows the v1 image tagged in a previous step:

Result
--------
v1

你现在有了一个容器映像,该映像存储在专用的 Azure 容器注册表实例中。You now have a container image that is stored in a private Azure Container Registry instance. 在下一教程中,此映像会从 ACR 部署到 Kubernetes 群集。This image is deployed from ACR to a Kubernetes cluster in the next tutorial.

后续步骤Next steps

在本教程中,你创建了一个 Azure 容器注册表并推送了可以在 AKS 群集中使用的映像。In this tutorial, you created an Azure Container Registry and pushed an image for use in an AKS cluster. 你已了解如何:You learned how to:

  • 创建 Azure 容器注册表 (ACR) 实例Create an Azure Container Registry (ACR) instance
  • 标记 ACR 的容器映像Tag a container image for ACR
  • 向 ACR 上传映像Upload the image to ACR
  • 查看注册表中的映像View images in your registry

继续学习下一篇教程,了解如何在 Azure 中部署 Kubernetes 群集。Advance to the next tutorial to learn how to deploy a Kubernetes cluster in Azure.