Docker: No Space Left on Device
Fix Docker disk space issues, clean unused images, containers, volumes and build cache to free up space
Docker accumulates unused images, containers, volumes, and build cache over time. This guide shows you how to diagnose and free up disk space.
Diagnose Disk Space Issues
Check Partition Usage
df -hSample output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 48G 1.5G 97% /Check Docker Disk Usage
docker system dfSample output:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 15 4 8.5GB 7.2GB (84%)
Containers 12 2 2.3GB 2.1GB (90%)
Local Volumes 8 3 1.8GB 1.5GB (83%)
Build Cache 24 0 3.2GB 3.2GB (100%)Quick Complete Cleanup
Clean all unused Docker resources at once:
# WARNING: Removes ALL unused images, containers, volumes, and build cache
sudo docker system prune -a --volumesSample output:
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all unused images
- all unused volumes
Are you sure you want to continue? [y/N] y
Deleted Containers: 8
Deleted Images: 12
Deleted Volumes: 5
Deleted Build Cache: 24
Total reclaimed space: 14.5GBdocker system prune -a removes ALL unused images, including those needed to restart stopped containers. Ensure you have docker-compose.yml files to recreate them if needed.
Selective Cleanup
Remove Stopped Containers Only
docker container prune
# Without confirmation
docker container prune -fRemove Unused Images
# Only dangling images (orphaned)
docker image prune
# All unused images (not used by any container)
docker image prune -a
# Remove images without confirmation
docker image prune -a -fRemove Unused Volumes
docker volume prune
# Without confirmation
docker volume prune -fRemove Build Cache
docker builder prune
# All build cache (even recent)
docker builder prune -a
# Without confirmation
docker builder prune -a -fContainer Log Rotation
Container logs can grow to gigabytes. Configure automatic log rotation.
Check Log Size
# Find which container has large logs
docker inspect <container_id> --format='{{.LogPath}}'
# Check size
du -h /var/lib/docker/containers/<container_id>/*-json.logTruncate Large Logs
# Clear logs for a container
sudo truncate -s 0 /var/lib/docker/containers/<container_id>/*-json.logEnable Log Rotation in Docker Daemon
Edit /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3",
"labels": "production_status=true"
}
}Restart Docker:
sudo systemctl restart dockerThis keeps each log file max 10MB, keeps 3 files per container (30MB total).
Identify Large Images
# List all images with sizes
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
# Find largest images
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | sort -k3 -h -r | head -10Remove Specific Image
docker image rm image_name:tag
# Force remove (even if in use)
docker image rm -f image_name:tagMove Docker to Larger Partition
If your root partition is full and you have another disk:
Edit Docker Daemon Configuration
Edit /etc/docker/daemon.json:
{
"data-root": "/mnt/large-disk/docker"
}Move Existing Data
# Stop Docker
sudo systemctl stop docker
# Create new directory
sudo mkdir -p /mnt/large-disk/docker
# Copy existing data (preserves permissions)
sudo rsync -av /var/lib/docker/ /mnt/large-disk/docker/
# Start Docker
sudo systemctl start docker
# Verify
docker system df
# Remove old data (after verification)
sudo rm -rf /var/lib/dockerAutomated Weekly Cleanup
Schedule automatic cleanup with crontab:
sudo crontab -eAdd:
# Clean Docker every Sunday at 2 AM
0 2 * * 0 /usr/bin/docker system prune -a --volumes -fVerify:
sudo crontab -lAnalyze Disk Usage in Detail
Use ncdu for interactive disk usage inspection:
sudo apt install ncdu
# Analyze Docker directory
sudo ncdu /var/lib/dockerNavigate with arrow keys, press 'd' to delete, 'q' to quit.
Troubleshooting Checklist
- Check available space:
df -h - View Docker usage:
docker system df - List largest images:
docker images --format "table {{.Repository}}\t{{.Size}}" | sort -k2 -h -r - Remove unused images:
docker image prune -a -f - Remove unused volumes:
docker volume prune -f - Clean build cache:
docker builder prune -a -f - Check container logs:
docker inspect <container> --format='{{.LogPath}}' - Enable log rotation in
/etc/docker/daemon.json - Verify space freed:
docker system df
Recovery: Restore from Backup
If Docker data was accidentally deleted, restore from backup:
# Stop Docker
sudo systemctl stop docker
# Restore data
sudo rsync -av /backup/docker/ /var/lib/docker/
# Start Docker
sudo systemctl start docker
# Verify containers and volumes
docker ps -a
docker volume ls