Skip to content

Instantly share code, notes, and snippets.

@blackandred
Last active October 27, 2021 14:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save blackandred/3b90877b944887949ad30cc22026502c to your computer and use it in GitHub Desktop.
Save blackandred/3b90877b944887949ad30cc22026502c to your computer and use it in GitHub Desktop.
Ansible + Docker: Clean up unused, old containers and images to free up the disk space
#
# Created for free for the anarchist movement around the world.
# See iwa-ait.org, zsp.net.pl, wolnosciowiec.net
#
- name: Schedule a clean up for Docker images
become: yes
cron:
name: "docker-clean-up-images"
minute: "0"
hour: "22"
job: "docker rmi $(docker images | awk '/^<none>/ { print $3 }') 2> /dev/null"
day: "10"
- name: Schedule a clean up for Docker unused containers
become: yes
cron:
name: "docker-clean-up-containers"
minute: "0"
hour: "22"
job: "docker ps --filter 'status=exited' | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm 2> /dev/null"
day: "5"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment