如何删除早于X且未使用的Docker映像 [英] How can I delete docker images older than X and not in use

查看:71
本文介绍了如何删除早于X且未使用的Docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器磁盘空间不足,并且 docker images 显示了6个月前但2年前的容器。我想删除所有8个月以上的邮件。我可以在 docker rmi $(MAGIC)中添加什么魔术来实现这一目标?

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?

推荐答案

您可以使用 docker images prune 删除所有容器未使用的所有图像,并将其与 filter 使您能够根据特定条件删除图像,具体方法请参见此docs 其中表示:

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:

您可以使用-filter 标志。例如,仅考虑24小时前创建的映像

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"

如果您需要删除8个月以上的图像,则命令为:

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

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

更新:
以上命令的更灵活版本,以防您需要更改的值,直到。假设 1 个月大约等于 730 小时,我们需要删除早于 8的图像个月,然后我们可以使用以下命令,让bash进行数学运算:

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"

这篇关于如何删除早于X且未使用的Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆