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>].
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
- Stop and remove the container(s) using the volume first.
- Then remove the volume.
docker rm -f a1b2c3d4e5f6
docker volume rm pgdataTear down with compose down -v
- Let compose remove containers and named volumes together in order.
docker compose down -vHow to prevent it
- Remove containers before their volumes, or use
docker compose down -v. - Use
--rmfor short-lived containers so they release volumes promptly.