August 3, 2018 · ansible Docker

Ansible - Clean up old Docker images

To keep this blog-thing alive, here comes a short post about cleaning up old Docker images using Ansible.

Prerequisites

First of you have to install the docker Python package on your Docker host.

Install with pip like this
pip install docker

or like this if you are using Python3
pip3 install docker

Note: If you have docker-py installed you have to uninstall it before installing the new docker package

Playbook

---
- name: Run docker image clean up
  hosts: linux
  tasks:
    - name: Start garbage collector container
      docker_container:
        name: docker-gc
        image: spotify/docker-gc
        state: started
        auto_remove: yes
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
          - /etc:/etc:ro