I know similar questions have been asked before, but I cannot find anything that does exactly what I need. I don’t have access to the new ‘docker image prune’ command, as I cannot upgrade from my current version 1.11.2 to the latest whizzy 1.13 yet, but I need a simple script that can be run under cron to remove all images, whether dangling or not, over 1 month old.
If docker images reported “4 weeks” as “1 month” it would be simple, I could use something like
docker images | grep " [months]* "
and pipe the output to the docker rmi command. However, since it appears to report in “weeks” up to 11 weeks, and only “months” once the image is 3 months old or over, I can’t do this.
… but I’m stuck trying to figure out how to test $4,$5 to only include anything that’s either “months” or >= “4 weeks” which can then be piped through to docker rmi . Anyone any clues for a simple bit of code that can do this, please?
could also check out this: docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -e GRACE_PERIOD_SECONDS=2419200 -e FORCE_IMAGE_REMOVAL=1 -v /etc:/etc:ro spotify/docker-gc
could try https://github.com/docker/docker-py
there you can easily list all images and do determine the age in python.
if you do it in bash/awk i doubt you could still understand your script in 3 months.
docker system prune and docker image prune have the until filter. So docker image prune --all --filter until=48h would remove all (not just dangling) images that were created more than 48 hours ago. Hopefully that helps.