Docker Tutorial · Production Practices · Lesson 25 of 26
Disk Usage and Cleanup
Images, build cache and stopped containers fill disks quietly; see where space goes and reclaim it safely.
Where space goes
docker system df shows space used by images, containers, local volumes and the build cache, and how much is
reclaimable. On CI runners and small servers, unused images and build cache are the usual culprits.
Cleaning up
| Command | Removes |
|---|---|
docker container prune |
Stopped containers |
docker image prune |
Dangling images (untagged) |
docker image prune -a |
All images not used by a container |
docker builder prune |
Build cache |
docker volume prune |
Unused volumes (data!) |
docker system prune |
Containers, networks, dangling images and cache |
Be careful with volumes: an unused volume may still hold the only copy of a database.
docker system df --format '{{.Type}}' | sed 's/^/tracked by docker system df: /'
# label the demo's containers so the prune below can only ever touch them
for i in 1 2 3; do docker run --label rc-demo=cleanup alpine:3.24 true; done
echo "stopped demo containers: $(docker ps -a --filter label=rc-demo=cleanup --filter status=exited -q | wc -l)"
docker container prune -f --filter label=rc-demo=cleanup > /dev/null
echo "after a label-scoped prune: $(docker ps -a --filter label=rc-demo=cleanup -q | wc -l)"
Output
tracked by docker system df: Images tracked by docker system df: Containers tracked by docker system df: Local Volumes tracked by docker system df: Build Cache stopped demo containers: 3 after a label-scoped prune: 0
Run this on your computer — see “Get Started” for setup.
See Prune unused objects.
Test yourself
Which prune command can destroy database data?
docker image prunedocker builder prunedocker volume prunedocker container prune
Show answer
Answer: docker volume prune
Volumes hold persistent data; check before pruning them.