Docker@2 - Docker v2 任务
生成或推送 Docker 映像、登录或注销、启动或停止容器,或运行 Docker 命令。
生成或推送 Docker 映像、登录或注销,或运行 Docker 命令。
语法
# Docker v2
# Build or push Docker images, login or logout, start or stop containers, or run a Docker command.
- task: Docker@2
inputs:
# Container Repository
#containerRegistry: # string. Container registry.
#repository: # string. Optional. Use when command != login && command != logout && command != start && command != stop. Container repository.
# Commands
command: 'buildAndPush' # 'buildAndPush' | 'build' | 'push' | 'login' | 'logout' | 'start' | 'stop'. Required. Command. Default: buildAndPush.
Dockerfile: '**/Dockerfile' # string. Required when command = build || command = buildAndPush. Dockerfile. Default: **/Dockerfile.
#buildContext: '**' # string. Optional. Use when command = build || command = buildAndPush. Build context. Default: **.
#tags: '$(Build.BuildId)' # string. Optional. Use when command = build || command = push || command = buildAndPush. Tags. Default: $(Build.BuildId).
#arguments: # string. Optional. Use when command != login && command != logout && command != buildAndPush. Arguments.
#addPipelineData: true # boolean. Add Pipeline metadata to image(s). Default: true.
#addBaseImageData: true # boolean. Add base image metadata to image(s). Default: true.
#container: # string. Optional. Use when command = start || command = stop. Container.
# Docker v2
# Build or push Docker images, login or logout, start or stop containers, or run a Docker command.
- task: Docker@2
inputs:
# Container Repository
#containerRegistry: # string. Container registry.
#repository: # string. Optional. Use when command != login && command != logout && command != start && command != stop. Container repository.
# Commands
command: 'buildAndPush' # 'buildAndPush' | 'build' | 'push' | 'login' | 'logout' | 'start' | 'stop'. Required. Command. Default: buildAndPush.
Dockerfile: '**/Dockerfile' # string. Required when command = build || command = buildAndPush. Dockerfile. Default: **/Dockerfile.
#buildContext: '**' # string. Optional. Use when command = build || command = buildAndPush. Build context. Default: **.
#tags: '$(Build.BuildId)' # string. Optional. Use when command = build || command = push || command = buildAndPush. Tags. Default: $(Build.BuildId).
#arguments: # string. Optional. Use when command != login && command != logout && command != buildAndPush. Arguments.
#addPipelineData: true # boolean. Add Pipeline metadata to image(s). Default: true.
#container: # string. Optional. Use when command = start || command = stop. Container.
# Docker v2
# Build or push Docker images, login or logout, or run a Docker command.
- task: Docker@2
inputs:
# Container Repository
#containerRegistry: # string. Container registry.
#repository: # string. Optional. Use when command != login && command != logout. Container repository.
# Commands
command: 'buildAndPush' # 'buildAndPush' | 'build' | 'push' | 'login' | 'logout'. Required. Command. Default: buildAndPush.
Dockerfile: '**/Dockerfile' # string. Required when command = build || command = buildAndPush. Dockerfile. Default: **/Dockerfile.
#buildContext: '**' # string. Optional. Use when command = build || command = buildAndPush. Build context. Default: **.
#tags: '$(Build.BuildId)' # string. Optional. Use when command = build || command = push || command = buildAndPush. Tags. Default: $(Build.BuildId).
#arguments: # string. Optional. Use when command != login && command != logout && command != buildAndPush. Arguments.
#addPipelineData: true # boolean. Add Pipeline metadata to image(s). Default: true.
输入
repository
-
容器存储库
string
。 可选。 当 command != login && command != logout && command != start && command != stop
时使用。
指定存储库的名称。
repository
-
容器存储库
string
。 可选。 当 command != login && command != logout
时使用。
指定存储库的名称。
command
-
命令
string
。 必填。 允许的值:buildAndPush
、build
、push
、login
、logout
、start
、stop
。 默认值:buildAndPush
。
指定要运行的 Docker 命令。
command
-
命令
string
。 必填。 允许的值:buildAndPush
、build
、push
、login
、logout
。 默认值:buildAndPush
。
指定要运行的 Docker 命令。
Dockerfile
-
Dockerfile
string
。
command = build || command = buildAndPush
时是必需的。 默认值:**/Dockerfile
。
指定 Docker 文件的路径。 该任务使用它找到的第一个 Docker 文件来生成映像。
buildContext
-
生成上下文
string
。 可选。 当 command = build || command = buildAndPush
时使用。 默认值:**
。
指定生成上下文的路径。 传递 **
以指示包含 Docker 文件的目录。
tags
-
标记
string
。 可选。 当 command = build || command = push || command = buildAndPush
时使用。 默认值:$(Build.BuildId)
。
指定逗号分隔标记的列表。 这些标记用于 build
、push
和 buildAndPush
命令。
arguments
-
参数
string
。 可选。 当 command != login && command != logout && command != buildAndPush
时使用。
指定要传递给 Docker 客户端的其他参数。 如果使用命令参数的值 buildAndPush
,则忽略参数属性。
示例:使用生成命令 --build-arg HTTP_PROXY=http://10.20.30.2:1234 --quiet
。
addPipelineData
-
向图像添加管道元数据
boolean
。 默认值:true
。
默认情况下,添加源分支名称或生成 ID 等管道数据,并帮助进行可跟踪。 例如,可以检查映像,找出生成映像的管道。 可以选择退出此默认行为。
addBaseImageData
-
向图像添加基础映像元数据
boolean
。 默认值:true
。
默认情况下,添加基础映像名称或摘要等基础映像数据,并帮助进行可跟踪。 可以选择退出此默认行为。
container
-
容器
string
。 可选。 当 command = start || command = stop
时使用。
指定要启动或停止的容器资源的名称。 将此命令用于 start
和 stop
命令。
任务控制选项
除任务输入之外,所有任务都具有控制选项。 有关详细信息,请参阅 控件选项和常见任务属性。
输出变量
此任务定义以下 输出变量,可以在下游步骤、作业和阶段中使用。
DockerOutput
指定包含命令输出的文件的路径。 可以在 buildAndPush
命令的单独行上列出两个文件路径,以及任何其他命令的一个文件路径。
注解
以下是使用 Docker 任务而不是直接在脚本中使用 Docker 客户端二进制文件的主要优势。
与 Docker 注册表服务连接 集成 - 该任务使使用 Docker 注册表服务连接轻松连接到任何容器注册表。 登录后,可以通过利用 Docker 任务使用的登录来添加执行其他任务或脚本的后续任务。 例如,使用 Docker 任务登录到任何 Azure 容器注册表,然后使用另一个任务或脚本生成映像并将其推送到注册表。
添加为标签的元数据 - 任务将可跟踪性相关的元数据添加到以下标签中的图像 -
- com.azure.dev.image.build.buildnumber
- com.azure.dev.image.build.builduri
- com.azure.dev.image.build.definitionname
- com.azure.dev.image.build.repository.name
- com.azure.dev.image.build.repository.uri
- com.azure.dev.image.build.sourcebranchname
- com.azure.dev.image.build.sourceversion
- com.azure.dev.image.release.definitionname
- com.azure.dev.image.release.releaseid
- com.azure.dev.image.release.releaseweburl
- com.azure.dev.image.system.teamfoundationcollectionuri
- com.azure.dev.image.system.teamproject
故障排除
为什么 Docker 任务会忽略传递给 buildAndPush 命令的参数?
使用 buildAndPush
命令配置的 Docker 任务会忽略传递的参数,因为它们与内部生成和推送命令不明确。 可以将命令拆分为单独的构建和推送步骤,并传递合适的参数。 有关示例,请参阅此 StackOverflow 文章。
DockerV2 仅支持 Docker 注册表服务连接,不支持 ARM 服务连接。 如何在 Docker 任务中使用现有的 Azure 服务主体(SPN)进行身份验证?
可以使用 Azure SPN 凭据创建 Docker 注册表服务连接。 从注册表类型中选择其他人,并提供详细信息,如下所示:
Docker Registry: Your container registry URL (eg. https://myacr.azurecr.io)
Docker ID: Service principal client ID
Password: Service principal key
例子
登录
以下 YAML 代码片段显示了使用 Docker 注册表服务连接进行的容器注册表登录。
- task: Docker@2
displayName: Login to ACR
inputs:
command: login
containerRegistry: dockerRegistryServiceConnection1
生成和推送
一个名为 buildAndPush
的便捷命令允许在单个命令中生成映像并将其推送到容器注册表。
以下 YAML 代码片段是生成映像的多个标记并将其推送到多个注册表的示例。
steps:
- task: Docker@2
displayName: Login to ACR
inputs:
command: login
containerRegistry: dockerRegistryServiceConnection1
- task: Docker@2
displayName: Login to Docker Hub
inputs:
command: login
containerRegistry: dockerRegistryServiceConnection2
- task: Docker@2
displayName: Build and Push
inputs:
command: buildAndPush
repository: contosoRepository # username/contosoRepository for DockerHub
tags: |
tag1
tag2
在上面的代码片段中,生成映像 contosoRepository:tag1
和 contosoRepository:tag2
并将其推送到对应于 dockerRegistryServiceConnection1
和 dockerRegistryServiceConnection2
的容器注册表。
如果要生成并推送到特定经过身份验证的容器注册表,而不是一次性生成并推送到所有经过身份验证的容器注册表,请显式指定具有 command: buildAndPush
的 containerRegistry
输入,如下所示:
steps:
- task: Docker@2
displayName: Build and Push
inputs:
command: buildAndPush
containerRegistry: dockerRegistryServiceConnection1
repository: contosoRepository
tags: |
tag1
tag2
Logout
以下 YAML 代码片段演示如何使用 Docker 注册表服务连接从容器注册表注销。
- task: Docker@2
displayName: Logout of ACR
inputs:
command: logout
containerRegistry: dockerRegistryServiceConnection1
启动/停止
使用此任务控制作业和服务容器。 这种用法并不常见,但偶尔在独特的情况下使用。
resources:
containers:
- container: builder
image: ubuntu:18.04
steps:
- script: echo "I can run inside the container (it starts by default)"
target:
container: builder
- task: Docker@2
inputs:
command: stop
container: builder
# any task beyond this point would not be able to target the builder container
# because it's been stopped
其他命令和参数
命令和参数输入用于使用 Docker 客户端二进制文件传递生成或推送命令的其他参数,如示例中所示。
steps:
- task: Docker@2
displayName: Login to ACR
inputs:
command: login
containerRegistry: dockerRegistryServiceConnection1
- task: Docker@2
displayName: Build
inputs:
command: build
repository: contosoRepository # username/contosoRepository for DockerHub
tags: tag1
arguments: --secret id=mysecret,src=mysecret.txt
注释
参数输入针对除 buildAndPush
以外的所有命令求值。
buildAndPush
是一个便捷命令(build
后跟 push
),arguments
输入在使用时将被忽略。