快速入门:使用 Azure CLI 创建专用容器注册表Quickstart: Create a private container registry using the Azure CLI

Azure 容器注册表是托管的 Docker 容器注册表服务,用于存储专用的 Docker 容器映像。Azure Container Registry is a managed Docker container registry service used for storing private Docker container images. 本指南详述了如何使用 Azure CLI 创建 Azure 容器注册表实例。This guide details creating an Azure Container Registry instance using the Azure CLI. 然后,使用 Docker 命令将容器映像推送到注册表中,最终从注册表提取并运行该映像。Then, use Docker commands to push a container image into the registry, and finally pull and run the image from your registry.

本快速入门需要运行 Azure CLI(建议运行 2.0.55 或更高版本)。This quickstart requires that you are running the Azure CLI (version 2.0.55 or later recommended). 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

还必须在本地安装 Docker。You must also have Docker installed locally. Docker 提供的包可在任何 macOSWindowsLinux 系统上轻松配置 Docker。Docker provides packages that easily configure Docker on any macOS, Windows, or Linux system.

创建资源组Create a resource group

使用 az group create 命令创建资源组。Create a resource group with the az group create command. Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed.

以下示例在“chinanorth” 位置创建名为“myResourceGroup” 的资源组。The following example creates a resource group named myResourceGroup in the chinanorth location.

az group create --name myResourceGroup --location chinanorth

创建容器注册表Create a container registry

本快速入门将创建一个“基本”注册表。该注册表已针对成本进行优化,是可供开发人员了解 Azure 容器注册表的选项。In this quickstart you create a Basic registry, which is a cost-optimized option for developers learning about Azure Container Registry. 有关可用服务层级的详细信息,请参阅容器注册表服务层级For details on available service tiers, see Container registry service tiers.

使用 az acr create 命令创建 ACR 实例。Create an ACR instance using the az acr create command. 注册表名称在 Azure 中必须唯一,并且包含 5-50 个字母数字字符。The registry name must be unique within Azure, and contain 5-50 alphanumeric characters. 以下示例使用 myContainerRegistry007。In the following example, myContainerRegistry007 is used. 将其更新为唯一值。Update this to a unique value.

az acr create --resource-group myResourceGroup \
  --name myContainerRegistry007 --sku Basic

创建注册表时,输出与下面类似:When the registry is created, the output is similar to the following:

{
  "adminUserEnabled": false,
  "creationDate": "2019-01-08T22:32:13.175925+00:00",
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.ContainerRegistry/registries/myContainerRegistry007",
  "location": "chinanorth",
  "loginServer": "mycontainerregistry007.azurecr.cn",
  "name": "myContainerRegistry007",
  "provisioningState": "Succeeded",
  "resourceGroup": "myResourceGroup",
  "sku": {
    "name": "Basic",
    "tier": "Basic"
  },
  "status": null,
  "storageAccount": null,
  "tags": {},
  "type": "Microsoft.ContainerRegistry/registries"
}

记下输出中的 loginServer,这是完全限定的注册表名称(全部小写)。Take note of loginServer in the output, which is the fully qualified registry name (all lowercase). 在本快速入门的剩余部分,<registry-name> 是容器注册表名称的占位符,<login-server> 是注册表的登录服务器名称的占位符。Throughout the rest of this quickstart <registry-name> is a placeholder for the container registry name, and <login-server> is a placeholder for the registry's login server name.

登录到注册表Log in to registry

在推送和拉取容器映像之前,必须登录到注册表。Before pushing and pulling container images, you must log in to the registry. 为此,请使用 az acr login 命令。To do so, use the az acr login command.

az acr login --name <registry-name>

该命令在完成后会返回消息 Login SucceededThe command returns a Login Succeeded message once completed.

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

要将映像推送到 Azure 容器注册表,首先必须具有一个映像。To push an image to an Azure Container registry, you must first have an image. 如果还没有任何本地容器映像,请运行以下 docker pull 命令,从 Docker Hub 拉取现有映像。If you don't yet have any local container images, run the following docker pull command to pull an existing image from Docker Hub. 就此示例来说,请拉取 hello-world 映像。For this example, pull the hello-world image.

docker pull hello-world

将映像推送到注册表之前,必须使用 ACR 登录服务器的完全限定的名称进行标记。Before you can push an image to your registry, you must tag it with the fully qualified name of your ACR login server. 登录服务器名称采用 <registry-name>.azurecr.cn(全小写)格式,例如 mycontainerregistry007.azurecr.cnThe login server name is in the format <registry-name>.azurecr.cn (all lowercase), for example, mycontainerregistry007.azurecr.cn.

使用 docker tag 命令标记映像。Tag the image using the docker tag command. 使用 ACR 实例的登录服务器名称替换 <acrLoginServer>Replace <acrLoginServer> with the login server name of your ACR instance.

docker tag hello-world <acrLoginServer>/hello-world:v1

最后,使用 docker push 将映像推送到 ACR 实例。Finally, use docker push to push the image to the ACR instance. 使用 ACR 实例的登录服务器名称替换 <acrLoginServer>Replace <acrLoginServer> with the login server name of your ACR instance. 此示例创建 hello-world 存储库,其中包含 hello-world:v1 映像。This example creates the hello-world repository, containing the hello-world:v1 image.

docker push <acrLoginServer>/hello-world:v1

将映像推送到容器注册表后,请从本地 Docker 环境中删除 hello-world:v1 映像。After pushing the image to your container registry, remove the hello-world:v1 image from your local Docker environment. (请注意,此 docker rmi 命令不从 Azure 容器注册表中的 hello-world 存储库删除该映像。)(Note that this docker rmi command does not remove the image from the hello-world repository in your Azure container registry.)

docker rmi <acrLoginServer>/hello-world:v1

列出容器映像List container images

以下示例列出了注册表中的存储库:The following example lists the repositories in your registry:

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

输出:Output:

Result
----------------
hello-world

以下示例列出了 hello-world 存储库中的标记。The following example lists the tags on the hello-world repository.

az acr repository show-tags --name <registry-name> --repository hello-world --output table

输出:Output:

Result
--------
v1

从注册表运行映像Run image from registry

现在,可以使用 hello-world:v1docker run 从容器注册表拉取并运行 容器映像:Now, you can pull and run the hello-world:v1 container image from your container registry by using docker run:

docker run <acrLoginServer>/hello-world:v1  

示例输出:Example output:

Unable to find image 'mycontainerregistry007.azurecr.cn/hello-world:v1' locally
v1: Pulling from hello-world
Digest: sha256:662dd8e65ef7ccf13f417962c2f77567d3b132f12c95909de6c85ac3c326a345
Status: Downloaded newer image for mycontainerregistry007.azurecr.cn/hello-world:v1

Hello from Docker!
This message shows that your installation appears to be working correctly.

[...]

清理资源Clean up resources

如果不再需要存储在该处的资源组、容器注册表和容器映像,可以使用 az group delete 命令将其删除。When no longer needed, you can use the az group delete command to remove the resource group, the container registry, and the container images stored there.

az group delete --name myResourceGroup

后续步骤Next steps

本快速入门介绍了如何使用 Azure CLI 创建 Azure 容器注册表,如何将容器映像推送到注册表,以及如何拉取并运行注册表中的映像。In this quickstart, you created an Azure Container Registry with the Azure CLI, pushed a container image to the registry, and pulled and ran the image from the registry. 请继续阅读 Azure 容器注册表教程,以更深入地了解 ACR。Continue to the Azure Container Registry tutorials for a deeper look at ACR.