How to remove orphaned volumes in Docker
When using Docker sometimes it's necessary to do some cleanup in order to free up space. I ran into an issue recently where I could not start my database container. The error message was:
db_1 | creating subdirectories ... initdb: could not create directory "/var/lib/postgresql/data/global": No space left on device
One simple way to free up space is to delete dangling or orphaned volumes. A dangling volume is a volume that is not referenced by any container.
Let's break this down.
First, if you want to see a list of the dangling volumes you can simply run:
docker volume ls -qf dangling=true
docker volume ls lists the volumes and -qf means list only the ids and filter on dangling=true.
To delete these volumes we'll pass them in to the docker volume rm function which takes a volume id or list of ids. The final command is:
docker volume rm $(docker volume ls -qf dangling=true)