快速入门:使用 Azure 容器注册表任务生成和运行容器映像

在本快速入门中,你将使用 Azure 容器注册表任务命令在 Azure 中以本机方式快速生成、推送和运行 Docker 容器映像,而无需本地安装 Docker。 ACR 任务是 Azure 容器注册表中的一套功能,可在整个容器生命周期内帮助管理和修改容器映像。 此示例说明如何使用本地 Dockerfile 通过按需生成将“内部循环”容器映像开发周期转移到云中。

完成本快速入门后,请使用教程探索 ACR 任务的更多高级功能。 ACR 任务可以基于代码提交或基础映像更新自动生成映像,或者同时测试多个容器,此外还支持其他一些方案。

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

先决条件

  • 如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

  • 本快速入门需要 Azure CLI 2.0.58 或更高版本。

    注意

    在可以在由世纪互联运营的 Microsoft Azure 中使用 Azure CLI 之前,请先运行 az cloud set -n AzureChinaCloud 来更改云环境。 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloud

创建资源组

如果还没有容器注册表,请先使用 az group create 命令创建一个资源组。 Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。

以下示例在“chinaeast2”位置创建名为“myResourceGroup”的资源组。

注意

必须使用 az acr build 功能来完成本教程的其余部分,目前只有 China East 2 区域支持 Azure 中国世纪互联上的 az acr build 功能。

请注意,在后续步骤中尝试调用 az acr build 之前,请先提前在 China East 2 区域中创建容器注册表。

az group create --name myResourceGroup --location chinaeast2

创建容器注册表

使用 az acr create 命令创建容器注册表。 注册表名称在 Azure 中必须唯一,并且包含 5-50 个字母数字字符。 以下示例使用 mycontainerregistry008。 将其更新为唯一值。

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

此示例创建一个基本注册表,这是为了解 Azure 容器注册表的开发人员提供的成本优化选项。 有关可用服务层级的详细信息,请参阅容器注册表服务层级

从 Dockerfile 生成和推送映像

现在,请使用 Azure 容器注册表来生成和推送映像。 首先创建一个本地工作目录,然后创建一个名为“Dockerfile”的 Dockerfile,其中只有一行内容:FROM mcr.microsoft.com/hello-world。 这是一个从 Azure 容器注册表中托管的 hello-world 映像生成 Linux 容器映像的简单示例。 你可以创建自己的标准 Dockerfile 并为其他平台生成映像。 如果使用 bash shell,请使用以下命令创建 Dockerfile:

echo "FROM mcr.microsoft.com/hello-world" > Dockerfile

运行 az acr build 命令,该命令将生成映像,并在成功生成映像后将其推送到注册表。 以下示例会生成并推送 sample/hello-world:v1 映像。 命令末尾处的 . 设置 Dockerfile 的位置(在本例中为当前目录)。

az acr build --image sample/hello-world:v1 \
  --registry mycontainerregistry008 \
  --file Dockerfile . 

成功生成并推送后,输出将如下所示:

Packing source code into tar to upload...
Uploading archived source code from '/tmp/build_archive_b0bc1e5d361b44f0833xxxx41b78c24e.tar.gz'...
Sending context (1.856 KiB) to registry: mycontainerregistry008...
Queued a build with ID: ca8
Waiting for agent...
2019/03/18 21:56:57 Using acb_vol_4c7ffa31-c862-4be3-xxxx-ab8e615c55c4 as the home volume
2019/03/18 21:56:57 Setting up Docker configuration...
2019/03/18 21:56:58 Successfully set up Docker configuration
2019/03/18 21:56:58 Logging in to registry: mycontainerregistry008.azurecr.cn
2019/03/18 21:56:59 Successfully logged into mycontainerregistry008.azurecr.cn
2019/03/18 21:56:59 Executing step ID: build. Working directory: '', Network: ''
2019/03/18 21:56:59 Obtaining source code and scanning for dependencies...
2019/03/18 21:57:00 Successfully obtained source code and scanned for dependencies
2019/03/18 21:57:00 Launching container with name: build
Sending build context to Docker daemon  13.82kB
Step 1/1 : FROM mcr.microsoft.com/hello-world
latest: Pulling from hello-world
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586fxxxx21577a99efb77324b0fe535
Successfully built fce289e99eb9
Successfully tagged mycontainerregistry008.azurecr.cn/sample/hello-world:v1
2019/03/18 21:57:01 Successfully executed container: build
2019/03/18 21:57:01 Executing step ID: push. Working directory: '', Network: ''
2019/03/18 21:57:01 Pushing image: mycontainerregistry008.azurecr.cn/sample/hello-world:v1, attempt 1
The push refers to repository [mycontainerregistry008.azurecr.cn/sample/hello-world]
af0b15c8625b: Preparing
af0b15c8625b: Layer already exists
v1: digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a size: 524
2019/03/18 21:57:03 Successfully pushed image: mycontainerregistry008.azurecr.cn/sample/hello-world:v1
2019/03/18 21:57:03 Step ID: build marked as successful (elapsed time in seconds: 2.543040)
2019/03/18 21:57:03 Populating digests for step ID: build...
2019/03/18 21:57:05 Successfully populated digests for step ID: build
2019/03/18 21:57:05 Step ID: push marked as successful (elapsed time in seconds: 1.473581)
2019/03/18 21:57:05 The following dependencies were found:
2019/03/18 21:57:05
- image:
    registry: mycontainerregistry008.azurecr.cn
    repository: sample/hello-world
    tag: v1
    digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
  runtime-dependency:
    registry: registry.hub.docker.com
    repository: library/hello-world
    tag: v1
    digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
  git: {}

Run ID: ca8 was successful after 10s

运行映像

现在,请快速运行已生成并推送到注册表的映像。 此处使用 az acr run 运行容器命令。 在容器开发工作流中,这可能是部署映像之前的验证步骤,或者你可以将该命令包含在多步骤 YAML 文件中。

以下示例使用 $Registry 指定在其中运行命令的注册表的终结点:

az acr run --registry mycontainerregistry008 \
  --cmd '$Registry/sample/hello-world:v1' /dev/null

此示例中的 cmd 参数以其默认配置运行容器,但 cmd 支持附加 docker run 参数甚至其他 docker 命令。

输出与下面类似:

Packing source code into tar to upload...
Uploading archived source code from '/tmp/run_archive_ebf74da7fcb04683867b129e2ccad5e1.tar.gz'...
Sending context (1.855 KiB) to registry: mycontainerre...
Queued a run with ID: cab
Waiting for an agent...
2019/03/19 19:01:53 Using acb_vol_60e9a538-b466-475f-9565-80c5b93eaa15 as the home volume
2019/03/19 19:01:53 Creating Docker network: acb_default_network, driver: 'bridge'
2019/03/19 19:01:53 Successfully set up Docker network: acb_default_network
2019/03/19 19:01:53 Setting up Docker configuration...
2019/03/19 19:01:54 Successfully set up Docker configuration
2019/03/19 19:01:54 Logging in to registry: mycontainerregistry008.azurecr.cn
2019/03/19 19:01:55 Successfully logged into mycontainerregistry008.azurecr.cn
2019/03/19 19:01:55 Executing step ID: acb_step_0. Working directory: '', Network: 'acb_default_network'
2019/03/19 19:01:55 Launching container with name: acb_step_0

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

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

2019/03/19 19:01:56 Successfully executed container: acb_step_0
2019/03/19 19:01:56 Step ID: acb_step_0 marked as successful (elapsed time in seconds: 0.843801)

Run ID: cab was successful after 6s

清理资源

如果不再需要存储在该处的资源组、容器注册表和容器映像,可以使用 az group delete 命令将其删除。

az group delete --name myResourceGroup

后续步骤

在本快速入门中,你使用了 Azure 任务功能在 Azure 中以本机方式快速构建、推送和运行 Docker 容器映像,而未本地安装 Docker。 请继续学习 Azure 容器注册表任务教程,了解如何使用 Azure 任务来自动生成和更新映像。