Prune Docker Now: Reclaim Disk Space with a Single Command
Introduction
Tired of Docker eating up your disk? In development environments, stale images, stopped containers, and orphaned volumes can quickly fill up storage, leading to slow builds or outright failures. Fortunately, Docker provides a single, powerful command that can clean up all of these leftovers in seconds. This post expands on the quick‑clean video tutorial and shows you how to use docker system prune -a --volumes safely and effectively.
Step‑by‑Step Technical Guide
1. Inspect Current Disk Usage
Before you start deleting anything, get a clear picture of what Docker is currently consuming. The docker system df command breaks down usage by images, containers, local volumes, and build cache.
docker system df
The output looks something like this:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 25 5 4.3GB 3.2GB (74%)
Containers 12 2 1.1GB 1.1GB (100%)
Local Volumes 8 3 2.5GB 2.0GB (80%)
Build Cache 0 0 0B 0B
Take note of the RECLAIMABLE column – those are the bytes you can safely free.
2. Run the Prune Command
The heavy‑lifting is done by docker system prune. Adding the -a flag tells Docker to remove all unused images, not just dangling ones, while --volumes extends the cleanup to orphaned volumes.
docker system prune -a --volumes
Docker will prompt for confirmation:
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all images without at least one container associated to them
- all build cache
- all volumes not referenced by any container
Proceed? [y/N] y
Press y and hit Enter. Docker then proceeds to delete the resources, printing a summary of reclaimed space.
3. Verify Reclaimed Space
Run the usage command again to see the impact of the prune.
docker system df
You should now see dramatically lower numbers in the SIZE and RECLAIMABLE columns, confirming that the host is cleaner and has more free disk space.
Best Practices & Recommendations
| Practice | Why It Matters |
|---|---|
| Run a dry‑run first | Use docker system df to understand what will be removed. |
| Schedule regular pruning | A weekly prune in development environments prevents disk bloat. |
Avoid -a in production |
In production you often need historic images for rollbacks; omit -a to keep them. |
| Version‑specific pruning | Target a specific image with docker image prune -a --filter "until=24h" to keep recent builds only. |
| Automate in CI/CD | Add the prune step to your CI pipeline after successful builds to keep runners lean. |
When not to use -a
-
Production servers where you need to retain previous image versions for quick rollbacks.
-
Shared build agents that other teams rely on; aggressive pruning may remove images they expect to exist.
Safer Alternatives
-
docker image prune– removes only dangling images. -
docker container prune– removes only stopped containers. -
docker volume prune– removes only unused volumes.
Combine them as needed instead of a blanket -a prune.
Pitfalls to Avoid
-
Accidental data loss – Volumes may contain persistent data. Double‑check that no running containers depend on them before using
--volumes. -
Pruning while builds are in progress – If a build is pulling images, a concurrent prune could delete those images mid‑process, causing failures.
-
Ignoring reclaimed space – After pruning, you may still see space usage due to Docker's overlay filesystem; a system reboot can sometimes free the remaining blocks.
Performance Benefits
-
Faster
docker build– With fewer layers and images cached locally, Docker can skip redundant downloads. -
Reduced I/O latency – Less disk fragmentation improves read/write performance for container start‑up.
-
Lower memory pressure – Fewer stopped containers mean less metadata held in the Docker daemon.
Conclusion
Keeping your Docker host tidy is a simple yet powerful habit. By regularly inspecting usage and running docker system prune -a --volumes (or its more granular cousins), you reclaim valuable storage, speed up builds, and avoid nasty out‑of‑space errors. Adopt a weekly prune schedule in development, be cautious with the -a flag in production, and automate cleanup in your CI pipelines for a consistently lean Docker environment.
How often do you prune your Docker environment? Share your routine in the comments!
Related Posts
Useful Linux Commands for Logs and File Search
A production-style walkthrough of find, tail, journalctl, grep, and awk for server troubleshooting.
Demystifying Kubernetes Service Discovery
Unlock the full potential of Kubernetes with our comprehensive guide to service discovery, enabling seamless communication between services within your cluster.
Harnessing the Power of Kubernetes: Pods and Deployments Demystified
Discover how Kubernetes Pods and Deployments provide powerful tools for managing and scaling applications efficiently within containerized environments.
Effortlessly Set Up Your First Kubernetes Cluster on AWS EKS
Learn how to effortlessly set up your first Kubernetes cluster on AWS EKS with this step-by-step guide, perfect for developers aiming to leverage cloud scalability.