Docker:如何删除所有用于构build容器的图像?

有没有一种方法来识别所有用来运行容器的图像?

我指的是组成容器的所有图层。

我想删除这些图像,但不能find一种方法来指定我只想删除那些关联到一个容器。

你首先需要理解图像和容器之间的区别(你正在混合“运行”和“构build”你的问题中的一个容器):你build立一个图像,然后运行一个基于图像的容器。

如果你想回收一些空间,删除未使用的图像和图层(从旧版本的图像),如@ oliver-charlesworth说,使用docker image prune
还有docker container prune以删除停止的容器。

进一步:

要获取容器的图像:

 docker container inspect --format '{{.Image}}' <CONTAINER_ID_OR_NAME> 

然后获取图像的图层,使用docker image history

 docker image history <RESULT_OF_PREVIOUS_COMMAND> 

但是每个图层都build立在前一个图层上,因此您将不能删除构成容器的图层

 docker image rm <IMAGE_ID> Error response from daemon: conflict: unable to delete <IMAGE_ID> (cannot be forced) - image has dependent child images # OR Error response from daemon: conflict: unable to remove repository reference "<IMAGE_NAME>" (must force) - container <CONTAINER_ID is using its referenced image <IMAGE_ID>