A safe place to share everything about Docker. 8-O ====== Docker Cleanup Guide: Reclaiming Disk Space ====== Docker is an amazing technology that backs up each project I work on. Without diving too much into details, let's go through a workflow on how to clean up space that Docker takes up on your system. ===== Understanding Docker's Storage Model ===== The concept behind Docker is that it wants to cache every image it runs. An instance of a running image is a container, and they also use disk space. Finally, to persist data between container uptimes, Docker uses volumes to persist data on the host system. ==== Step 1: Understanding What Takes Space ==== The first step is always understanding what's consuming your disk space: ```bash docker system df ``` This command shows you a high-level overview, but for detailed analysis, use: ```bash docker system df -v ``` This verbose output breaks down Docker's storage into four key components: === 1. Images - The Blueprint Cache === - **What they are:** Templates used to create containers - **Why they accumulate:** Docker caches every image you pull or build - **Space impact:** Often the largest consumer (can be 20-30GB+) - **Reclaimable:** Unused images can be safely removed === 2. Containers - The Running Instances === - **What they are:** Actual running or stopped instances of images - **Why they accumulate:** Stopped containers aren't automatically deleted - **Space impact:** Usually minimal, but can add up - **Reclaimable:** Stopped containers can be safely removed if you can recreate them === 3. Local Volumes - The Data Persistence Layer === - **What they are:** Persistent storage for container data - **Why they accumulate:** Created for databases, config files, user data - **Space impact:** Can be substantial, especially database volumes - **Reclaimable:** ⚠️ **CAUTION** - Only remove if you're sure data isn't needed === 4. Build Cache - The Development Speedup === - **What it is:** Cached layers from building Docker images - **Why it accumulates:** Each docker build creates cached layers - **Space impact:** Can easily reach 15-20GB+ during active development - **Reclaimable:** Safe to remove, but rebuilds will be slower ---- [[https://docs.portainer.io/user/docker/containers/stats|Checking the resource usage in Portainer]] ==== Step 2: Systematic Cleanup Workflow ==== === Investigate Before You Delete === ```bash # List all images with sizes docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" # List all containers (running and stopped) docker ps -a # List all volumes docker volume ls # Check build cache details docker buildx du ``` === Safe Cleanup Commands === **Remove unused images (usually the biggest space saver):** ```bash docker image prune -a # Removes all unused images ``` **Remove stopped containers:** ```bash docker container prune # Removes all stopped containers # Or selectively: docker rm [container_id] [container_id] ``` **Remove unused volumes (⚠️ BE CAREFUL - this deletes data):** ```bash docker volume prune # Only removes truly unused volumes # Or selectively: docker volume rm [volume_name] ``` **Remove build cache:** ```bash docker builder prune -a # Safe to remove, just slows future builds ``` ==== The Nuclear Option ==== If you want to reclaim maximum space and start fresh: ```bash docker system prune -a --volumes ``` > ⚠️ **Warning:** This removes everything unused - images, containers, volumes, networks, and build cache. ===== Step 3: Sustainable Cleanup Strategy ===== Rather than the "delete everything and hope it's fine" approach, I recommend this workflow: 1. **Weekly maintenance:** Run `docker system df -v` to monitor growth 2. **Monthly cleanup:** Remove unused images and build cache 3. **Project completion:** Clean up project-specific volumes and images 4. **Before major updates:** Full cleanup to start with a clean slate ## Pro Tips for Long-term Management * **Use .dockerignore:** Reduces image sizes and build cache bloat * **Multi-stage builds:** Keep final images lean * **Regular pruning:** Don't let cache accumulate to 50GB+ before cleaning * **Volume data:** Backup important container data separately --- Happy coding, and may your Docker storage never exceed your SSD capacity! 🐳 > **Remember:** The goal isn't to delete everything, but to maintain a balance between performance (caching) and disk usage. Clean regularly, but thoughtfully.