Skip to content
Latchkey

Docker "cannot remove volume in use" in CI

A named volume cannot be removed while any container has it mounted, even a stopped one. CI teardown that tears down volumes before the containers that use them - or that leaves a service container around - hits "volume is in use".

What this error means

A docker volume rm pgdata fails with Error response from daemon: remove pgdata: volume is in use - [<container id>].

docker
Error response from daemon: remove pgdata: volume is in use - [a1b2c3d4e5f6]

Common causes

A container still mounts the volume

A stopped or running container referencing the volume blocks its removal.

Teardown ordering removes the volume too early

Removing the volume before the consuming container hits the conflict.

How to fix it

Remove consuming containers, then the volume

  1. Stop and remove the container(s) using the volume first.
  2. Then remove the volume.
Terminal
docker rm -f a1b2c3d4e5f6
docker volume rm pgdata

Tear down with compose down -v

  1. Let compose remove containers and named volumes together in order.
Terminal
docker compose down -v

How to prevent it

  • Remove containers before their volumes, or use docker compose down -v.
  • Use --rm for short-lived containers so they release volumes promptly.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →