/var/lib/docker grows. Stopped containers, dangling images, unused volumes, and the build cache accumulate over weeks until the partition fills and the daemon stops accepting writes. docker system prune is the cleanup hammer, but it’s also the command that has accidentally deleted production volumes in plenty of real incidents. This runbook is how to use it without losing data.
How to verify
See where the disk is actually going before deciding what to prune:
docker system df
docker system df -v | head -50
# Underlying directory sizes
sudo du -h --max-depth=1 /var/lib/docker | sort -h
# Build cache breakdown
docker buildx du
docker system df shows the headline numbers: images, containers, local volumes, build cache. The -v flag lists every individual item with its size and reclaimable bytes. If reclaimable is large and total is large, prune is the answer. If reclaimable is small and total is large, the disk is being used by things you actually need (volume data, the build cache backing your CI) — prune won’t help.
What’s happening
Docker stores several distinct things under /var/lib/docker:
- Images — read-only layers. Anonymous layers (intermediate build layers, unreferenced base layers) accumulate.
- Containers — including stopped ones. A stopped container holds onto its writable layer and logs.
- Volumes — both named and anonymous. Anonymous volumes are created by every container with a
VOLUMEdirective that wasn’t given an explicit name; many end up unreferenced. - Networks — user-defined bridges left behind by removed Compose projects.
- Build cache — BuildKit’s per-layer cache, which can grow to tens of GB with active builds.
prune walks each category and removes things that aren’t referenced. The risk is in volumes: an anonymous volume holding two months of database data looks identical to one created by yesterday’s test run.
The procedure
- The safe operations — these never delete persistent state:
# Remove stopped containers
docker container prune -f
# Remove dangling images (no tag, no container reference)
docker image prune -f
# Remove unused user-defined networks
docker network prune -f
# Remove build cache older than 30 days, keep at most 20 GB
docker buildx prune -f --filter "until=720h" --keep-storage 20GB
These four together free most of the disk most of the time without touching named volumes. Safe as a weekly cron.
- Image prune with the
-aflag is more aggressive — it removes any image not used by a running container, even if there are stopped containers using it:
docker image prune -af --filter "until=168h"
--filter "until=168h" only prunes images last used more than 168 hours (7 days) ago. Without that filter, -a will delete images you just pulled but haven’t yet run.
- Volume prune is the dangerous one. Default behavior only removes volumes not referenced by any container, but a stopped container counts as a reference. Never run plain
docker volume pruneon production without a recent backup confirmed.
# Show what would be deleted, don't delete
docker volume ls --filter dangling=true
# Delete dangling volumes (no container, running or stopped, references them)
docker volume prune -f --filter "label!=keep"
The label!=keep filter is a defensive pattern — mark anything important with --label keep=true at creation time and prune leaves them alone.
docker system pruneis the convenience wrapper. Read what each flag does before running:
docker system prune # containers + dangling images + networks + build cache
docker system prune -a # ...plus images not used by running container
docker system prune --volumes # ...plus dangling volumes — DANGEROUS
docker system prune -a --volumes # all of the above — VERY DANGEROUS
In production runbooks, we forbid --volumes in any prune script. Volumes are removed deliberately, one at a time, after confirming what’s on them.
- The scheduled job we run on every host:
#!/bin/bash
# /usr/local/sbin/docker-cleanup.sh — weekly cron
set -euo pipefail
log() { logger -t docker-cleanup "$*"; echo "$(date -Iseconds) $*"; }
log "starting"
log "disk before: $(df -h /var/lib/docker | tail -1)"
docker container prune -f
docker image prune -f
docker network prune -f
docker image prune -af --filter "until=168h"
docker buildx prune -f --filter "until=720h" --keep-storage 20GB
log "disk after: $(df -h /var/lib/docker | tail -1)"
log "complete"
Cron entry:
0 4 * * 0 /usr/local/sbin/docker-cleanup.sh > /var/log/docker-cleanup.log 2>&1
Weekly at 04:00 Sunday. Output goes to a log file you can rotate.
- Inspecting and removing specific items manually. The unsafe operations done safely:
# List dangling images with their size
docker images -f dangling=true --format "table {{.ID}}\t{{.Repository}}\t{{.Size}}"
# List volumes with name and mount point
docker volume ls --format "table {{.Name}}\t{{.Mountpoint}}\t{{.Driver}}"
# What's IN a volume (mount and peek)
docker run --rm -v <volume>:/data:ro alpine du -sh /data
# Remove specific volume after confirming it's not what you think it is
docker volume rm <name>
For databases, the safe sequence is: dump the volume contents, verify the dump, then remove.
- Compose’s own cleanup. When you tear down a project, anonymous volumes are kept by default:
docker compose down # containers + networks
docker compose down --volumes # ...plus anonymous volumes from this stack
docker compose down --rmi all # ...plus images this stack built
Use the explicit form. down --volumes will not touch named volumes from this project unless you also include --volumes.
Common pitfalls
- Running
docker system prune --volumeson a production host, especially afterdocker compose downhas stopped (but not removed) the containers — the volume is technically dangling, the data goes away. - Pruning images aggressively without
--filter "until=..."and deleting the image you’re about to deploy. - Forgetting that
docker buildx duanddocker system dfshow different things — buildx du shows the BuildKit cache; system df shows the image store. Both can be large independently. - Setting a cleanup cron that runs
--volumes“to keep things tidy” and discovering a developer’s local test database is gone on Monday morning. - Pruning while a CI build is running on the same host — the build’s working layers can be evicted mid-build.
For production hosts we run, the disk under /var/lib/docker is monitored with alerts at 80% and 90%, the scheduled cleanup runs weekly, and named volumes are inventoried — none of them are anonymous. It’s part of managed operations, the discipline that turns “docker disk full” from an outage into an alert handled before it bites.