Zum Inhalt

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
# playbooks/proxmox/deploy_docker_base.yml
- hosts: docker_base
  roles:
    - role: docker_cleanup

The role provides this tag: - docker-cleanup - Execute Docker cleanup operations

Docs

Clean up Docker resources on all hosts:

1
ansible-playbook -i inventory/hosts.yml playbooks/proxmox/deploy_docker_base.yml --tags docker-cleanup

What Gets Cleaned

The role executes these cleanup operations in sequence:

  1. Image Prune (docker image prune -a -f)
  2. Removes all unused images, not just dangling ones
  3. Frees space from old image layers

  4. Builder Cache Prune (docker builder prune -a -f)

  5. Clears BuildKit cache
  6. Removes build cache from all builders

  7. System Prune (docker system prune -a -f)

  8. Removes stopped containers
  9. Removes unused networks
  10. Removes unused volumes (dangling volumes)
  11. Additional cleanup of any remaining unused resources

Output

After cleanup, the role displays reclaimed space information for each operation:

1
2
3
Image prune: Total reclaimed space: 2.3GB
Builder prune: Total reclaimed space: 1.1GB
System prune: Total reclaimed space: 500MB

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.