Compartilhar via

快速入门:使用Azure Container Registry任务生成和运行容器映像

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

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

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

先决条件

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

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

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

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

创建资源组

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

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

注意

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

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

az group create --name myResourceGroup --location chinaeast2

创建容器注册表

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

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

此示例创建一个 Basic 注册表,这是一个成本优化选项,适用于了解Azure Container Registry的开发人员。 有关可用服务层级的详细信息,请参阅 Azure Container Registry SKU 功能和限制

从 Dockerfile 生成和推送映像

现在使用Azure Container Registry生成和推送映像。 首先,创建本地工作目录。 然后使用单行创建名为 Dockerfile 的 DockerfileFROM mcr.microsoft.com/hello-world 此行是一个简单的示例,它从托管在 Azure Container Registry 的 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 . 

注意

如果使用 启用了 ABAC 的源注册表,则必须将调用方标识传递为 build 用于向注册表进行身份验证的标识。 在运行--source-acr-auth-id [caller]时使用az acr build标志验证身份。

有关详细信息,请参阅 在 ACR 任务、快速任务、快速生成和快速运行上启用 ABAC 的效果

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

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...
2023/03/18 21:56:57 Using acb_vol_4c7ffa31-c862-4be3-xxxx-ab8e615c55c4 as the home volume
2023/03/18 21:56:57 Setting up Docker configuration...
2023/03/18 21:56:58 Successfully set up Docker configuration
2023/03/18 21:56:58 Logging in to registry: mycontainerregistry008.azurecr.cn
2023/03/18 21:56:59 Successfully logged into mycontainerregistry008.azurecr.cn
2023/03/18 21:56:59 Executing step ID: build. Working directory: '', Network: ''
2023/03/18 21:56:59 Obtaining source code and scanning for dependencies...
2023/03/18 21:57:00 Successfully obtained source code and scanned for dependencies
2023/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
2023/03/18 21:57:01 Successfully executed container: build
2023/03/18 21:57:01 Executing step ID: push. Working directory: '', Network: ''
2023/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
2023/03/18 21:57:03 Successfully pushed image: mycontainerregistry008.azurecr.cn/sample/hello-world:v1
2023/03/18 21:57:03 Step ID: build marked as successful (elapsed time in seconds: 2.543040)
2023/03/18 21:57:03 Populating digests for step ID: build...
2023/03/18 21:57:05 Successfully populated digests for step ID: build
2023/03/18 21:57:05 Step ID: push marked as successful (elapsed time in seconds: 1.473581)
2023/03/18 21:57:05 The following dependencies were found:
2023/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 运行 container 命令。 在容器开发工作流中,在部署映像之前,此步骤可能是验证步骤,也可以在 多步骤 YAML 文件中包括该命令。

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

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

注意

如果您使用启用了 ABAC 的源注册表,则必须将调用方的标识传递给 命令,以用于向注册表进行身份验证。 在运行--source-acr-auth-id [caller]时使用az acr run标志验证身份。

有关详细信息,请参阅 在 ACR 任务、快速任务、快速生成和快速运行上启用 ABAC 的效果

此示例中的 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...
2023/03/19 19:01:53 Using acb_vol_60e9a538-b466-475f-9565-80c5b93eaa15 as the home volume
2023/03/19 19:01:53 Creating Docker network: acb_default_network, driver: 'bridge'
2023/03/19 19:01:53 Successfully set up Docker network: acb_default_network
2023/03/19 19:01:53 Setting up Docker configuration...
2023/03/19 19:01:54 Successfully set up Docker configuration
2023/03/19 19:01:54 Logging in to registry: mycontainerregistry008.azurecr.cn
2023/03/19 19:01:55 Successfully logged into mycontainerregistry008.azurecr.cn
2023/03/19 19:01:55 Executing step ID: acb_step_0. Working directory: '', Network: 'acb_default_network'
2023/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/

2023/03/19 19:01:56 Successfully executed container: acb_step_0
2023/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

后续步骤

在本快速入门中,你使用 ACR 任务的功能快速生成、推送和运行 Azure 中的 Docker 容器映像,而无需安装本地 Docker。 若要了解如何使用 ACR 任务自动执行映像生成和更新,请参阅Azure Container Registry任务教程。