If you’ve been using Docker for more than five minutes, you’ve probably accumulated a graveyard of unused containers, dangling images, and anonymous volumes eating disk space. Here’s how to actually keep things tidy, plus a few tricks that’ll save you time every day.
1. The Nuclear Cleanup (Without Losing Data)
Everyone knows docker system prune -a, but that leaves volumes untouched. The full monty:
docker system prune -a --volumes -f
Caveat: This nukes all unused volumes. If you have persistent databases you might want back, use docker volume ls -qf dangling=true first to preview what’s getting deleted.
2. Single-Line Dev Environment Teardown
Working on a project and want to reset everything without manually hunting container IDs?
docker compose down -v --rmi all --remove-orphans
This tears down containers, volumes, and images in one shot. Great for CI pipelines and when you’ve borked your local state beyond repair.
3. Interactive Cleanup with fzf
Pipe Docker commands into fzf to interactively select what to kill:
docker ps -a --format '{{.ID}} {{.Image}} {{.Status}}' | fzf --multi | awk '{print $1}' | xargs docker rm
Same pattern works for images, volumes, and networks. Wrap it in a shell function and you’ve got a poor man’s Docker dashboard.
4. Limiting Log Growth (The Silent Disk Filler)
Docker defaults to JSON-file logging with no rotation. A chatty container can eat gigabytes in hours. Fix it globally in /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
Restart Docker and new containers will cap logs at 30MB total. Existing containers need recreating.
5. Quick Tip: Skip the Build Cache When You Need Fresh
Use --no-cache when you suspect layer caching is hiding a bug, but --pull is the unsung hero — it forces Docker to check for newer base images too:
docker build --no-cache --pull -t myapp .
6. Watch Resource Usage Per Container
Built-in alternative to installing ctop or Portainer:
docker stats --no-stream --format "table {{.Name}} {{.CPUPerc}} {{.MemUsage}} {{.NetIO}}"
Drop --no-stream for live monitoring. Pipe it to watch for a real-time dashboard.
Bottom line: The Docker CLI is more capable than people give it credit for. Learn the prune flags, cap your logs early, and wrap anything you type twice a week into a shell alias.
Be First to Comment