Docker’s disk usage can quickly spiral out of control on production VPS environments, with the overlay2 storage driver consuming gigabytes through accumulated layers, unused volumes, and orphaned containers. This comprehensive guide shows you how to diagnose Docker storage issues, safely clean up disk space, and implement automated cleanup strategies on Ubuntu 24.04 LTS.
Whether you’re running applications on an Amsterdam VPS or New York VPS, these techniques will help you maintain optimal storage performance and prevent disk space emergencies.
Understanding Docker Storage Challenges
Docker’s layered filesystem architecture creates several storage challenges:
- Overlay2 growth from accumulated image layers
- Container logs that grow indefinitely
- Unused volumes consuming disk space
- Build cache from frequent image builds
- Dangling images from failed or incomplete builds
Left unchecked, these issues can lead to out-of-memory conditions and application failures. When disk space runs low, you might need to consider extending your VPS storage.
Prerequisites
Before starting this tutorial, ensure you have:
- Ubuntu 24.04 LTS VPS with root or sudo access
- Docker 24.0+ installed (
docker --version) - At least 2GB available disk space for safe operations
- Basic familiarity with Docker concepts
- Recent backup of critical data
Warning: Always backup critical data before performing cleanup operations. Some commands permanently delete data and cannot be undone.
Step-by-Step Storage Cleanup Tutorial
Step 1: Diagnose Docker Storage Usage
First, assess your current Docker disk usage:
# Check overall Docker disk usage
sudo docker system df
# Get detailed breakdown
sudo docker system df -v
# Check overlay2 directory size
sudo du -sh /var/lib/docker/overlay2
# List largest directories
sudo du -h /var/lib/docker/ | sort -rh | head -10
Step 2: Safe Docker Cleanup Operations
Perform cleanup operations in order of safety:
# Remove stopped containers
sudo docker container prune -f
# Remove dangling images (untagged)
sudo docker image prune -f
# Remove unused networks
sudo docker network prune -f
# Remove build cache
sudo docker builder prune -f
Step 3: Aggressive Cleanup (Use with Caution)
For severe disk space issues:
# Remove ALL unused images (not just dangling)
sudo docker image prune -a -f
# Remove unused volumes (be very careful!)
sudo docker volume prune -f
# Nuclear option - remove everything unused
sudo docker system prune -a -f --volumes
Critical Warning: The
docker system prune -a -f --volumescommand will delete ALL unused containers, images, networks, and volumes. Only use this if you’re certain about data retention.
Step 4: Clean Up Container Logs
Container logs can consume significant space:
# Check log sizes for all containers
sudo find /var/lib/docker/containers/ -name "*-json.log" -exec ls -lh {} \; | sort -k5 -hr
# Truncate logs for specific container
sudo truncate -s 0 /var/lib/docker/containers/CONTAINER_ID/*-json.log
# Configure log rotation in daemon.json
sudo tee /etc/docker/daemon.json <<EOF
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
EOF
# Restart Docker to apply changes
sudo systemctl restart docker
Step 5: Automate Cleanup with Cron
Create automated cleanup scripts:
# Create cleanup script
sudo tee /usr/local/bin/docker-cleanup.sh <<'EOF'
#!/bin/bash
echo "$(date): Starting Docker cleanup..."
# Remove stopped containers older than 24 hours
docker container prune -f --filter "until=24h"
# Remove dangling images
docker image prune -f
# Remove unused networks
docker network prune -f
# Remove build cache older than 48 hours
docker builder prune -f --filter "until=48h"
echo "$(date): Docker cleanup completed."
EOF
# Make executable
sudo chmod +x /usr/local/bin/docker-cleanup.sh
# Add to crontab (runs daily at 2 AM)
echo "0 2 * * * /usr/local/bin/docker-cleanup.sh >> /var/log/docker-cleanup.log 2>&1" | sudo tee -a /etc/crontab
Best Practices for Docker Storage Management
Preventive Measures
- Use multi-stage builds to reduce final image size
- Implement .dockerignore to exclude unnecessary files
- Regular cleanup scheduling prevents emergency situations
- Monitor disk usage with tools like
df -handdu - Use specific image tags instead of
:latest
Monitoring and Alerting
Set up monitoring for proactive disk space management:
# Create disk space alert script
sudo tee /usr/local/bin/disk-alert.sh <<'EOF'
#!/bin/bash
USAGE=$(df /var/lib/docker | tail -1 | awk '{print $5}' | sed 's/%//')
if [ $USAGE -gt 80 ]; then
echo "Docker disk usage at ${USAGE}%" | logger -t docker-alert
fi
EOF
# Run every 15 minutes
echo "*/15 * * * * /usr/local/bin/disk-alert.sh" | sudo tee -a /etc/crontab
Security Considerations
- Review containers before deletion to avoid removing critical data
- Use volume mounts for persistent data instead of container storage
- Implement backup strategies for important volumes
- Test cleanup procedures in development environments first
Conclusion
Effective Docker storage management is crucial for maintaining healthy VPS environments. By implementing regular cleanup procedures, configuring log rotation, and monitoring disk usage, you can prevent storage-related issues before they impact your applications.
The automated cleanup scripts and monitoring tools covered in this guide will help maintain optimal performance whether you’re running Docker workloads on high-performance infrastructure in Amsterdam or New York. For production environments requiring consistent high-performance storage with NVMe and triple data replication, consider upgrading to enterprise-grade VPS solutions.
Remember to always test cleanup procedures in non-production environments first, and maintain regular backups of critical data. With proper storage management practices, your Docker deployments will run efficiently and reliably.




