简体   繁体   中英

How can I delete docker images older than X and not in use

I'm running out of disk space on a server and docker images shows some containers from 6 months ago but as old as 2 years ago. I'd like to remove all the ones older than 8 months. What magic can I add to docker rmi $(MAGIC) that'll accomplish this?

You can use docker images prune which will delete all images that are not being used by any container, combining it with filter makes you able to delete images with certain conditions, according to this docs where it says:

You can limit which images are pruned using filtering expressions with the --filter flag. For example, to only consider images created more than 24 hours ago

$ docker image prune -a --filter "until=24h"

In case you need to delete images older than 8 months the command would be:

$ docker image prune -a --filter "until=5840h"

Update: A more flexible version of the command above in case you need to change the value of until . Given that 1 month equals to 730 hour approximately and we need to delete images older than 8 months then we can use the command as the following and let the bash do the math:

$ docker image prune -a --filter "until=$((8 * 730))h"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM