如何删除群中未使用的docker镜像? [英] How to delete unused docker images in swarm?
问题描述
我们有一个系统,用户可以在其中安装一些Docker容器.我们对他可以安装的东西没有限制.一段时间后,我们需要清理-删除群集中所有未使用的图像.
We have a system where user may install some docker containers. We dont have a limit on what he can install. After some time, we need to clean up - delete all the images that are not in used in the swarm.
使用docker远程API的解决方案是什么?
What would be the solution for that using docker remote API?
我们的想法是让背景图像垃圾收集器线程:
Our idea is to have background image-garbage-collector thread that:
- 列出所有图像
- 尝试删除一些内容
- 如果失败,请忽略
这有意义吗?这会以某种方式影响蜂群吗?
Would this make sense? Would this affect swarm somehow?
推荐答案
更清晰的列出和(尝试)删除所有图像的方式
命令 docker rmi $(docker images -q)
与@tpbowden的回答相同,但方式更简洁. -q | --quiet
仅列出图像ID.
Cleaner way to list and (try to) remove all images
The command docker rmi $(docker images -q)
would do the same as the answer by @tpbowden but in a cleaner way. The -q|--quiet
only list the images ID.
如果执行此操作,则当用户尝试 swarm运行delete-image
时,它将:
If you do this, when the user will try to swarm run deleted-image
it will:
- 拉图像(<在此处插入网络消耗警告)
- 如果我没记错的话,只是阻塞即可,因为拉动动作不是自动进行的(<在此处插入频繁的支持请求警告,以免误解了Swarm行为/> ).
"dangling = true"过滤器:
一个有用的选项是-过滤器"dangling = true"
.执行 swarm images -q --filter"dangling = true"
将显示当前未运行的图像.
"dangling=true" filter:
A useful option is the --filter "dangling=true"
. Executing swarm images -q --filter "dangling=true"
will display not-currently-running images.
您发出的问题使我想起了计算机中的内存管理.您的真正问题是:
You issue reminds me the memory management in a computer. Your real issue is:
如何删除将来不再使用的图像?
How to remove image that won't be used in the future?
这真的很难,实际上取决于您的政策.如果您的政策是要删除旧映像,那么可能有用的命令是: docker images --format ='{{.CreatedSince}}:{{.ID}}'代码>.但是随后黑客开始...您可能需要
grep"months"
,然后 cut -d':'-f 2
.
Which is really hard and really depends on your policy. If your policy is old images are to be deleted the command that could help is: docker images --format='{{.CreatedSince}}:{{ .ID}}'
. But then the hack starts... You may need to grep "months"
and then cut -d ':' -f 2
.
整个命令的结果为:
docker rmi $(docker images --format='{{.CreatedSince}}:{{ .ID}}' G months | cut -d ':' -f 2)
请注意,此命令将需要在每个Swarm代理以及Swarm管理器上运行,而不是仅在Swarm管理器上运行.
Note that this command will need to be run on every Swarm agent as well as the Swarm manager, not only the Swarm manager.
请注意,
Be aware than a swarm pull image:tag
will not pull the image on Swarm agents! Each Swarm agent must pull the image itself. Thus deleting still used images will result in network load.
我希望这个答案会有所帮助.目前无法查询"一个月以来未使用的图像" AFAIK.
I hope this answer helps. At this time there is no mean to query "image not used since a month" AFAIK.
这篇关于如何删除群中未使用的docker镜像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!