从私有注册表中删除标记的Docker映像


10

如何从私有Docker注册表中的映像中删除错误添加的标签?选项-rmi似乎不适用于Docker 1.9.1中的远程映像。

user@ubuntu-user:~$ docker --version  
Docker version 1.9.1, build a34a1d5

user@ubuntu-user:~$ docker search myregistry:5000/user/image
NAME                                                                     
myregistry:5000/user/image:20160119                                         
myregistry:5000/user/image:20160119-20160120        

user@ubuntu-user:~$ docker rmi myregistry:5000/user/image:20160119-20160120
Error response from daemon: could not find image: no such id: myregistry:5000/user/image:20160119-20160120
Error: failed to remove images: [myregistry:5000/user/image:20160119-20160120]

Answers:


9

到目前为止,似乎还没有一种简单的方法可以从注册表中删除图像,并且它似乎是注册表2.1里程碑的一项功能。

我们今天无法解决的选择之一

anovil@ubuntu-anovil remove-registry]$ curl -X DELETE localhost:5000/v2/alpine/manifests/v1
{"errors":[{"code":"UNSUPPORTED","message":"The operation is unsupported."}]}
[anovil@ubuntu-anovil remove-registry]$ 

是从注册表本身中手动将其删除。为了避免意外删除错误的文件,我使用github中的此脚本对其进行了测试。对于该脚本的工作方式,我不做任何保证(尽管在测试之前,我已对其进行了快速检查)。

所以,我做了一个测试,这似乎可行:)

[1]我假设您正在使用docker本身运行注册表。

[anovil@ubuntu-anovil remove-registry]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
88f8e1a1d7a7        registry:2          "/bin/registry /etc/d"   37 minutes ago      Up 37 minutes       0.0.0.0:5000->5000/tcp   registry
[anovil@ubuntu-anovil remove-registry]$ 

[2]我创建了一个仅FROM alpine包含内容的最小Dockerfile,并创建了alpine:v1并将其推送到在localhost:5000上运行的私有注册表。从注册表中查询它,它按预期返回。

[anovil@ubuntu-anovil remove-registry]$ curl -X GET localhost:5000/v2/alpine/tags/list
{"name":"alpine","tags":["v1"]}
[anovil@ubuntu-anovil remove-registry]$ 

[3]然后我使用登录到注册表docker exec并检查磁盘使用情况,然后再进行实验

root@88f8e1a1d7a7:/# du -sch /var/lib/registry/                     
2.5M    /var/lib/registry/
2.5M    total
root@88f8e1a1d7a7:/# 

[4]回到主机后,我将一个沉重的文件(mongodb.tgz)复制到了我的容器中,并创建了一个构建并推送的版本v2。

[anovil@ubuntu-anovil remove-registry]$ docker build -t localhost:5000/alpine:v2 .
Sending build context to Docker daemon 61.99 MB
Step 1 : FROM alpine
 ---> 2314ad3eeb90
Step 2 : COPY mongodb.tgz /mongodb.tgz
 ---> d7c7645a3fe2
Successfully built d7c7645a3fe2
[anovil@ubuntu-anovil remove-registry]$ docker push localhost:5000/alpine:v2
The push refers to a repository [localhost:5000/alpine] (len: 1)
d7c7645a3fe2: Pushed 
5ff05309724e: Image already exists 
v2: digest: sha256:7bea1ec2910170bd88412b622aee6129791673cf1fd8c0e1e34f15ec26428774 size: 4467
[anovil@ubuntu-anovil remove-registry]$ 

[5]在注册表中再次检查大小后,它已增加到62MB:

root@88f8e1a1d7a7:/# du -sch /var/lib/registry/                                                                               
62M /var/lib/registry/
62M total
root@88f8e1a1d7a7:/# 

[6]为了运行delete_docker_registry_image,您需要将脚本放入托管注册表的容器中,其中一种选择是使用curl。另外,此脚本需要jq

root@88f8e1a1d7a7:/# apt-get update && apt-get install -y curl jq
...
root@88f8e1a1d7a7:/#

[7]运行脚本,先尝试使用--dry-run选项,不要忘记版本标记(在这种情况下为v2),这也是一个很好的选择-h

root@88f8e1a1d7a7:/# delete_docker_registry_image --image alpine:v2 --dry-run
DRY_RUN: would have deleted tag directory: repositories/alpine/_manifests/tags/v2
DRY_RUN: would have deleted manifest revision: repositories/alpine/_manifests/revisions/sha256/7bea1ec2910170bd88412b622aee6129791673cf1fd8c0e1e34f15ec26428774
DRY_RUN: would have deleted directory: blobs/sha256/e2/e2cc9aed084e01fa5cf93c09121035ac4d712113425ae68b678c28591beec5c6
DRY_RUN: would have deleted directory: blobs/sha256/7a/7ada67971e952e353ab14d8f9bdd4e41e4c41099b05a5da09f2700b51d93908a
DRY_RUN: would have deleted directory: blobs/sha256/7b/7bea1ec2910170bd88412b622aee6129791673cf1fd8c0e1e34f15ec26428774
DRY_RUN: would have deleted layer metadata directory: repositories/alpine/_layers/sha256/e2cc9aed084e01fa5cf93c09121035ac4d712113425ae68b678c28591beec5c6
root@88f8e1a1d7a7:/# delete_docker_registry_image --image alpine:v2          
root@88f8e1a1d7a7:/#

[8]瞧!

root@88f8e1a1d7a7:/# du -sch /var/lib/registry/                     
2.5M    /var/lib/registry/
2.5M    total
root@88f8e1a1d7a7:/#  


0

就在最近我自己遇到了这个问题,但随后想到了为什么要删除,我将重新发布一个旧版本:

docker push my/image:1.0.0
docker push my/image:1.0.1 # This is broken

docker tag  my/image:1.0.0 my/image:1.0.2
docker push my/image:1.0.2

损坏的图像仍将存在,但是由于有可用的“较新”版本,因此不太可能有人使用它。显然,最好向前修复,但是在紧急情况下,这是一个快速的解决方案。

如果您要删除它的原因是它具有秘密或您不想泄露的东西,则其他解决方案会更好,但是假设现在知道了并进行更改(密码,密钥等)。


1
因此,随着更多图像被推送但从未清除,所需的存储空间会越来越大吗?
emmdee '18

@emmdee好,我的意思是……但是,请记住,从磁盘角度来看,它仅存储修订版之间的增量,就像git一样。对于最终为1 GB的映像,这并不意味着每次发布新版本时,磁盘上就会再使用1 GB。
Michael Butler
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.