因为新旧镜像同名,旧镜像名称被取消,从而出现仓库名、标签均为的镜像。这类无标签镜像也被称为虚悬镜像(dangling image) 。html
# docker image ls -f dangling=trueREPOSITORY TAG IMAGE ID CREATED SIZE<none> <none> 00285df0df87 5 days ago 342 MB
通常来讲,虚悬镜像已经失去了存在的价值,是能够随意删除的,能够用下面的命令删除。nginx
# docker image prune
为了加速镜像构建、重复利用资源,Docker会利用中间层镜像。因此在使用一段时间后,可能会看到一些依赖的中间层镜像。docker
这些中间层镜像是其它镜像所依赖的镜像,中间层镜像不该该删除,不然会致使上层镜像由于依赖丢失而出错。实际上,这些镜像也不必删除,由于相同的层只会存一遍,而这些镜像是别的镜像的依赖,所以并不会由于它们被列出来而多存一份,不管如何你也会须要它们。只要删除那些依赖它们的镜像后,这些依赖的中间层镜像也会被连带删除。centos
若是实在想删除这些中间层镜像且不想影响依赖它们的镜像,能够按照下面这么作。ide
# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE jenkins latest 369817e36fc5 22 hours ago 1.24GB nginx latest 8b0a86bf5764 24 hours ago 418MB centos latest 0f3e07c0138f 2 weeks ago 220MB# docker images -aREPOSITORY TAG IMAGE ID CREATED SIZE<none> <none> 64494614f22b 22 hours ago 1.24GB<none> <none> 7ac19a9cd510 22 hours ago 1.24GB<none> <none> fc2251321ab5 22 hours ago 1.24GB<none> <none> 63d2cbb95230 22 hours ago 1.24GB jenkins latest 369817e36fc5 22 hours ago 1.24GB<none> <none> 6ed44c1bb8da 22 hours ago 1.24GB<none> <none> caff29bcf1e2 22 hours ago 1.24GB<none> <none> 4e84b30eda89 22 hours ago 694MB<none> <none> 47aa632b5427 22 hours ago 616MB<none> <none> de63e9ae754a 24 hours ago 418MB nginx latest 8b0a86bf5764 24 hours ago 418MB<none> <none> bdb2c14e7f0b 24 hours ago 418MB<none> <none> ca8b12c4da26 24 hours ago 418MB<none> <none> 4a69b2a70168 24 hours ago 418MB<none> <none> a5d099eb709f 45 hours ago 220MB centos latest 0f3e07c0138f 2 weeks ago 220MB
除了有效的三个镜像外,其他所有都是中间层镜像。3d
# docker rmi 63d2cbb95230Error response from daemon: conflict: unable to delete 63d2cbb95230 (cannot be forced) - image has dependent child images# docker rmi -f 63d2cbb95230Error response from daemon: conflict: unable to delete 63d2cbb95230 (cannot be forced) - image has dependent child images# docker rmi -f a5d099eb709fError response from daemon: conflict: unable to delete a5d099eb709f (cannot be forced) - image has dependent child images# docker rmi -f 64494614f22bError response from daemon: conflict: unable to delete 64494614f22b (cannot be forced) - image has dependent child images
即便加上-f
参数也没法删除,这是由于这些中间层镜像有依赖它们的镜像,也就是jenkins:latest
及nginx:latest
。code
# docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=fc)[jenkins:latest] sha256:369817e36fc5250dcc15b320dae5fde9da2caa0d28262458f98d96550b6ccb6b sha256:7ac19a9cd5108d532d4f8e29289cd3bd61721a9c52f4a5ad9271a4aa372a6505# docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=4e)[jenkins:latest] sha256:369817e36fc5250dcc15b320dae5fde9da2caa0d28262458f98d96550b6ccb6b sha256:7ac19a9cd5108d532d4f8e29289cd3bd61721a9c52f4a5ad9271a4aa372a6505# docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=47)[jenkins:latest] sha256:369817e36fc5250dcc15b320dae5fde9da2caa0d28262458f98d96550b6ccb6b sha256:7ac19a9cd5108d532d4f8e29289cd3bd61721a9c52f4a5ad9271a4aa372a6505
slice=
后面跟中间层镜像的id能够查看哪些镜像依赖于这些中间层镜像。orm
# docker save -o /docker/jenkins.tar jenkins:latest# docker save -o /docker/nginx.tar nginx:latest# ls /dockerjenkins.tar nginx.tar# docker rmi jenkins:latest# docker rmi nginx:latest# docker images -aREPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0f3e07c0138f 2 weeks ago 220MB
能够看到,在删除依赖于中间层镜像的镜像时,中间层镜像也被跟着删除了。接着导入原来依赖于中间层镜像的镜像便可。htm
# docker load < /docker/jenkins.tar fa0b0907a72e: Loading layer [==================================================>] 397.8MB/397.8MB 24878c480cae: Loading layer [==================================================>] 78.25MB/78.25MB 156c821fe3b0: Loading layer [==================================================>] 553MB/553MB Loaded image: jenkins:latest# docker load < /docker/nginx.tar b21caef323db: Loading layer [==================================================>] 208.8MB/208.8MB 4d1228e0ab7c: Loading layer [==================================================>] 5.632kB/5.632kB Loaded image: nginx:latest# docker images -aREPOSITORY TAG IMAGE ID CREATED SIZE jenkins latest 369817e36fc5 22 hours ago 1.24GB nginx latest 8b0a86bf5764 24 hours ago 418MB centos latest 0f3e07c0138f 2 weeks ago 220MB