Drone

Build Docker image and re-use in the next step

A common question we receive is how to build a Docker image and then use that image in a subsequent step. This can be achieved by mounting the host machine Docker socket into your container (the repository must be Trusted). You can then build the image and use in subsequent steps.

Please note that if your image uses the :latest tag the system will automatically try to pull a newer version from the registry. As a result, the image created previously will be overwritten. You should therefore change the default pull behavior, demonstrated below.

---
kind: pipeline
name: default

steps:
- name: build
  image: docker
  volumes:
  - name: dockersock
    path: /var/run/docker.sock
  commands:
  - docker build -t foo:latest

- name: test
  image: foo:latest
  pull: if-not-exists
  commands:
  - echo bar

volumes:
- name: dockersock
  host:
    path: /var/run/docker.sock

...

As an aside, if you find yourself frequently mounting the host machine Docker socket for your pipelines, you may want to re-consider using Docker pipelines. Instead consider using exec pipelines, which execute builds directly on the host and do not use Docker.