Skip to main content

Disk Usage and Cleanup

Docker objects -- images, containers, volumes, and build cache -- accumulate over time. Without periodic cleanup, they can consume gigabytes of disk space. This lesson covers how to monitor usage, identify waste, and clean up safely.

Checking Disk Usage​

Quick Summary​

docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 12 5 2.3GB 1.1GB (47%)
Containers 5 2 120MB 50MB (41%)
Local Volumes 3 1 500MB 0B (0%)
Build Cache 0 0 0B 0B

The RECLAIMABLE column shows how much space you can safely free.

Detailed Breakdown​

# See per-image, per-container, per-volume sizes
docker system df -v

# See writable layer size per container
docker ps -s

What Takes Up Space​

SourceWhat AccumulatesRisk Level
Old imagesPrevious versions after pulling updatesLow -- safe to prune
Dangling imagesUntagged layers from rebuilds (tagged <none>)Low -- safe to prune
Stopped containersContainers you stopped but did not removeLow -- safe to prune
Build cacheCached layers from docker buildLow -- safe to prune
Unused volumesNamed volumes not attached to any containerHigh -- may contain data

Prune Commands​

Docker provides prune commands for each object type:

CommandWhat It DeletesSafe?
docker image pruneDangling images (tagged <none>)✓ Safe
docker image prune -aAll images not used by running containers⚠ Removes pullable images
docker container pruneAll stopped containers✓ Safe
docker network pruneAll unused networks✓ Safe
docker volume pruneAll unused volumes❌ Dangerous -- can delete data
docker builder pruneBuild cache✓ Safe

The Master Command: docker system prune​

Cleans up multiple types at once:

# Remove stopped containers, dangling images, unused networks, and build cache
docker system prune

The Nuclear Option​

# Remove EVERYTHING not currently in use (including tagged images and volumes)
docker system prune -a --volumes
Extreme Caution

--volumes will delete your database data if the database container is stopped. Never put this in a cron job.

Safe Automated Cleanup​

For build servers or CI environments, use time-based filtering:

# Remove items older than 24 hours
docker system prune -f --filter "until=24h"

This is safe for CI because it does not delete recently-built artifacts.

Monitoring Disk Over Time​

CheckHow OftenCommand
Docker disk summaryWeeklydocker system df
Host disk spaceWeeklydf -h
Largest log filesMonthlysudo du -sh /var/lib/docker/containers/*/*-json.log | sort -rh | head
Unused volume checkMonthlydocker volume ls -f dangling=true

Key Takeaways​

  • Run docker system df weekly to understand where disk space is going.
  • docker system prune is safe for routine cleanup (stopped containers, dangling images, build cache).
  • Never use docker volume prune or --volumes without checking what volumes exist first -- it destroys data.
  • Use --filter "until=24h" for automated CI cleanup to avoid deleting fresh builds.
  • Combine Docker monitoring with host-level disk checks (df -h) for full visibility.

What's Next​