ACR 任务是 Azure 容器注册表中的一套功能,可在整个容器生命周期内帮助管理和修改容器映像。
本文演示了如何使用 Azure 资源管理器模板来将快速任务运行排入队列,这与使用 az acr build 命令来手动创建任务运行类似。
用于将任务运行排入队列的资源管理器模板在自动化方案中很有用,它扩展了 az acr build
的功能。 例如:
- 使用模板创建容器注册表,并立即将任务运行排入队列,以生成并推送容器映像
- 创建或启用可在快速任务运行中使用的其他资源,例如 Azure 资源的托管标识
- 必须为任务运行指定一个远程上下文(例如 GitHub 存储库)作为源位置。 不能使用本地源上下文。
- 对于使用托管标识的任务运行,只允许使用用户分配的托管标识。
- GitHub 帐户 - 如果没有帐户,请在 https://github.com 上创建一个帐户。
- 创建示例存储库分支 - 对于此处显示的任务示例,请使用 GitHub UI 在 GitHub 帐户中创建以下示例存储库分支: https://github.com/Azure-Samples/acr-build-helloworld-node 。 此存储库包含用于生成小型容器映像的示例 Dockerfile 和源代码。
本示例使用示例模板创建容器注册表,并将生成和推送映像的任务运行排入队列。
对于本示例,请提供以下模板参数的值:
参数 | Value |
---|---|
registryName | 所创建注册表的唯一名称 |
repository | 用于生成任务的目标存储库 |
taskRunName | 用于指定映像标记的任务运行的名称 |
sourceLocation | 生成任务的远程上下文,例如 https://github.com/Azure-Samples/acr-build-helloworld-node 。 存储库根路径中的 Dockerfile 为小型 Node.js Web 应用生成容器映像。 如有需要,可以使用存储库的分支作为生成上下文。 |
使用 az deployment group create 命令部署模板。 本示例将生成 helloworld-node:testrun 映像,并将该映像推送到名为 mycontainerregistry 的注册表中 。
备注
以下示例中使用的 Dockerfile 依赖于 Docker Hub 中的公共基容器映像。 若要在使用公共内容时提高可靠性,请在专用 Azure 容器注册表中导入和管理映像,并将 Dockerfile 更新为使用专门管理的基础映像。 了解有关使用公共映像的详细信息。
az deployment group create \
--resource-group myResourceGroup \
--template-uri https://raw.githubusercontent.com/Azure/acr/master/docs/tasks/run-as-deployment/quickdockerbuild/azuredeploy.json \
--parameters \
registryName=mycontainerregistry \
repository=helloworld-node \
taskRunName=testrun \
sourceLocation=https://github.com/Azure-Samples/acr-build-helloworld-node.git#main
上一条命令在命令行上传递了参数。 如有需要,请将这些参数传递到参数文件中。
部署成功完成后,请运行 az acr repository show-tags,以验证映像是否已生成:
az acr repository show-tags \
--name mycontainerregistry \
--repository helloworld-node --output table
输出:
Result
--------
testrun
要查看有关任务运行的详细信息,请查看运行日志。
首先,使用 az acr task list-runs 获取运行 ID
az acr task list-runs \
--registry mycontainerregistry --output table
输出类似于:
RUN ID TASK PLATFORM STATUS TRIGGER STARTED DURATION
-------- ------ ---------- --------- --------- -------------------- ----------
ca1 linux Succeeded Manual 2020-03-23T17:54:28Z 00:00:48
运行 az acr task logs 以查看运行 ID 的任务运行日志,在本例中为 ca1:
az acr task logs \
--registry mycontainerregistry \
--run-id ca1
输出显示任务运行日志。
还可以在 Azure 门户中查看任务运行日志。
- 导航到容器注册表
- 在“服务”下,选择“任务”>“运行” 。
- 选择运行 ID,在本例中为 ca1。
门户将显示任务运行日志。
使用示例模板将启用用户分配托管标识的任务运行排入队列。 在任务运行期间,会对标识进行身份验证,以从其他 Azure 容器注册表中拉取映像。
此方案与在 ACR 任务中使用 Microsoft 托管标识进行跨注册表身份验证类似。 例如,一个组织可能维护一个集中注册表,其中包含许多基础映像,多个开发团队都会访问这些映像。
出于演示目的,创建一个单独的容器注册表作为基础注册表,然后推送从 Docker Hub 拉取的 Node.js 基础映像。
创建第二个容器注册表(例如 mybaseregistry),用于存储基础映像。
从 Docker Hub 中拉取
node:9-alpine
映像,将其标记为基础映像,然后推送到基础注册表:docker pull node:9-alpine docker tag node:9-alpine mybaseregistry.azurecr.cn/baseimages/node:9-alpine az acr login -n mybaseregistry docker push mybaseregistry.azurecr.cn/baseimages/node:9-alpine
创建从基础注册表中拉取基础映像的 Dockerfile。 在 GitHub 存储库的本地分支中执行以下步骤,例如,https://github.com/myGitHubID/acr-build-helloworld-node.git
。
- 在 GitHub UI 中,选择“创建新文件”。
- 将文件命名为 Dockerfile-test,并粘贴以下内容。 将注册表名称替换为 mybaseregistry。
FROM mybaseregistry.azurecr.cn/baseimages/node:9-alpine COPY . /src RUN cd /src && npm install EXPOSE 80 CMD ["node", "/src/server.js"]
- 选择“提交新文件”。
使用 az identity create 命令在订阅中创建一个名为 myACRTasksId 的标识。 可以使用之前用于创建容器注册表的同一资源组,也可以使用其他资源组。
az identity create \
--resource-group myResourceGroup \
--name myACRTasksId
为了在以下步骤中配置用户分配的标识,请使用 az identity show 命令将标识的资源 ID、主体 ID 和客户端 ID 存储在变量中。
# Get resource ID of the user-assigned identity
resourceID=$(az identity show \
--resource-group myResourceGroup \
--name myACRTasksId \
--query id --output tsv)
# Get principal ID of the task's user-assigned identity
principalID=$(az identity show \
--resource-group myResourceGroup \
--name myACRTasksId \
--query principalId --output tsv)
# Get client ID of the user-assigned identity
clientID=$(az identity show \
--resource-group myResourceGroup \
--name myACRTasksId \
--query clientId --output tsv)
授予托管标识从基础注册表 mybaseregistry 中拉取映像的权限。
使用 az acr show 命令获取基础注册表的资源 ID,并将其存储在变量中:
baseregID=$(az acr show \
--name mybaseregistry \
--query id --output tsv)
使用 az role assignment create 命令向基础注册表分配 Acrpull 角色标识。 此角色仅有权从该注册表提取映像。
az role assignment create \
--assignee $principalID \
--scope $baseregID \
--role acrpull
对于本示例,请提供以下模板参数的值:
参数 | Value |
---|---|
registryName | 在其中生成映像的注册表名称 |
repository | 用于生成任务的目标存储库 |
taskRunName | 用于指定映像标记的任务运行的名称 |
userAssignedIdentity | 在任务中启用的用户分配标识的资源 ID |
customRegistryIdentity | 在任务中启用的用户分配标识的客户端 ID,用于向自定义注册表进行身份验证 |
customRegistry | 在任务中访问的自定义注册表的登录服务器名称,例如 mybaseregistry.azurecr.cn |
sourceLocation | 生成任务的远程上下文,例如 https://github.com/\<your-GitHub-ID>/acr-build-helloworld-node。 |
dockerFilePath | 远程上下文中 Dockerfile 的路径,用于生成映像。 |
使用 az deployment group create 命令部署模板。 本示例将生成 helloworld-node:testrun 映像,并将该映像推送到名为 mycontainerregistry 的注册表中 。 基础映像从 mybaseregistry.azurecr.cn 中拉取。
az deployment group create \
--resource-group myResourceGroup \
--template-uri https://raw.githubusercontent.com/Azure/acr/master/docs/tasks/run-as-deployment/quickdockerbuildwithidentity/azuredeploy.json \
--parameters \
registryName=mycontainerregistry \
repository=helloworld-node \
taskRunName=basetask \
userAssignedIdentity=$resourceID \
customRegistryIdentity=$clientID \
sourceLocation=https://github.com/<your-GitHub-ID>/acr-build-helloworld-node.git#main \
dockerFilePath=Dockerfile-test \
customRegistry=mybaseregistry.azurecr.cn
上一条命令在命令行上传递了参数。 如有需要,请将这些参数传递到参数文件中。
部署成功完成后,请运行 az acr repository show-tags,以验证映像是否已生成:
az acr repository show-tags \
--name mycontainerregistry \
--repository helloworld-node --output table
输出:
Result
--------
basetask
要查看运行日志,请参阅前面部分中的步骤。
- 在 ACR GitHub 存储库中查看更多模板示例。
- 有关模板属性的详细信息,请参阅任务运行和任务的模板参考。