由于新旧镜像同名,旧镜像名称被取消,从而出现仓库名、标签均为<none>的镜像。这类无标签镜像也被称为虚悬镜像(dangling image) 。

# docker image ls -f dangling=true
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              00285df0df87        5 days ago          342 MB

一般来说,虚悬镜像已经失去了存在的价值,是可以随意删除的,可以用下面的命令删除。

# docker image prune

为了加速镜像构建、重复利用资源,Docker会利用中间层镜像。所以在使用一段时间后,可能会看到一些依赖的中间层镜像。

这些中间层镜像是其它镜像所依赖的镜像,中间层镜像不应该删除,否则会导致上层镜像因为依赖丢失而出错。实际上,这些镜像也没必要删除,因为相同的层只会存一遍,而这些镜像是别的镜像的依赖,因此并不会因为它们被列出来而多存一份,无论如何你也会需要它们。只要删除那些依赖它们的镜像后,这些依赖的中间层镜像也会被连带删除。

如果实在想删除这些中间层镜像且不想影响依赖它们的镜像,可以按照下面这么做。

# docker images
REPOSITORY          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 -a
REPOSITORY          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

除了有效的三个镜像外,其余全部都是中间层镜像。

# docker rmi 63d2cbb95230
Error response from daemon: conflict: unable to delete 63d2cbb95230 (cannot be forced) - image has dependent child images

# docker rmi -f 63d2cbb95230
Error response from daemon: conflict: unable to delete 63d2cbb95230 (cannot be forced) - image has dependent child images

# docker rmi -f a5d099eb709f
Error response from daemon: conflict: unable to delete a5d099eb709f (cannot be forced) - image has dependent child images

# docker rmi -f 64494614f22b
Error response from daemon: conflict: unable to delete 64494614f22b (cannot be forced) - image has dependent child images

即使加上-f参数也无法删除,这是因为这些中间层镜像有依赖它们的镜像,也就是jenkins:latestnginx:latest

# 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可以查看哪些镜像依赖于这些中间层镜像。

# docker save -o /docker/jenkins.tar jenkins:latest

# docker save -o /docker/nginx.tar nginx:latest

# ls /docker
jenkins.tar  nginx.tar

# docker rmi jenkins:latest

# docker rmi nginx:latest

# docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0f3e07c0138f        2 weeks ago         220MB

可以看到,在删除依赖于中间层镜像的镜像时,中间层镜像也被跟着删除了。接着导入原来依赖于中间层镜像的镜像即可。

# 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 -a
REPOSITORY          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

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐