Beginner LevelDocker & Deployment
How do you clean up dangling images and stopped containers using docker system prune?
❓ Interview Prompt: Explain dangling images, build cache bloat, and the docker system prune command for host disk space reclamation.
Over time, Docker accumulates significant disk waste from:
Types of Docker Disk Waste:
1
Dangling Images: Unnamed (untagged) intermediate images created when rebuilding images with the same tag. Old image loses its tag but lingers.
2
Stopped Containers: Containers in
Exited state still occupy writable layer storage.3
Unused Volumes: Named volumes no longer referenced by any container.
4
Build Cache: Intermediate layers cached from previous builds.
5
Unused Networks: Custom networks with no connected containers.
Cleanup Commands (from least to most aggressive):
•
docker image prune — removes dangling images only.•
docker container prune — removes all stopped containers.•
docker system prune — removes stopped containers + dangling images + unused networks + build cache.•
docker system prune -a — additionally removes all unused images (not just dangling).•
docker system prune -a --volumes — nuclear option: also removes unused volumes (⚠️ data loss risk).Code Implementation Example
bash
# Check current disk usage breakdown
docker system df
# TYPE TOTAL ACTIVE SIZE RECLAIMABLE
# Images 24 3 8.432GB 7.1GB (84%)
# Containers 6 1 1.2MB 1.1MB
# Local Volumes 4 2 512MB 256MB
# Build Cache - - 2.1GB 2.1GB
# Remove only dangling (untagged) images
docker image prune -f
# Remove all stopped containers
docker container prune -f
# Remove stopped containers + dangling images + unused networks + build cache
docker system prune -f
# Remove ALL unused images (not just dangling) + everything above
docker system prune -a -f
# ⚠️ CAUTION: also removes unused named volumes (potential data loss)
docker system prune -a --volumes -f
# Automate in CI — prune after each build to keep runners clean
docker system prune -af --filter "until=24h" # older than 24 hours💡docker system df shows breakdown of reclaimable space; prune commands clean up progressively.
🎯 Interview Key Takeaway
Run docker system prune -a regularly in CI/CD environments to reclaim GB of disk space from dangling images and build caches.
#Docker#Prune#Disk Cleanup#Dangling Images#Build Cache#Beginner
Back to all Docker & Deployment Questions→