问题
在 Docker 中删除 image 时有时会遇到类似
Error response from daemon: conflict: unable to delete 6ec9a5a0fc9f (cannot be forced) - image has dependent child images
这样的错误,原因是有另外的 image FROM 了这个 image,可以使用下面的命令列出所有在指定 image 之后创建的 image 的父 image
docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=xxxxxx)
其中 xxxxxx 是报错 image 的 id,在文章开头的例子中就是 6ec9a5a0fc9f。从列表中查找到之后就可以核对并删除这些 image。
示例
1、查看我的镜像列表。
➜ ~ docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> b707620d204c 8 hours ago 179MB tingfeng/dockerfile_build_demo latest 6586e000b464 8 hours ago 179MB <none> <none> 54f305491871 10 hours ago 122MB <none> <none> 97ea9f11c94f 10 hours ago 81.2MB tingfeng/commit_test latest 58fac7144497 31 hours ago 234MB tomcat latest 61205f6444f9 2 weeks ago 467MB ubuntu latest 113a43faa138 2 weeks ago 81.2MB nginx latest cd5239a0906a 2 weeks ago 109MB hello-world latest e38bc07ac18e 2 months ago 1.85kB
2、删除none的镜像(删不掉)
➜ ~ docker rmi b707620d204c Error response from daemon: conflict: unable to delete b707620d204c (cannot be forced) - image has dependent child images ➜ ~ docker rmi 97ea9f11c94f Error response from daemon: conflict: unable to delete 97ea9f11c94f (cannot be forced) - image has dependent child images ➜ ~ docker rmi -f 54f305491871 Error response from daemon: conflict: unable to delete 54f305491871 (cannot be forced) - image has dependent child images
3、查找出所有在指定 image 之后创建的 image 的父 image,本示例看得出是同一个依赖镜像 tingfeng/dockerfile_build_demo
➜ ~ docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=b707620d204c) [tingfeng/dockerfile_build_demo:latest] sha256:6586e000b464654f420b0aa9cf6c3c61cc29edfbbe7cc5cb5d6e0fe037efaf87 sha256:b707620d204ca475f13394b14614e1f2fde986931c925cd8cc8e8bb3de7debe3 ➜ ~ docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=54f305491871) [tingfeng/dockerfile_build_demo:latest] sha256:6586e000b464654f420b0aa9cf6c3c61cc29edfbbe7cc5cb5d6e0fe037efaf87 sha256:b707620d204ca475f13394b14614e1f2fde986931c925cd8cc8e8bb3de7debe3 ➜ ~ docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=97ea9f11c94f) [tingfeng/dockerfile_build_demo:latest] sha256:6586e000b464654f420b0aa9cf6c3c61cc29edfbbe7cc5cb5d6e0fe037efaf87 sha256:b707620d204ca475f13394b14614e1f2fde986931c925cd8cc8e8bb3de7debe3
4、删除关联的依赖镜像,关联的none镜像也会被删除
➜ ~ docker rmi 6586e000b464 Untagged: tingfeng/dockerfile_build_demo:latest Deleted: sha256:6586e000b464654f420b0aa9cf6c3c61cc29edfbbe7cc5cb5d6e0fe037efaf87 Deleted: sha256:b707620d204ca475f13394b14614e1f2fde986931c925cd8cc8e8bb3de7debe3 Deleted: sha256:c241c7f781a3176d395b38a7e96eb2e0b7e031e622ad9d14eaa9098de1a063d1 Deleted: sha256:54f305491871f5609295cd6c59f304401761c7fa96bdda8a74968358c54ba402 Deleted: sha256:be4d80c4407bde1fe700983ad805a0237a148d7af04e8bf2197fc040ae654acb Deleted: sha256:97ea9f11c94fb8f76288361e37f884d639c6ea918bc6142feee2409e7ff43791
5、再次查看镜像列表
➜ ~ docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE tingfeng/commit_test latest 58fac7144497 31 hours ago 234MB tomcat latest 61205f6444f9 2 weeks ago 467MB ubuntu latest 113a43faa138 2 weeks ago 81.2MB nginx latest cd5239a0906a 2 weeks ago 109MB hello-world latest e38bc07ac18e 2 months ago 1.85kB
其他操作
# 停止所有容器 ➜ ~ docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker stop # 删除所有容器 ➜ ~ docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker rm # 删除所有none容器 ➜ ~ docker images|grep none|awk '{print $3 }'|xargs docker rmi
相关阅读
删除docker-register的镜像& none无效镜像讲解:https://segmentfault.com/a/1190000011153919
未经允许请勿转载:程序喵 » 如何解决 image has dependent child images 错误