快速入门:使用 Azure 容器注册表任务生成和运行容器映像Quickstart: Build and run a container image using Azure Container Registry Tasks

本快速入门介绍如何使用 Azure 容器注册表任务命令在 Azure 本地快速生成、推送和运行 Docker 容器映像,并借此演示如何将“内部循环”开发周期工作卸载到云中。In this quickstart, you use Azure Container Registry Tasks commands to quickly build, push, and run a Docker container image natively within Azure, showing how to offload your "inner-loop" development cycle to the cloud. ACR 任务是 Azure 容器注册表中的一套功能,可在整个容器生命周期内帮助管理和修改容器映像。ACR Tasks is a suite of features within Azure Container Registry to help you manage and modify container images across the container lifecycle.

完成本快速入门后,请探索 ACR 任务的更高级功能。After this quickstart, explore more advanced features of ACR Tasks. ACR 任务可以基于代码提交或基础映像更新自动生成映像,或者同时测试多个容器,此外还支持其他一些方案。ACR Tasks can automate image builds based on code commits or base image updates, or test multiple containers, in parallel, among other scenarios.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

可以使用本地安装的 Azure CLI 来完成此快速入门。You can use a local installation of the Azure CLI to complete this quickstart. 如果想要在本地使用 Azure CLI,建议使用 2.0.58 或更高版本。If you'd like to use it locally, version 2.0.58 or later is recommended. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建资源组Create a resource group

如果还没有容器注册表,请先使用 az group create 命令创建一个资源组。If you don't already have a container registry, first 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

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

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

此示例创建一个基本注册表,这是为了解 Azure 容器注册表的开发人员提供的成本优化选项。This example creates a Basic registry, a cost-optimized option for developers learning about Azure Container Registry. 有关可用服务层级的详细信息,请参阅容器注册表服务层级For details on available service tiers, see Container registry service tiers.

从 Dockerfile 生成和推送映像Build and push image from a Dockerfile

现在,请使用 Azure 容器注册表来生成和推送映像。Now use Azure Container Registry to build and push an image. 首先创建一个工作目录,然后创建一个名为 Dockerfile 的 Dockerfile,其中只有一行内容:FROM hello-worldFirst, create a working directory and then create a Dockerfile named Dockerfile with the single line: FROM hello-world. 这是一个从 Docker Hub 中的 hello-world 映像生成 Linux 容器映像的简单示例。This is a simple example to build a Linux container image from the hello-world image in Docker Hub. 你可以创建自己的标准 Dockerfile 并为其他平台生成映像。You can create your own standard Dockerfile and build images for other platforms. 如果使用 bash shell,请使用以下命令创建 Dockerfile:If you are working at a bash shell, create the Dockerfile with the following command:

echo FROM hello-world > Dockerfile

运行 az acr build 命令,该命令将生成映像,并在成功生成映像后将其推送到注册表。Run the az acr build command, which builds the image and, after the image is successfully built, pushes it to your registry. 以下示例会生成并推送 sample/hello-world:v1 映像。The following example builds and pushes the sample/hello-world:v1 image. 命令末尾处的 . 设置 Dockerfile 的位置(在本例中为当前目录)。The . at the end of the command sets the location of the Dockerfile, in this case the current directory.

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

成功生成并推送后,输出将如下所示:Output from a successful build and push is similar to the following:

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 hello-world
latest: Pulling from library/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

运行映像Run the image

现在,请快速运行已生成并推送到注册表的映像。Now quickly run the image you built and pushed to your registry. 此处使用 az acr run 运行容器命令。Here you use az acr run to run the container command. 在容器开发工作流中,这可能是部署映像之前的验证步骤,或者你可以将该命令包含在多步骤 YAML 文件中。In your container development workflow, this might be a validation step before you deploy the image, or you could include the command in a multi-step YAML file.

以下示例使用 $Registry 指定运行命令的注册表:The following example uses $Registry to specify the registry where you run the command:

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

此示例中的 cmd 参数以其默认配置运行容器,但 cmd 支持附加 docker run 参数甚至其他 docker 命令。The cmd parameter in this example runs the container in its default configuration, but cmd supports additional docker run parameters or even other docker commands.

输出与下面类似:Output is similar to the following:

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

清理资源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 任务功能在 Azure 中以本机方式快速构建、推送和运行 Docker 容器映像,而未本地安装 Docker。In this quickstart, you used features of ACR Tasks to quickly build, push, and run a Docker container image natively within Azure, without a local Docker installation. 请继续学习 Azure 容器注册表任务教程,了解如何使用 Azure 任务来自动生成和更新映像。Continue to the Azure Container Registry Tasks tutorials to learn about using ACR Tasks to automate image builds and updates.