From 2eae7c68db07d78a83e7bddb51d72667d9806f83 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Sat, 21 May 2022 19:45:46 +0200 Subject: [PATCH] Prune the dangling images daily. --- defaults/main.yml | 2 ++ files/docker-prune-dangling-images.sh | 4 ++++ tasks/docker_setup.yml | 33 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 files/docker-prune-dangling-images.sh diff --git a/defaults/main.yml b/defaults/main.yml index 2be6aa2..e8dc435 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -12,6 +12,8 @@ docker_swarm_worker_hostname: '{{ ansible_fqdn }}' docker_swarm_allowed_hosts: - '0.0.0.0/0' +docker_prune_dangling_images: true + # Set this to one of the manager hostnames to be able to initialize the cluster docker_swarm_manager_main_node_hostname: localhost docker_swarm_manager_main_node_address: 127.0.0.1 diff --git a/files/docker-prune-dangling-images.sh b/files/docker-prune-dangling-images.sh new file mode 100644 index 0000000..74cc4f6 --- /dev/null +++ b/files/docker-prune-dangling-images.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +docker image prune --force > /var/log/docker-prune.log 2>&1 +exit 0 diff --git a/tasks/docker_setup.yml b/tasks/docker_setup.yml index 02152b5..b053606 100644 --- a/tasks/docker_setup.yml +++ b/tasks/docker_setup.yml @@ -40,6 +40,39 @@ when: docker_install tags: [docker] +- name: Cron job that prunes the dangling images + block: + - name: Install a script that prunes the docker dangling images + copy: + src: docker-prune-dangling-images.sh + dest: /usr/local/sbin/docker-prune-dangling-images + owner: root + group: root + mode: 0744 + + - name: Install a cron job that prunes the docker images + cron: + name: Prune docker images + cron_file: docker-prune-images + special_time: daily + user: root + state: present + job: "/usr/local/sbin/docker-prune-dangling-images" + when: docker_prune_dangling_images + + - name: Disable the cron job that prunes the docker images + cron: + name: Prune docker images + cron_file: docker-prune-images + special_time: daily + user: root + state: absent + job: "/usr/local/sbin/docker-prune-dangling-images" + when: not docker_prune_dangling_images + + when: docker_install + tags: [docker, docker_prune_images] + - block: - name: Remove the docker user user: name=docker state=absent