Docker@2 - Docker v2 工作

建置或推送 Docker 映射、登入或註銷、啟動或停止容器,或執行 Docker 命令。

建置或推送 Docker 映射、登入或註銷,或執行 Docker 命令。

Syntax

# 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.
# 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.

輸入

containerRegistry - 容器登錄
string.

Docker 登錄服務連線的名稱。 執行登錄驗證之命令的必要專案。


repository - 容器存放庫
string. 選擇性。 使用時機 command != login && command != logout && command != start && command != stop

指定存放庫的名稱。


repository - 容器存放庫
string. 選擇性。 使用時機 command != login && command != logout

指定存放庫的名稱。


command - 命令
string. 必要。 允許的值:buildAndPush、、、buildpushloginlogoutstartstop。 預設值:buildAndPush

指定要執行的 Docker 命令。


command - 命令
string. 必要。 允許的值:buildAndPush、、、buildpushloginlogout 預設值: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)

指定逗號分隔標記的清單。 這些標記用於 buildpushbuildAndPush 命令。


arguments - 參數
string. 選擇性。 使用時機 command != login && command != logout && command != buildAndPush

指定要傳遞至 Docker 用戶端的其他自變數。 如果使用命令參數的值 buildAndPush ,則會忽略arguments屬性。

範例:使用建置命令 --build-arg HTTP_PROXY=http://10.20.30.2:1234 --quiet


addPipelineData - 將管線元數據新增至映像 ()
boolean. 預設值:true

根據預設,會新增來源分支名稱或組建標識符等管線數據,並協助追蹤。 例如,您可以檢查映射,以找出哪一個管線建置映射。 您可以退出宣告此預設行為。


addBaseImageData - 將基底映像元數據新增至映像 ()
boolean. 預設值:true

根據預設,會新增基底映射名稱或摘要等基底映像數據,並協助追蹤。 您可以退出宣告此預設行為。


container - 容器
string. 選擇性。 使用時機 command = start || command = stop

指定要啟動或停止的容器資源名稱。 搭配和 stop 命令使用此命令start


工作控制選項

除了工作輸入之外,所有工作都有控制選項。 如需詳細資訊,請參閱 控件選項和一般工作屬性

輸出變數

此工作會定義下列 輸出變數,您可以在下游步驟、作業和階段中取用這些變數。

DockerOutput
指定包含命令輸出的檔案路徑。 您可以在命令的個別行 buildAndPush 上列出兩個檔案路徑,以及任何其他命令的一個檔案路徑。

備註

以下是使用 Docker 工作的主要優點,而不是直接在腳本中使用 Docker 用戶端二進位檔。

  • 與 Docker 登錄服務連線整合 - 工作可讓您輕鬆地使用 Docker 登錄服務連線來連線到任何容器登錄。 登入之後,您可以利用 Docker 工作所使用的登入,新增執行其他工作或腳本的後續工作。 例如,使用 Docker 工作登入任何 Azure Container Registry,然後使用另一個工作或腳本來建置映射,並將映射推送至登錄。

  • 新增為標籤的元資料 - 工作會將追蹤相關元資料新增至下列標籤中的影像 -

    • 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:tag1contosoRepository:tag2 會建置並推送至對應至 dockerRegistryServiceConnection1dockerRegistryServiceConnection2的容器登錄。

如果您想要建置並推送至特定的已驗證容器登錄,而不是一次建置並推送至所有已驗證的容器登錄,請明確指定 containerRegistry 輸入, command: buildAndPush 如下所示:

steps:
- task: Docker@2
  displayName: Build and Push
  inputs:
    command: buildAndPush
    containerRegistry: dockerRegistryServiceConnection1
    repository: contosoRepository
    tags: |
      tag1
      tag2

登出

下列 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 使用時會忽略輸入。

規格需求

需求 描述
管線類型 YAML、傳統組建、傳統版本
執行於 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 任何
Settable 變數 任何
代理程式版本 2.172.0 或更新版本
工作類別 建置
需求 描述
管線類型 YAML、傳統組建、傳統版本
執行於 Agent、DeploymentGroup
要求
Capabilities 此工作不符合作業中後續工作的任何需求。
命令限制 任何
Settable 變數 任何
代理程式版本 所有支援的代理程式版本。
工作類別 建置