Docker Cleanup role
Remove unused Docker images, containers, volumes, networks, and build cache to reclaim disk space on Docker hosts.
Usage
Include the role in your playbook:
1 2 3 4 | |
The role provides this tag:
- docker-cleanup - Execute Docker cleanup operations
Docs
Clean up Docker resources on all hosts:
1 | |
What Gets Cleaned
The role executes these cleanup operations in sequence:
- Image Prune (
docker image prune -a -f) - Removes all unused images, not just dangling ones
-
Frees space from old image layers
-
Builder Cache Prune (
docker builder prune -a -f) - Clears BuildKit cache
-
Removes build cache from all builders
-
System Prune (
docker system prune -a -f) - Removes stopped containers
- Removes unused networks
- Removes unused volumes (dangling volumes)
- Additional cleanup of any remaining unused resources
Output
After cleanup, the role displays reclaimed space information for each operation:
1 2 3 | |
Safety
All cleanup operations use the -f (force) flag, so no confirmation is required. Be aware that this will remove:
- All stopped containers
- All images not used by at least one container
- All unused volumes
- All unused networks
- All build cache
Note: Active containers and their images are never removed.
