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
- Stop and remove the container(s) using the volume first.
- Then remove the volume.
Terminal
docker rm -f a1b2c3d4e5f6
docker volume rm pgdataTear down with compose down -v
- Let compose remove containers and named volumes together in order.
Terminal
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.
Related guides
Docker "image is being used by stopped container" in CIFix Docker "conflict: unable to delete image, it is being used by stopped container" in CI - a `docker rmi` f…
Docker "device or resource busy" - Fix Unmount/Remove Failures in CIFix Docker "unlinkat ... device or resource busy" / "Error removing ... device or resource busy" in CI - a st…
Docker Compose "read-only file system" on a Volume Mount in CIFix Docker Compose "Read-only file system" when a service writes to a mounted volume in CI - a `:ro` flag, a…