Docker 说“...:已经存在”,即使图像已事先删除

Docker says "...: Already exists" even though image has been removed beforehand

正在学习中Docker。尝试 运行 hello-world 示例:

# docker run hello-world

成功了。

使用

删除了图像
# docker image rm -f <image-id>

成功了。

# docker images

没有显示图像。

现在,如果我再次尝试 运行 hello-world 示例,它显示如下:

它提到:

2db29710123e: Already exists

这是否意味着镜像已经存在于本地主机上,或者镜像确实存在于 Docker 注册表中,这是一个新的拉取?

如有任何见解,我们将不胜感激。

当 Docker 说 2db29710123e: Already exists 时,是的,它已经存在于您的本地计算机上。它不作为 hello-world 图像的一部分存在,因为您事先强制删除了它。但是,为什么您首先必须强制删除它?为什么定期删除还不够?这是它显示的消息,当 运行 docker image rm hello-world:

Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 4d198a310ed5 is using its referenced image feb5d9fea6a5

这意味着您仍然有一个引用该图像的容器(来自之前的 hello-world 测试)。因此,当您强制删除时,图像(标记或标签)将从 docker images 中删除,但由于现有容器,底层数据仍然存在于本地。

但是,如果您在再次 运行 hello-world 测试之前删除容器(例如 docker container rmdocker container prune),您将从存储库中提取图像,因为它在本地已经不存在了。如果您在删除图像之前删除了容器,您将不再需要强制删除。

您必须先使用以下代码停止并删除使用图像的 docker 容器:

docker kill <container-id> # kills the container

docker rm <container-id> # remove container using hello-world image

然后,终于可以删除图像了。

docker image rm hello-world  # remove the hello-world image