Docker System Cleanup & Disk Space
Free up disk space with Docker prune commands. Learn how to clean containers, images, volumes, networks, and build cache. Monitor Docker disk usage.
System
Detailed Explanation
Checking Disk Usage
Start by understanding what Docker is consuming:
docker system df
Output shows usage for images, containers, volumes, and build cache:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 45 12 12.5GB 8.2GB (65%)
Containers 15 3 256MB 200MB (78%)
Local Volumes 8 4 3.1GB 1.5GB (48%)
Build Cache 120 0 5.8GB 5.8GB (100%)
The Nuclear Option: docker system prune
# Remove all stopped containers, unused networks, dangling images, and build cache
docker system prune
# Also remove ALL unused images (not just dangling)
docker system prune -a
# Also remove volumes (DANGEROUS: deletes data!)
docker system prune -a --volumes
# Skip the confirmation prompt
docker system prune -a -f
Targeted Cleanup
For more control, prune specific resource types:
# Remove stopped containers
docker container prune
# Remove dangling images
docker image prune
# Remove ALL unused images
docker image prune -a
# Remove unused volumes
docker volume prune
# Remove unused networks
docker network prune
# Remove build cache
docker builder prune
# Remove build cache older than 24h
docker builder prune --filter "until=24h"
Automated Cleanup Scripts
#!/bin/bash
# Weekly cleanup script
echo "Docker disk usage before cleanup:"
docker system df
docker container prune -f
docker image prune -a -f --filter "until=168h"
docker volume prune -f
docker builder prune -f --filter "until=168h"
echo "Docker disk usage after cleanup:"
docker system df
Use Case
Reclaiming disk space on development machines and CI/CD runners, setting up automated cleanup cron jobs for build servers, and troubleshooting 'no space left on device' errors in Docker environments.