测试环境和生产环境 部署了不同的registry服务,通过cli 操作过生产环境或测试环境push/pull image功能。本地虚拟机,docker image 残留了很多image ,现在想删除掉,发现有一个imageid(e9d3f7300f03)无法删除。
原因:docker rmi 不加-f,只能删除无tag的镜像 ;被曾经打过多处tag(被tag引用),无法直接删除,需要先删除tag,删除曾经启动过的容器,最后才能删除镜像。
此时,tag可比做引用,image为tag指向的对象。
export QCOSINDEXHOST=https://index-dev.qiniu.io/
mashaofang:Documents shaofangma$ kirk images
IMAGE DIGEST ACCESS SIZE CREATED
cs-kirk/nginx:latest sha256:c7c1149150a8f7536bd19b70ea34748bf9dfbc93e5dee677f11.. READ/WRITE 47 MB 6 months
发现对应本地docker images中的 registry:index-dev.qiniu.io/cs-kirk/nginx
export QCOSINDEXHOST= https://index.qiniu.com/
找不到。。
发现对应本地docker images中的 registry:index.qiniu.com/qcos-test/msfngix
因为一次线上事故,导致镜像丢失。
mashaofang:Documents shaofangma$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
index-dev.qiniu.io/cs-kirk/nginx latest e9d3f7300f03 6 months ago 124.8 MB
index-dev.qiniu.io/index.qbox.me/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index-dev.qiniu.io/index.qiniu.com/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index.qbox.me/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/index.qbox.me/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/ubuntu latest e9d3f7300f03 6 months ago 124.8 MB
index-dev.qiniu.io/cs-kirk/nginx e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/qcos-test/msfngix e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/ubuntu e9d3f7300f03 6 months ago 124.8 MB
执行docker rmi e9d3f7300f03,报error:
原因 :
镜像被打成不同的tag,现在多个tag引用同一个镜像,因为image id(e9d3f7300f03)都相同。估计都是ubuntu镜像,但当时是初学者,tag名字打成了msfnginx。
此时,与曾经push到远端不同registry的镜像没有关系,该命令只是docker client 删除本地image。
mashaofang:Documents shaofangma$ docker rmi e9d3f7300f03
Error response from daemon: conflict: unable to delete e9d3f7300f03 (must be forced) - image is referenced in one or more repositories
////没有tag在引用的image可以直接删除
mashaofang:Documents shaofangma$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
index.qiniu.com/ubuntu 16.04 966495178565 6 months ago 124.8 MB
index.qiniu.com/ubuntu 966495178565 6 months ago 124.8 MB
mashaofang:Documents shaofangma$ docker rmi 966495178565
Untagged: index.qiniu.com/ubuntu:16.04
Untagged: index.qiniu.com/ubuntu@sha256:14c83946c7d69370743eee1249305854a25d95f37d7d5dc0bcfd2c740140bdab
Deleted: sha256:966495178565397219d95af298ecfc2ea64586503bafb9ed5dd5a34134487cab
Deleted: sha256:7f0c52e8b6b95d7eea4d97ccc11f001a9caab1d71d31c29386dcffc61fed7801
Deleted: sha256:0f59a28dcf9826aaf6432bc5192f2789a31b2a4e28387a039d24de2689f753c0
////有tag在引用的image 可以-f强制删除,或者先删除tag 再rm 由此tag 启动过的container,最后删除image
mashaofang:Documents shaofangma$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
index-dev.qiniu.io/cs-kirk/nginx latest e9d3f7300f03 6 months ago 124.8 MB
index-dev.qiniu.io/index.qbox.me/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index-dev.qiniu.io/index.qiniu.com/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index.qbox.me/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/index.qbox.me/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/qcos-test/msfngix latest e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/ubuntu latest e9d3f7300f03 6 months ago 124.8 MB
index-dev.qiniu.io/cs-kirk/nginx e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/qcos-test/msfngix e9d3f7300f03 6 months ago 124.8 MB
index.qiniu.com/ubuntu e9d3f7300f03 6 months ago 124.8 MB
mashaofang:Documents shaofangma$ docker rmi -f e9d3f7300f03
Untagged: index-dev.qiniu.io/cs-kirk/nginx:latest
Untagged: index-dev.qiniu.io/cs-kirk/nginx@sha256:c7c1149150a8f7536bd19b70ea34748bf9dfbc93e5dee677f11362c06b12735d
Untagged: index-dev.qiniu.io/index.qbox.me/qcos-test/msfngix:latest
Untagged: index-dev.qiniu.io/index.qiniu.com/qcos-test/msfngix:latest
Untagged: index.qbox.me/qcos-test/msfngix:latest
Untagged: index.qiniu.com/index.qbox.me/qcos-test/msfngix:latest
Untagged: index.qiniu.com/qcos-test/msfngix:latest
Untagged: index.qiniu.com/qcos-test/msfngix@sha256:c7c1149150a8f7536bd19b70ea34748bf9dfbc93e5dee677f11362c06b12735d
Untagged: index.qiniu.com/ubuntu:latest
Untagged: index.qiniu.com/ubuntu@sha256:c7c1149150a8f7536bd19b70ea34748bf9dfbc93e5dee677f11362c06b12735d
Deleted: sha256:e9d3f7300f03338534ee351fafc108fa911ae1d08ef1a0cc55a880143d57f65d
Deleted: sha256:55c1cfa314a680da7086fdba81d351d837901d0c2762ba8cea17870cc9857c0d
Deleted: sha256:7b30e626a41da838be8de0ea78996c48b8cfbf132329d1842ff55482198789a7
Deleted: sha256:d72d0199021776e77ac14a764a3decd156e9f6f18b37e25b867b0f1ca42fb3d9
Deleted: sha256:fb178c6cea1453d089ce033b96eba0efdb42274138e042517ea2d5ea7bb51665
Deleted: sha256:8945af30572845a904adce2aeaf73402c842d86e99e8f10688b25cb27834110b
Deleted: sha256:ea9f151abb7e06353e73172dad421235611d4f6d0560ec95db26e0dc240642c1
#删除tag
docker rmi index-dev.qiniu.io/cs-kirk/nginx:latest
docker rmi index-dev.qiniu.io/cs-kirk/nginx@sha256:c7c1149150a8f7536bd19b70ea34748bf9dfbc93e5dee677f11362c06b12735d
#删除曾经启动过的容器
docker rm containerid
#删除没有tag的镜像
docker rmi e9d3f7300f03
docker rmi $(docker images |awk '{if($2=="") print $3}' )
注意这个不会影响已经push到远程registry(index.qiniu.com/index.qbox.me/index-dev.qiniu.io)的镜像。
mashaofang:Documents shaofangma$ kirk info
ID ACCOUNT APP REGION
1380537556 cs-kirk cs-kirk.mashaofang cs
kirk 0.5.5 is now available (current: 0.5.3). Use 'kirk upgrade' to upgrade to latest version.
mashaofang:Documents shaofangma$ kirk images
IMAGE DIGEST ACCESS SIZE CREATED
cs-kirk/nginx:latest sha256:c7c1149150a8f7536bd19b70ea34748bf9dfbc93e5dee677f11362c06b12735d READ/WRITE 47 MB 6 months
镜像唯一识别的是image id&摘要,一个镜像可以有多个标签。
因此当我们使用上面命令删除镜像的时候,实际上是在要求删除某个标签的镜像。所以首先需要做的是将满足我们要求的所有镜像标签都取消,这就是我们看到的 Untagged 的信息。因为一个镜像可以对应多个标签,因此当我们删除了所指定的标签后,可能还有别的标签指向了这个镜像,如果是这种情况,那么 Delete 行为就不会发生。所以并非所有的 docker rmi 都会产生删除镜像的行为,有可能仅仅是取消了某个标签而已。
当该镜像所有的标签都被取消了,该镜像很可能会失去了存在的意义,因此会触发删除行为。镜像是多层存储结构,因此在删除的时候也是从上层向基础层方向依次进行判断删除。镜像的多层结构让镜像复用变动非常容易,因此很有可能某个其它镜像正依赖于当前镜像的某一层。这种情况,依旧不会触发删除该层的行为。直到没有任何层依赖当前层时,才会真实的删除当前层。这就是为什么,有时候会奇怪,为什么明明没有别的标签指向这个镜像,但是它还是存在的原因,也是为什么有时候会发现所删除的层数和自己 docker pull 看到的层数不一样的源。
除了镜像依赖以外,还需要注意的是容器对镜像的依赖。如果有用这个镜像启动的容器存在(即使容器没有运行),那么同样不可以删除这个镜像。之前讲过,容器是以镜像为基础,再加一层容器存储层,组成这样的多层存储结构去运行的。因此该镜像如果被这个容器所依赖的,那么删除必然会导致故障。如果这些容器是不需要的,应该先将它们删除,然后再来删除镜像。