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

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

如何从私有 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]

答案1

截至目前,似乎没有简单的方法可以从注册表中删除图像,看起来这是注册表的一个功能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] 我创建了一个只有内容的最小 Dockerfile FROM alpine,并创建了 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:/#  

答案2

最近我自己也遇到了这个问题,但后来想,为什么要删除,我只需重新发布一个旧版本:

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

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

如果您想要删除它的原因是它包含秘密或您不想泄露的某些内容,那么其他解决方案会更好,但要假设现在已知它是什么并进行更改(密码、密钥等)。

相关内容