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

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

指定存放庫的名稱。

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

以下是使用 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
        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: |
    

    在上述代碼段中,映像 contosoRepository:tag1contosoRepository:tag2 會建置並推送至對應至 dockerRegistryServiceConnection1dockerRegistryServiceConnection2的容器登錄。

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

    steps:
    - task: Docker@2
      displayName: Build and Push
      inputs:
        command: buildAndPush
        containerRegistry: dockerRegistryServiceConnection1
        repository: contosoRepository
        tags: |
        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 使用時會忽略輸入。

    即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:https://aka.ms/ContentUserFeedback

    提交並檢視相關的意見反應