Building Docker images in Drone CI using Docker-in-Docker
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
steps:
- name: build
image: docker:dind
volumes:
- name: dockersock
path: /var/run
environment:
DOCKER_PASSWORD:
from_secret: docker_password
commands:
- 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
services:
- name: docker
image: docker:dind
privileged: true
volumes:
- name: dockersock
path: /var/run
volumes:
- name: dockersock
temp: {}
trigger:
branch:
- master
event:
- push
This shortens the build process from 4 to 3 minutes!