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

Azure 容器注册表是 Azure 中的专用 Docker 注册表,你可在其中存储和管理专用 Docker 容器映像。An Azure container registry is a private Docker registry in Azure where you can store and manage your private Docker container images. 在本快速入门教程中,你会使用 Azure 门户创建容器注册表。In this quickstart, you create a container registry with the Azure portal. 然后,使用 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 或更高版本)。To log in to the registry to work with container images, 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 提供的包可在任何 MacWindowsLinux 系统上轻松配置 Docker。Docker provides packages that easily configure Docker on any Mac, Windows, or Linux system.

登录 AzureSign in to Azure

通过 https://portal.azure.cn 登录到 Azure 门户。Sign in to the Azure portal at https://portal.azure.cn.

创建容器注册表Create a container registry

选择“创建资源” ,在“新建” 选项卡的搜索筛选器中键入“容器注册表” ,然后单击回车键。Select Create a resource, type the Container Registry in search filter of NEW tab, and click the return key.

在 Azure 门户中创建容器注册表

在搜索结果中选择“容器注册表” 项。Select the item of Container Registry in search result.

在 Azure 门户中创建容器注册表

选择“创建” 。Select Create.

在 Azure 门户中创建容器注册表

在“注册表名称”和“资源组”中输入值 。Enter values for Registry name and Resource group. 注册表名称在 Azure 中必须唯一,并且包含 5-50 个字母数字字符。The registry name must be unique within Azure, and contain 5-50 alphanumeric characters. 对于本快速入门,在 China North 位置创建名为 myResourceGroup 的新资源组,对于 SKU,选择“基本”。For this quickstart create a new resource group in the China North location named myResourceGroup, and for SKU, select 'Basic'. 选择“创建”,部署 ACR 实例 。Select Create to deploy the ACR instance.

在 Azure 门户中创建容器注册表

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

显示“部署成功”消息时,请在门户中选择容器注册表 。When the Deployment succeeded message appears, select the container registry in the portal.

Azure 门户中的容器注册表概述

记下“登录服务器”的值。 Take note of the value of the Login server. 结合 Azure CLI 和 Docker 使用注册表时,请在以下步骤中使用此值。You use this value in the following steps while working with your registry with the Azure CLI and Docker.

登录到注册表Log in to registry

在推送和拉取容器映像之前,必须登录到 ACR 实例。Before pushing and pulling container images, you must log in to the ACR instance. 在操作系统中打开命令外壳,然后在 Azure CLI 中使用 az acr login 命令。Open a command shell in your operating system, and use the az acr login command in the Azure CLI.

az acr login --name <acrName>

该命令在完成后返回 Login SucceededThe command returns Login Succeeded 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 中心拉取现有映像。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

若要列出注册表中的映像,请在门户中导航到注册表并选择“存储库”,然后选择使用 docker push 创建的存储库 。To list the images in your registry, navigate to your registry in the portal and select Repositories, then select the repository you created with docker push.

在本示例中,选择 hello-world 存储库,并可在“标记”下看到 v1 标记的映像 。In this example, we select the hello-world repository, and we can see the v1-tagged image under TAGS.

在 Azure 门户中列出容器映像

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

现在,可以使用 docker run 从容器注册表拉取并运行 hello-world:v1 容器映像: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

若要清理资源,请在门户中导航到 myResourceGroup 资源组。To clean up your resources, navigate to the myResourceGroup resource group in the portal. 加载该资源组后,单击“删除资源组”以删除该资源组、容器注册表以及其中存储的容器映像。 Once the resource group is loaded click on Delete resource group to remove the resource group, the container registry, and the container images stored there.

在 Azure 门户中删除资源组

后续步骤Next steps

本快速入门介绍了如何使用 Azure 门户创建 Azure 容器注册表、推送容器映像,以及提取和运行注册表中的映像。In this quickstart, you created an Azure Container Registry with the Azure portal, pushed a container image, and pulled and ran the image from the registry.