How to enable Docker Layer Caching with 1.0


#1

I would like to share several snippets on how to take advantage of Docker Layer Caching with Drone CI. Hopefully this will be useful for others.

Docker Hub

---
kind: pipeline
name: build-example-dockerhub
globals:
  - &docker_creds
    username:
      from_secret: docker_username
    password:
      from_secret: docker_password

steps:
  - name: prepare
    image: busybox
    commands:
      - mkdir -p /cache/${DRONE_REPO}/docker
    volumes:
      - name: cache
        path: /cache

  - name: package-dockerhub
    image: plugins/docker
    settings:
      tags: "${DRONE_BRANCH//\\//-}${DRONE_TAG//\\//-}"
      repo: foo/bar
      create_repository: true
      use_cache: true
      <<: *docker_creds
    volumes:
      - name: docker
        path: /var/lib/docker

volumes:
  - name: cache
    host:
      path: /var/cache
  - name: docker
    host:
      path: /var/cache/${DRONE_REPO}/docker

AWS ECR

---
kind: pipeline
name: build-example-ecr

globals:
  - &registry
      999999999999.dkr.ecr.us-east-1.amazonaws.com
  - &aws_creds
    access_key:
      from_secret: aws_access_key_id
    secret_key:
      from_secret: aws_secret_access_key
    region:
      from_secret: aws_default_region

steps:
  - name: prepare
    image: busybox
    commands:
      - mkdir -p /cache/${DRONE_REPO}/docker
    volumes:
      - name: cache
        path: /cache

  - name: package-ecr
    image: plugins/ecr
    settings:
      tags: "${DRONE_BRANCH//\\//-}${DRONE_TAG//\\//-}"
      repo: 999999999999.dkr.ecr.us-east-1.amazonaws.com/bar
      registry: *registry
      create_repository: true
      use_cache: true
      <<: *aws_creds
    volumes:
      - name: docker
        path: /var/lib/docker

volumes:
  - name: cache
    host:
      path: /var/cache
  - name: docker
    host:
      path: /var/cache/${DRONE_REPO}/docker

More complex
The snippet below shows how to:

  • use package-specific Docker Layer Cache
  • cache Maven system’s local repository (~/.m2)
  • cache target directory pipeline-wide using drillster/drone-volume-cache plugin
---
kind: pipeline
name: build-example-maven

globals:
  - &docker_creds
    username:
      from_secret: docker_username
    password:
      from_secret: docker_password

steps:
  - &cache_settings
    name: restore-cache
    image: drillster/drone-volume-cache
    settings:
      restore: true
      mount:
        - target
    volumes:
      - name: cache
        path: /cache

  - name: prepare
    image: busybox
    commands:
      - mkdir -p /cache/${DRONE_REPO}/target
      - mkdir -p /cache/${DRONE_REPO}/docker-lib1
      - mkdir -p /cache/${DRONE_REPO}/docker-lib2
      - mkdir -p /cache/${DRONE_REPO}/m2
    volumes:
      - name: cache
        path: /cache

  - name: build-war
    pull: default
    image: maven:3.6.0-jdk-8-alpine
    commands:
      - mvn -B -Dproject.version=${DRONE_BRANCH//\\//-}${DRONE_TAG//\\//-}-${DRONE_COMMIT_SHA} -Pwar prepare-package war:exploded
      - ls ./target -la
    volumes:
      - name: m2
        path: /root/.m2

  - name: package-1
    image: plugins/docker
    settings:
      dockerfile: Dockerfile.main
      tags:
        - "${DRONE_BRANCH//\\//-}${DRONE_TAG//\\//-}"
        - "${DRONE_BRANCH//\\//-}${DRONE_TAG//\\//-}-${DRONE_COMMIT_SHA}"
      repo: foo/main
      use_cache: true
      <<: *docker_creds
    volumes:
      - name: docker-lib1
        path: /var/lib/docker

  - name: package-2
    image: plugins/docker
    settings:
      dockerfile: Dockerfile.sidecar
      tags:
        - "${DRONE_BRANCH//\\//-}${DRONE_TAG//\\//-}"
        - "${DRONE_BRANCH//\\//-}${DRONE_TAG//\\//-}-${DRONE_COMMIT_SHA}"
      repo: foo/sidecar
      use_cache: true
      <<: *docker_creds
    volumes:
      - name: docker-lib2
        path: /var/lib/docker

  - <<: *cache_settings
    name: rebuild-cache
    settings:
      rebuild: true

volumes:
  - name: cache
    host:
      path: /var/cache
  - name: target
    host:
      path: /var/cache/${DRONE_REPO}/target
  - name: docker-lib1
    host:
      path: /var/cache/${DRONE_REPO}/docker-lib1
  - name: docker-lib2
    host:
      path: /var/cache/${DRONE_REPO}/docker-lib2
  - name: m2
    host:
      path: /var/cache/${DRONE_REPO}/m2