jlelse’s Blog

Thoughts, stories and ideas

Building Docker images in Drone CI using Docker-in-Docker

Published on in 👨‍💻 Dev
Short link: https://b.jlel.se/s/531
Share onlineTranslate

This evening I tried to improve the build process of GoBlog. GoBlog gets built using Drone CI and Docker. The problem was that two image variants are to be built, one based on the other, and the whole thing always took quite a long time.

So far, I have relied on the Docker plugin for Drone and split the build of the two variants into two build steps and two Dockerfiles. But that’s kind of a stupid solution. It would be better to just use different build steps in a single Dockerfile.

After all, Docker-in-Docker exists and using BuildKit, the Docker build can be further accelerated.

After a lot of trial and error (and reading the documentation), I finally ended up with the following Drone configuration:

kind: pipeline
name: default
type: docker

  - name: build
    image: docker:dind
      - name: dockersock
        path: /var/run
        from_secret: docker_password
      - sleep 5 # give docker enough time to start
      - echo $DOCKER_PASSWORD | docker login --username nologin --password-stdin example.org
      - DOCKER_BUILDKIT=1 docker build -t example.org/image:latest . --target base
      - DOCKER_BUILDKIT=1 docker build -t example.org/image:tools . --target tools
      - docker push --all-tags example.org/image

  - name: docker
    image: docker:dind
    privileged: true
      - name: dockersock
        path: /var/run

  - name: dockersock
    temp: {}

    - master
    - push

This shortens the build process from 4 to 3 minutes!

Tags: , ,

Jan-Lukas Else
Interactions & Comments