docker 镜像删除

Posted xiaowei123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker 镜像删除相关的知识,希望对你有一定的参考价值。

(我们以删除 php-fpm 这个镜像为例子)

一、查看镜像的 ID

[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis               latest              36304d3b4540        3 weeks ago         104 MB
docker.io/redislabs/rebloom   latest              ff42eae2d4fc        3 months ago        98.5 MB
docker.io/feisky/php-fpm      sp                  65aa12240203        20 months ago       384 MB
docker.io/feisky/nginx        sp                  915c06c73824        20 months ago       109 MB

(php-fpm 镜像ID 为:65aa12240203

 

二、执行镜像删除命令: docker rmi IMAGE ID 

[root@localhost ~]# docker rmi 65aa12240203
Error response from daemon: conflict: unable to delete 65aa12240203 (cannot be forced) - image is being used by running container 7ce6e056ebb3

这里,我们发现报错了是因为我们要删除的镜像正在被容器ID为:7ce6e056ebb3 的正在运行着。

(1)我们可以使用  docker ps -a  看一下:

 

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                      PO         NAMES
7ce6e056ebb3        feisky/php-fpm:sp   "php-fpm -F --pid ..."   About a minute ago   Up About a minute                      phpfpm
6e740aa100ef        feisky/nginx:sp     "nginx -g ‘daemon ..."   2 minutes ago        Up 2 minutes                0.tcp      nginx
ba6392c9da69        redis               "docker-entrypoint..."   12 days ago          Exited (255) 13 hours ago   1279/tcp   redis-server-6379
f29c1f1814a1        redislabs/rebloom   "docker-entrypoint..."   13 days ago          Exited (0) 13 days ago                 compassionate_aryabhata
f5ed3ec05a9e        redislabs/rebloom   "docker-entrypoint..."   13 days ago          Created                                blissful_nobel
e212a66e1964        redislabs/rebloom   "docker-entrypoint..."   13 days ago          Exited (0) 13 days ago                 dreamy_jennings
2995bb0ef7b6        redislabs/rebloom   "docker-entrypoint..."   13 days ago          Created                                festive_shirley

 

可以看到容器ID为 7ce6e056ebb3 的已经在运行中。(Up About a minute

(2)这时,我们需要先停止正在运行的容器,否则会出现如下情况:

[root@localhost ~]# docker rm 7ce6e056ebb3
Error response from daemon: You cannot remove a running container 7ce6e056ebb37062e7ed26ded5bff57a11b4d005193be35ca18d8bbc9307cb21. Stop the container before attempting removal or use -f

解决方法:(1)停止正在运行容器: docker stop 7ce6e056ebb3 

                           最后执行删除容器操作: docker rm 7ce6e056ebb3 

     (2)强制删除正在运行容器: docker rm -f 7ce6e056ebb3 

 

三、现在我们可以执行镜像删除  docker rmi IMAGE ID 

[root@localhost ~]# docker rmi 65aa12240203
Untagged: docker.io/feisky/php-fpm:sp
Untagged: docker.io/feisky/php-fpm@sha256:b3835a298915058f41cf4c01895dd6556b272f6035a77ce1dad1f0c39f37b19f
Deleted: sha256:65aa1224020319c62d5f1045ecf096797ce0db3b895c4bf00d083a97e837bb3c
Deleted: sha256:94f2d9ba77fcab9e8ef305324c0063450067753418a368d88f3983aca63e3802
Deleted: sha256:389a8a0e2506f7ddaa2bff3d1b23af2296c5fe4c1441dda68562dfd2e9f6ade0
......

 

四、检查镜像是否删除成功  docker images 

[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis               latest              36304d3b4540        3 weeks ago         104 MB
docker.io/redislabs/rebloom   latest              ff42eae2d4fc        3 months ago        98.5 MB
docker.io/feisky/nginx        sp                  915c06c73824        20 months ago       109 MB

我们可以看到 php-fpm 镜像已经删除成功。

 

以上是关于docker 镜像删除的主要内容,如果未能解决你的问题,请参考以下文章

Docker常用指令和镜像操作详解一文速学+实例代码

docker学习笔记5:利用commit命令创建镜像 和 删除本地镜像

docker镜像删除还是历史版本

docker 删除镜像

Docker实践:镜像的概念管理(查看下载删除)

Docker实践:镜像的概念管理(查看下载删除)