快速入门:通过 Azure 门户创建 Azure 容器注册表

Azure 容器注册表是用于生成、存储和管理容器映像和相关项目的专用注册表服务。 在本快速入门中,你将使用 Azure 门户创建一个 Azure 容器注册表实例。 然后,使用 Docker 命令将容器映像推送到注册表中,最终从注册表提取并运行该映像。

若要登录到注册表以使用容器映像,本快速入门要求运行 Azure CLI(建议使用 2.0.55 或更高版本)。 运行 az --version 即可查找版本。 如果需要进行安装或升级,请参阅安装 Azure CLI

还必须在本地安装 Docker,同时运行守护程序。 Docker 提供的包可在任何 MacWindowsLinux 系统上轻松配置 Docker。

登录 Azure

登录 Azure 门户

创建容器注册表

选择“创建资源”,在“创建资源”页的搜索筛选器中键入“容器注册表”,然后按回车键。

Navigate to container registry in portal

在搜索结果中选择“容器注册表”项。

Selecting container registry object in the Azure portal

选择创建

Creating a container registry in the Azure portal

在“基本信息”选项卡中,输入“资源组”和“注册表名称”的值 。 注册表名称在 Azure 中必须唯一,并且包含 5-50 个字母数字字符。 对于本快速入门,在 China East 2 位置创建名为 myResourceGroup 的新资源组,对于 SKU,选择“基本”。

Create container registry in the portal

对于剩余的设置,请接受默认值。 然后选择“查看 + 创建”。 查看设置后,选择“创建”。

提示

本快速入门将创建一个“基本”注册表。该注册表已针对成本进行优化,是可供开发人员了解 Azure 容器注册表的选项。 选择其他层,以获得更高的存储和映像吞吐量,以及使用专用终结点的连接等功能。 有关可用服务层级 (SKU) 的详细信息,请参阅容器注册表服务层级

显示“部署成功”消息时,请在门户中选择容器注册表。

Container registry Overview in the portal

记下登录服务器的注册表名称和值,这是在 Azure 云中以 azurecr.cn 结尾的完全限定名称。 使用 Docker 推送和拉取映像时,请在以下步骤中使用这些值。

登录到注册表

必须登录到注册表实例才可推送和拉取容器映像。 在本地计算机上登录到 Azure CLI,然后运行 az acr login 命令。 使用 Azure CLI 登录时仅指定注册表资源名称。 请勿使用完全限定的登录服务器名称。

az acr login --name <registry-name>

示例:

az acr login --name mycontainerregistry

该命令在完成后返回 Login Succeeded

将映像推送到注册表

要将映像推送到 Azure 容器注册表,首先必须具有一个映像。 如果还没有任何本地容器映像,请运行以下 docker pull 命令,拉取现有公共映像。 例如,从 Microsoft Container Registry 中拉取 hello-world 映像。

docker pull mcr.microsoft.com/hello-world

将映像推送到注册表之前,必须使用注册表登录服务器的完全限定的名称进行标记。 登录服务器名称采用 <registry-name>.azurecr.cn(必须全小写)格式,例如 mycontainerregistry.azurecr.cn。

使用 docker tag 命令标记映像。 使用 ACR 实例的登录服务器名称替换 <login-server>

docker tag mcr.microsoft.com/hello-world <login-server>/hello-world:v1

示例:

docker tag mcr.microsoft.com/hello-world mycontainerregistry.azurecr.cn/hello-world:v1

最后,使用 docker push 将映像推送到注册表实例。 使用注册表实例的登录服务器名称替换 <login-server>。 此示例创建 hello-world 存储库,其中包含 hello-world:v1 映像。

docker push <login-server>/hello-world:v1

将映像推送到容器注册表后,请从本地 Docker 环境中删除 hello-world:v1 映像。 (请注意,此 docker rmi 命令不从 Azure 容器注册表中的 hello-world 存储库删除该映像。)

docker rmi <login-server>/hello-world:v1

列出容器映像

若要列出注册表中的映像,请在门户中导航到注册表并选择“存储库”,然后选择使用 docker push 创建的 hello-world 存储库。

List container images in the portal

通过选择 hello-world 存储库,可以在“标记”下看到 v1 标记的映像 。

从注册表运行映像

现在,可以使用 docker run 从容器注册表拉取并运行 hello-world:v1 容器映像:

docker run <login-server>/hello-world:v1  

示例输出:

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

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

[...]

清理资源

若要清理资源,请在门户中导航到 myResourceGroup 资源组。 加载该资源组后,单击“删除资源组”,删除该资源组、容器注册表以及其中存储的容器映像。

Delete resource group in the portal

后续步骤

本快速入门介绍了如何使用 Azure 门户创建 Azure 容器注册表、推送容器映像,以及提取和运行注册表中的映像。 请继续阅读 Azure 容器注册表教程,以更深入地了解 ACR。