Clean up docker containers at the end of a run

I’m using the docker runner to run docker-compose with a mounted docker socket to create database and cache containers for testing. This creates sibling containers on the host machine which need to be cleaned up at the end of a run. Is it possible to ensure a pipeline step (such as docker-compose down) is executed for each build regardless of success or failure?

when you mount the host machine docker socket you are working with docker out-of-band which means Drone will not cleanup after you. You can add a step to your pipeline that executes on failure to cleanup any dangling resources:

steps:
- name: cleanup
  image: docker
  commands:
  - docker kill ...
  - docker rm -v ....
  when:
    status: [ failure ]
  volumes: ...

However, if you cancel a pipeline it could still leave resources on the host machine, since the above step would not execute on cancel. So your best bet is to install a docker garbage collector (e.g. docker-gc) and have it periodically delete oprhaned resources.

Generally speaking, we do not recommend using docker-compose with Drone. The two technologies do not work well together (the docker-compose services will be on a completely different network than your pipeline, and will be unreachable from inside your pipeline). Instead, Drone has many native features that can be used to replace docker-compose, including support for launching databases and cache containers as part of your pipeline. See https://docs.drone.io/pipeline/docker/syntax/services/ and https://docs.drone.io/pipeline/docker/examples/

Services is exactly what I was looking for! Thanks once again

Services seem to be incompatible with drone exec --resume-at <step> which is used for debugging. Services don’t start when using that command line flag.

sounds like an oversight. we would accept a pull request at drone/drone-cli to improve this behavior.