How to reclaim disk space from Docker

· 2 min read dockerdevopsdisk

Docker never deletes images or volumes on its own. Over months these pile up. Check usage first:

docker system df

Add -v for a per-object breakdown.

The one-liner

Remove everything unused (stopped containers, dangling images, orphaned networks, build cache):

docker system prune

Add flags for deeper cleaning:

docker system prune -a          # also all images not used by a container
docker system prune --volumes   # also anonymous volumes

-a is aggressive: images will re-download on next pull. Never run --volumes on a database host unless you have verified backups.

Targeted cleanup

Stopped containers:

docker container prune

Dangling images (<none> tags left behind by rebuilds):

docker image prune

All untagged-and-unused build cache (often the biggest win):

docker builder prune

Specific image:

docker rmi myimage:mytag

Find what is eating space

Largest images:

docker images --format '{{.Size}}\t{{.Repository}}:{{.Tag}}' | sort -rh | head

Volumes live under /var/lib/docker/volumes — inspect before deleting:

docker volume ls
docker volume rm <name>

Keep it clean automatically

A weekly cron job keeps things sane on build servers:

0 4 * * 0 /usr/bin/docker system prune -af --filter "until=168h" >> /var/log/docker-prune.log 2>&1

until=168h protects anything touched in the last week.