How to remove docker images which created 7 days ago automatically?
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"
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')"