01Understanding Docker Disk Usage
Docker can consume significant disk space over time. Images, containers, volumes, and build cache all accumulate.
**What takes up space:**
• Images (pulled and built)
• Containers (running and stopped)
• Volumes (persistent data)
• Build cache (layer cache from builds)
• Logs (if not rotated)
1# See overall Docker disk usage2docker system df34# Detailed breakdown5docker system df -v67# Example output:8# TYPE TOTAL ACTIVE SIZE RECLAIMABLE9# Images 45 12 8.5GB 5.2GB (61%)10# Containers 15 8 2.1GB 1.8GB (85%)11# Local Volumes 23 8 15GB 12GB (80%)12# Build Cache 150 0 3.2GB 3.2GB1314# The RECLAIMABLE column shows what can be cleaned up02Cleaning Up with Prune Commands
Docker provides prune commands to clean up unused resources. Be careful—some commands are destructive!
1# Remove unused containers, networks, images, and cache2docker system prune34# Also remove unused volumes (DANGEROUS - data loss possible!)5docker system prune --volumes67# Remove ALL unused images, not just dangling ones8docker system prune -a910# Individual prune commands (more control):11docker container prune # Remove stopped containers12docker image prune # Remove dangling images13docker image prune -a # Remove all unused images14docker volume prune # Remove unused volumes (CAREFUL!)15docker network prune # Remove unused networks16docker builder prune # Remove build cache1718# Prune with filters19docker image prune -a --filter "until=168h" # Older than 1 week20docker container prune --filter "until=24h" # Older than 1 day'docker volume prune' can delete data! Always verify volumes are truly unused before pruning.
03Automated Cleanup with Cron
Set up automated cleanup to prevent disk from filling up. Be conservative with automation—you don't want to accidentally delete data.
1# Create cleanup script2cat > /usr/local/bin/docker-cleanup.sh << 'EOF'3#!/bin/bash4set -e56echo "Docker cleanup starting at $(date)"78# Remove containers stopped more than 24 hours ago9docker container prune -f --filter "until=24h"1011# Remove images not used for 7 days12docker image prune -a -f --filter "until=168h"1314# Remove build cache older than 7 days15docker builder prune -f --filter "until=168h"1617# Remove unused networks18docker network prune -f1920# DO NOT auto-prune volumes - too risky2122echo "Cleanup complete at $(date)"23docker system df24EOF2526chmod +x /usr/local/bin/docker-cleanup.sh2728# Add to cron (runs daily at 3 AM)29echo "0 3 * * * /usr/local/bin/docker-cleanup.sh >> /var/log/docker-cleanup.log 2>&1" | crontab -Log cleanup output so you can review what was deleted. Never automate volume pruning.
04Managing Container Logs
Container logs can grow unbounded without rotation. Configure log rotation to prevent disk exhaustion.
1# Check log sizes2sudo du -sh /var/lib/docker/containers/*/*-json.log | sort -rh | head -1034# Set daemon-wide log defaults5# /etc/docker/daemon.json6{7 "log-driver": "json-file",8 "log-opts": {9 "max-size": "10m",10 "max-file": "3"11 }12}1314# Restart Docker to apply15sudo systemctl restart docker1617# Note: Existing containers keep their old log config18# You need to recreate containers for new defaults1920# Emergency: Truncate a specific log (use with caution)21sudo truncate -s 0 /var/lib/docker/containers/<container-id>/*-json.log05Managing Volumes
Volumes persist data but can become orphaned when containers are removed. Manage them carefully.
1# List all volumes2docker volume ls34# Find orphaned/unused volumes5docker volume ls -f dangling=true67# Inspect a volume8docker volume inspect volume_name910# Find what's using a volume11docker ps -a --filter volume=volume_name1213# Remove specific volumes (after verification!)14docker volume rm volume_name1516# NEVER run this without verification17# docker volume prune # This deletes ALL unused volumes!1819# Backup a volume before removing20docker run --rm -v volume_name:/data -v $(pwd):/backup alpine tar czf /backup/volume_backup.tar.gz -C /data .Always identify what's in a volume before removing it. There's no undo for deleted volumes.
06Moving Docker's Data Directory
If your root partition is full, you can move Docker's data to a larger drive.
1# Stop Docker2sudo systemctl stop docker34# Move existing data5sudo mv /var/lib/docker /new/path/docker67# Create symlink (simple method)8sudo ln -s /new/path/docker /var/lib/docker910# OR configure in daemon.json (cleaner)11# /etc/docker/daemon.json12{13 "data-root": "/new/path/docker"14}1516# Start Docker17sudo systemctl start docker1819# Verify20docker info | grep "Docker Root Dir"Use an SSD for Docker's data directory if possible. Performance will be much better than HDD.