How to remove docker images which created 7 days ago automatically?

不羁的心 提交于 2020-08-24 07:14:29

问题


Is there a way to remove built docker images some days ago?

If we check docker images, will got:

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE

There exists a CREATED item.

Researched from the official document, didn't find an option for that.


回答1:


docker image prune provides a filter to remove images until a specific date:

docker image prune -a --filter "until=$(date +'%Y-%m-%dT%H:%M:%S' --date='-15 days')"



回答2:


You can tell docker image prune to delete any images older than a given number of hours, in your case: 7 * 24h= 168h.

docker image prune -a --force --filter "until=168h"

With the option --force, there won't be any prompt so it can easily be added to a crontab to be run on a daily basis.

For this, open crontab in edit mode (crontab -e) and add the following line to run this command every day at 1am.

0 1 * * * docker image prune -a --force --filter "until=168h"



回答3:


https://docs.docker.com/engine/reference/commandline/image_prune/

docker image prune -a --force --filter "until=240h"



来源:https://stackoverflow.com/questions/50737059/how-to-remove-docker-images-which-created-7-days-ago-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!