Docker Registry 2.0 - 如何删除未使用的图像? [英] Docker Registry 2.0 - how to delete unused images?

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

问题描述

我们将我们的私人码头服务器注册表更新为官方注册表2.0。此版本现在可以删除由主题标签识别的docker图像(请参阅 https://docs.docker)。 com / registry / spec / api /#deleted-an-image ),但是我仍然没有看到清理旧图像的方法。



CI服务器不断地生成新的图像,我需要一种从私有注册表中删除所有不再被命名标签识别的图像的方法。



如果没有内置 - 为了实现这一点,我认为自定义脚本可能有效,但是我没有看到一个v2 API方法来列出所有存储的图像的标签。



如何保持我的私人注册表清洁?任何提示?

解决方案

这是可行的,虽然丑陋。您需要运行(我认为)注册表2.3或更高版本,并已启用删除( REGISTRY_STORAGE_DELETE_ENABLED = True env var或等效)。下面的示例命令假定在 / srv / docker-registry 中的本地文件存储,但是如果某些等同物不能为其他存储后端进行烹饪,我会感到惊讶。 / p>

对于您希望整理的每个存储库,您需要枚举不再需要的摘要引用。最简单的方法是使用每个标签,使用最新作为示例,您可以执行以下操作:

  ls / srv / docker-registry / v2 / repositories /< repo> / _ manifestests / tags / index / latest / index / sha256 -t | tail -n +3 

这将列出推送到最新的标签。或者,如果你不太在乎标签,但只是想保留最后几个参考文献,你可以这样做:

  ls / srv / docker-registry / v2 / repositories /< repo> / _清单/修订版/ sha256 -t | tail -n +3 

然后,您只需删除不需要的引用:

  for ls / srv / docker-registry / v2 / repositories /< repo> / _ manifestests / tags / index / latest / index / sha256 -t |尾+3);做卷曲-X DELETE https://注册表:5000 / v2 /< repo> / manifestests / sha256:$ hash;完成

最后,您需要执行GC运行,因为注册表实现软删除,其中实际上不删除任何东西,它只会使其不可用:

  docker exec docker-registry / bin / registry垃圾收集/path/to/config.yml 

是的,这是凌乱的因为没有API方法枚举与给定标签相关联的所有摘要,而是cookie的崩溃方式。


We updated our private docker registry to the official Registry 2.0. This version can now delete docker images identified by a hashtag (see https://docs.docker.com/registry/spec/api/#deleting-an-image) but I still don't see a way to cleanup old images.

As our CI server is continously producing new images, I would need a method to delete all images from the private registry which are no longer identified by a named tag.

If there's no built-in way to achieve this, I think a custom script could possibly work, but I don't see a v2 API method either to list all stored hashtags of an image..

How can I keep my private registry clean? Any hints?

解决方案

This is doable, although ugly. You need to be running (I think) registry 2.3 or greater, and have enabled deleting (REGISTRY_STORAGE_DELETE_ENABLED=True env var or equivalent). The example commands below assume a local filestore in /srv/docker-registry, but I'd be surprised if something equivalent couldn't be cooked up for other storage backends.

For each repository you wish to tidy up, you need to enumerate the digest references that are no longer required. The easiest way to do this is per-tag, using latest as an example in this case, you'd do something like:

ls /srv/docker-registry/v2/repositories/<repo>/_manifests/tags/index/latest/index/sha256 -t | tail -n +3

This will list all but the three most recent digests pushed to the latest tag. Alternately, if you don't care about tags so much, but just want to keep the last few references pushed, you can do:

ls /srv/docker-registry/v2/repositories/<repo>/_manifests/revisions/sha256 -t | tail -n +3

Then, you just delete the references you don't need:

for hash in ls /srv/docker-registry/v2/repositories/<repo>/_manifests/tags/index/latest/index/sha256 -t | tail -n +3); do curl -X DELETE https://registry:5000/v2/<repo>/manifests/sha256:$hash; done

Finally, you need to do a GC run, because the registry implements "soft deletes", which doesn't actually delete anything, it just makes it unavailable:

docker exec docker-registry /bin/registry garbage-collect /path/to/config.yml

Yes, this is all messy as hell, grovelling around in the backend storage, because there's no API method to enumerate all digests associated with a given tag, but that's the way the cookie crumbles.

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

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