docker.recipes
Operations7 min read

Docker System Maintenance

Keep your Docker host healthy with regular cleanup, disk management, and automated maintenance routines.

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 usage
2docker system df
3
4# Detailed breakdown
5docker system df -v
6
7# Example output:
8# TYPE TOTAL ACTIVE SIZE RECLAIMABLE
9# 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.2GB
13
14# The RECLAIMABLE column shows what can be cleaned up

02Cleaning 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 cache
2docker system prune
3
4# Also remove unused volumes (DANGEROUS - data loss possible!)
5docker system prune --volumes
6
7# Remove ALL unused images, not just dangling ones
8docker system prune -a
9
10# Individual prune commands (more control):
11docker container prune # Remove stopped containers
12docker image prune # Remove dangling images
13docker image prune -a # Remove all unused images
14docker volume prune # Remove unused volumes (CAREFUL!)
15docker network prune # Remove unused networks
16docker builder prune # Remove build cache
17
18# Prune with filters
19docker image prune -a --filter "until=168h" # Older than 1 week
20docker 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 script
2cat > /usr/local/bin/docker-cleanup.sh << 'EOF'
3#!/bin/bash
4set -e
5
6echo "Docker cleanup starting at $(date)"
7
8# Remove containers stopped more than 24 hours ago
9docker container prune -f --filter "until=24h"
10
11# Remove images not used for 7 days
12docker image prune -a -f --filter "until=168h"
13
14# Remove build cache older than 7 days
15docker builder prune -f --filter "until=168h"
16
17# Remove unused networks
18docker network prune -f
19
20# DO NOT auto-prune volumes - too risky
21
22echo "Cleanup complete at $(date)"
23docker system df
24EOF
25
26chmod +x /usr/local/bin/docker-cleanup.sh
27
28# 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 sizes
2sudo du -sh /var/lib/docker/containers/*/*-json.log | sort -rh | head -10
3
4# Set daemon-wide log defaults
5# /etc/docker/daemon.json
6{
7 "log-driver": "json-file",
8 "log-opts": {
9 "max-size": "10m",
10 "max-file": "3"
11 }
12}
13
14# Restart Docker to apply
15sudo systemctl restart docker
16
17# Note: Existing containers keep their old log config
18# You need to recreate containers for new defaults
19
20# Emergency: Truncate a specific log (use with caution)
21sudo truncate -s 0 /var/lib/docker/containers/<container-id>/*-json.log

05Managing Volumes

Volumes persist data but can become orphaned when containers are removed. Manage them carefully.
1# List all volumes
2docker volume ls
3
4# Find orphaned/unused volumes
5docker volume ls -f dangling=true
6
7# Inspect a volume
8docker volume inspect volume_name
9
10# Find what's using a volume
11docker ps -a --filter volume=volume_name
12
13# Remove specific volumes (after verification!)
14docker volume rm volume_name
15
16# NEVER run this without verification
17# docker volume prune # This deletes ALL unused volumes!
18
19# Backup a volume before removing
20docker 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 Docker
2sudo systemctl stop docker
3
4# Move existing data
5sudo mv /var/lib/docker /new/path/docker
6
7# Create symlink (simple method)
8sudo ln -s /new/path/docker /var/lib/docker
9
10# OR configure in daemon.json (cleaner)
11# /etc/docker/daemon.json
12{
13 "data-root": "/new/path/docker"
14}
15
16# Start Docker
17sudo systemctl start docker
18
19# Verify
20docker info | grep "Docker Root Dir"

Use an SSD for Docker's data directory if possible. Performance will be much better than HDD.