How to Enable Docker layer caching in Azure DevOps

后端 未结 3 935
日久生厌
日久生厌 2021-02-09 14:20

I am running the below yaml script to build docker images and push into kubernetes cluster but at the same time I wanted to enable docker layer caching in the azure DevOps while

3条回答
  •  无人共我
    2021-02-09 15:13

    Here's how I fixed this. I just pull the latest version of the image from my registry (Azure Container Registry in my case) to the Azure DevOps hosted agent. Then I add --cache-from to the Docker build arguments pointing to this latest tag which it just downloaded to the local machine/cache.

    - task: Docker@2
      inputs:
        containerRegistry: '$(ContainerRegistryName)'
        command: 'login'
    
    - script: "docker pull $(ACR_ADDRESS)/$(REPOSITORY):latest"
      displayName: Pull latest for layer caching
      continueOnError: true # for first build, no cache
    
    - task: Docker@2
      displayName: build
      inputs:
        containerRegistry: '$(ContainerRegistryName)'
        repository: '$(REPOSITORY)'
        command: 'build'
        Dockerfile: './dockerfile '
        buildContext: '$(BUILDCONTEXT)'
        arguments: '--cache-from=$(ACR_ADDRESS)/$(REPOSITORY):latest' 
        tags: |
          $(Build.BuildNumber)
          latest
    
    - task: Docker@2
      displayName: "push"
      inputs:
        command: push
        containerRegistry: "$(ContainerRegistryName)"
        repository: $(REPOSITORY) 
        tags: |
          $(Build.BuildNumber)
          latest
    

提交回复
热议问题