docker删除镜像的时候报错--image has dependent child images

背景

偶然间发现服务器上有很多镜像占用不少空间,想清理一下。
结果直接进行删除报错:

docker rmi 8f5116cbc201

Error response from daemon: conflict: unable to delete 8f5116cbc201 (cannot be forced) - image has dependent child images

然后上网需求方法,主流的方法有两种

方法一:强制删除镜像

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

以失败告终。。。

方法二:批量删除容器,再删除镜像

# 停止所有容器
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

还是以失败告终。。。。。

原因


搜了很久,发现其实是因为TAG的问题,即有其他 image FROM 了这个 image,可以使用下面的命令列出所有在指定 image 之后创建的 image 的父 image

方案

先查询依赖

docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=XXX)    # XXX指镜像ID


然后根据根据TAG删除容器

docker rm REPOSITORY:TAG

补充

docker none镜像

有效的 none 镜像
Docker文件系统的组成,docker镜像是由很多 layers组成的,每个 layer之间有父子关系,所有的docker文件系统层默认都存储在/var/lib/docker/graph目录下,docker称之为图层数据库。

最后做一个总结< none>:< none> 镜像是一种中间镜像,我们可以使用docker images -a来看到,他们不会造成硬盘空间占用的问题(因为这是镜像的父层,必须存在的),但是会给我们的判断带来迷惑。

无效的 none 镜像

另一种类型的 < none>:< none> 镜像是dangling images ,这种类型会造成磁盘空间占用问题。

像Java和Golang这种编程语言都有一个内存区,这个内存区不会关联任何的代码。这些语言的垃圾回收系统优先回收这块区域的空间,将他返回给堆内存,所以这块内存区对于之后的内存分配是有用的

docker的悬挂(dangling)文件系统与上面的原理类似,他是没有被使用到的并且不会关联任何镜像,因此我们需要一种机制去清理这些悬空镜像。

我们在上文已经提到了有效的< none>镜像,他们是一种中间层,那无效的< none>镜像又是怎么出现的?这些 dangling镜像主要是我们触发 docker build 和 docker pull命令产生的。

使用下面的命令可以清理
docker rmi $(docker images -f “dangling=true” -q)
docker没有自动垃圾回收处理机制,未来可能会有这方面的改进,但是目前我们只能这样手动清理(写个脚本就好)。

猜你喜欢

转载自www.cnblogs.com/111testing/p/11208086.html