How do you clean up Docker resources (stopped containers, unused images)?
Fast recall
Quick revision
Core answer
docker system df, then remove only confirmed unused objects. docker container prune removes stopped containers; docker image prune removes dangling images by default (-a broadens it); docker network prune removes unused networks; and build-cache/volume cleanup is separate. docker system prune combines several categories but does not remove named volumes unless explicitly requested.Interview-ready guide
Interview answer
I start cleanup by running docker system df and listing containers, images, volumes, and build cache so I know what owns the space. Then I choose the narrowest command.
docker container prune removes stopped containers. docker image prune removes dangling images by default; docker image prune -a also removes images unused by any container. docker network prune removes unused networks, and build cache and volumes have separate prune commands. docker system prune combines several categories, but named volumes are not removed by default.
The key safety point is that 'unused' is Docker's reference test, not my business-retention intent. A stopped debugging container, cached image for rollback, anonymous volume, or build cache may still be valuable. I inspect filters and the confirmation list, back up data, and avoid broad scheduled prune commands on shared production hosts without a retention policy.
How do you clean up Docker resources (stopped containers, unused images)
Direct Answer
Start with docker system df, then remove only confirmed unused objects. docker container prune removes stopped containers; docker image prune removes dangling images by default (-a broadens it); docker network prune removes unused networks; and build-cache/volume cleanup is separate. docker system prune combines several categories but does not remove named volumes unless explicitly requested.
Code Example
// Check Docker disk usage first
docker system df
// Remove all stopped containers, unused networks, dangling images, and build cache
docker system prune
// More aggressive: include unused images (not just dangling)
docker system prune -a
// Remove only stopped containers
docker container prune
// Remove only unused images
docker image prune
// Remove specific container
docker rm order-api
// Remove specific image
docker rmi order-service:v1
// Remove all stopped containers (manual approach)
docker rm $(docker ps -a -q -f status=exited)
// Force remove running container
docker rm -f order-api
// Remove image even if containers reference it
docker rmi -f order-service:v1
Common Mistakes & Pitfalls
- Not knowing Docker caches everything: Freshers run docker build dozens of times during a feature, never realizing that each failed build leaves layers behind. Weeks later, Docker uses 25GB and they think it's a bug. Running docker system df reveals the truth — build cache alone can hit 15GB.
- Confusing docker rm and docker rmi: Trying to delete an image with docker rm order-service:v1 fails with a confusing error. Remember: rm for containers (running instances), rmi for images (templates). The names are frustratingly similar.
- Pruning without checking first: Running docker system prune -a blindly deletes all unused images, forcing you to rebuild or re-download everything on next use. Always run docker system df first to see what you're about to delete and whether it's worth the rebuild cost.
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.