部署Docker----解决删除镜像问题

在Docker使用中,经常会碰到删除镜像不成功,反而让镜像变成了<none > <none>即,没名字,没Tag的镜像。


root@iZ28ikebrg6Z:/var/run# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>                 <none>              def2e0b08cbc        About an hour ago   1.37 GB
sameersbn/redmine      latest              f0bec095f291        2 hours ago         614.6 MB
sameersbn/gitlab       latest              bf5c375d9057        3 days ago          635.1 MB
<none>                 <none>              9cbaf023786c        3 days ago          192.8 MB
sameersbn/postgresql   latest              24a6064fa4cd        10 days ago         142.1 MB
<none>                 <none>              48648094d099        5 weeks ago         997.9 MB
<none>                 <none>              195fa62e92a8        6 weeks ago         496.4 MB
training/webapp        latest              31fa814ba25a        4 months ago        278.8 MB
sameersbn/redmine      2.4.2               b95b8046d47c        8 months ago        1.327 GB


好多,删又删不了。这主要是一些container使用了该image导致的,不仅仅是说现在运行的容器,之前运行的也会。
docker ps -a


可以查历史记录,然后将container commit成镜像。

我们需要删除这些依赖Image的container,当使用到该镜像的所有container都删除了,就可以直接用IMAGE_ID来删除该镜像。


root@iZ28ikebrg6Z:/var/run# docker ps -a
CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS                     PORTS               NAMES
5d6373cb79e6        224b40d4b89f             "/bin/sh -c 'apt-get   25 hours ago        Exited (0) 25 hours ago                        distracted_blackwell
aa47ba8e71c8        training/webapp:latest   "python app.py"        28 hours ago        Exited (-1) 27 hours ago                       suspicious_ardinghelli
09792841c0b4        training/webapp:latest   "python app.py"        29 hours ago        Exited (-1) 27 hours ago                       compassionate_torvalds
d8eca9742031        9cbaf023786c             "/bin/sh -c 'while t   31 hours ago        Exited (-1) 30 hours ago                       mad_jones
95d20e5442f9        9cbaf023786c             "/bin/bash"            31 hours ago        Exited (0) 31 hours ago                        stupefied_torvalds
9d40292642e6        9cbaf023786c             "/bin/echo 'hello wo   31 hours ago        Exited (0) 31 hours ago                        stoic_mclean
1078d5bd868e        9cbaf023786c             "/bin/echo 'hello wo   31 hours ago        Exited (0) 31 hours ago                        high_leakey
e88cad8893f5        9cbaf023786c             "/bin/echo hello"      31 hours ago        Exited (0) 31 hours ago                        compassionate_mclean
0949d7b6148d        9cbaf023786c             "/bin/echo 'hello wo   31 hours ago        Exited (0) 31 hours ago                        romantic_carson
e5ab0f35322f        9cbaf023786c             "/bin/bash"            31 hours ago        Exited (0) 31 hours ago                        romantic_elion
9fbad8b573d3        9cbaf023786c             "/bin/bash"            32 hours ago        Exited (0) 31 hours ago                        thirsty_davinci
9ce1789ee43d        9cbaf023786c             "/bin/echo hello"      32 hours ago        Exited (0) 32 hours ago                        insane_colden


一个个的删除
root@iZ28ikebrg6Z:/var/run# docker rm 5d63
5d63
root@iZ28ikebrg6Z:/var/run# docker rm  aa47
aa47
root@iZ28ikebrg6Z:/var/run# docker rm  0979
0979
root@iZ28ikebrg6Z:/var/run# docker rm  d8ec
d8ec
root@iZ28ikebrg6Z:/var/run# docker rm  95d
95d
root@iZ28ikebrg6Z:/var/run# docker rm  9d4
9d4
root@iZ28ikebrg6Z:/var/run# docker rm  1078
1078
root@iZ28ikebrg6Z:/var/run# docker rm  e88c
e88c
root@iZ28ikebrg6Z:/var/run# docker rm  0949
0949
root@iZ28ikebrg6Z:/var/run# docker rm  e5ab
e5ab
root@iZ28ikebrg6Z:/var/run# docker rm  9fba
9fba
root@iZ28ikebrg6Z:/var/run# docker rm  9ce1
9ce1
root@iZ28ikebrg6Z:/var/run# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


root@iZ28ikebrg6Z:/var/run# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
sameersbn/redmine      latest              f0bec095f291        3 hours ago         614.6 MB
sameersbn/gitlab       latest              bf5c375d9057        3 days ago          635.1 MB
sameersbn/postgresql   latest              24a6064fa4cd        10 days ago         142.1 MB
<none>                 <none>              48648094d099        5 weeks ago         997.9 MB
<none>                 <none>              195fa62e92a8        6 weeks ago         496.4 MB
training/webapp        latest              31fa814ba25a        4 months ago        278.8 MB
sameersbn/redmine      2.4.2               b95b8046d47c        8 months ago        1.327 GB
root@iZ28ikebrg6Z:/var/run# docker rmi 4864
Deleted: 48648094d099bf7c38c5687f4cde3f2b7d27e8a1dc45c30d8fdf8e5f42c2473e
Deleted: 4b1b0cd0c62c3e86701bf4bc4736ca403e3ddd264101d69110bd1924bf05ec2d
Deleted: cd2308a31b296d77f1ef1562432d3908582528ed13d838a3ab6702e7d19ce604
Deleted: 212c2526444a6cefedf9d985f2727bef4b50b84062400a8f1d98b19f779baf3d
Deleted: ac89032e1c4548eaf6f6bf817850431b9180a5b7f06b29e6d7f73b4b8878fe10
Deleted: dcd2c451d11f4896c31b0178c41bf764a87625004ff0048863e627474147c413
Deleted: 7eac533353c28beeab8fd4abb5a6663abc99f0cf24d3650963352f3a41ce8e29
Deleted: 11ed21fa7166f4dee1cfe406885d889f07222a458e706c9d30c28aa9e4cfc398
Deleted: cf6daa523a25b1b947138ad883d18f8501d210ae110487066a9b4ccd8fbd9c70
Deleted: 830bf73bda66fe5ac8bf9262b5be7f4cfacec7a2dd28b7d8cb61c505e68fb787
Deleted: 41176d2b465f4072fa46897aef5f4e35a1941d73a20687bb704c2bfd789a5ae3
Deleted: fa078497ab6f0be3ce013f336ff052af7eaa96cbe2e14e3e6f6352c00544ede0
Deleted: 4477fcbe8e773d2699bf50a6a24b265fbede90ab95793e9cd36f1627348424ed
Deleted: 314c98eff22790bf333b9f98ff39576d9cfbeb3f380fe839ca74ef641b30d1f6
Deleted: 01fb8a54ae0821edb5d6d30280b56c47934efa05b158b67ad3a4a4bb9a82928b
Deleted: 533b41917c87f1479dd6cc852cf8ad11ccd9ef93d5b42978b66bbb1bb30dd476
Deleted: b9c0b9efe68d7fe7caa1c9dcfd61a0aa4a3fb4e2629b7d10c9140a495a438855
Deleted: a91d7692121d15b5399bfb0b4096aa7d696d9e0e0b44269479e5666ea3b39dd9
Deleted: 37d36b47338de2dca6f957a520016d9e7ca4db1e57d6eb38824c9ba267b29db7
Deleted: c4ff7513909dedf4ddf3a450aea68cd817c42e698ebccf54755973576525c416
Deleted: cc58e55aa5a53b572f3b9009eb07e50989553b95a1545a27dcec830939892dba
Deleted: 0ea0d582fd9027540c1f50c7f0149b237ed483d2b95ac8d107f9db5a912b4240
Deleted: d92c3c92fa73ba974eb409217bb86d8317b0727f42b73ef5a05153b729aaf96b
Deleted: 9942dd43ff211ba917d03637006a83934e847c003bef900e4808be8021dca7bd
Deleted: 1c9383292a8ff4c4196ff4ffa36e5ff24cb217606a8d1f471f4ad27c4690e290
root@iZ28ikebrg6Z:/var/run# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
sameersbn/redmine      latest              f0bec095f291        3 hours ago         614.6 MB
sameersbn/gitlab       latest              bf5c375d9057        3 days ago          635.1 MB
sameersbn/postgresql   latest              24a6064fa4cd        10 days ago         142.1 MB
<none>                 <none>              195fa62e92a8        6 weeks ago         496.4 MB
training/webapp        latest              31fa814ba25a        4 months ago        278.8 MB
sameersbn/redmine      2.4.2               b95b8046d47c        8 months ago        1.327 GB
root@iZ28ikebrg6Z:/var/run# docker rmi 195f
Error response from daemon: No such image: 762df2d4d7bb52a46148cf3e5ab423b30aafd58ca8f758ea78ea2cbd1b1753b6
2014/10/17 17:26:04 Error: failed to remove one or more images
root@iZ28ikebrg6Z:/var/run# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
sameersbn/redmine      latest              f0bec095f291        3 hours ago         614.6 MB
sameersbn/gitlab       latest              bf5c375d9057        3 days ago          635.1 MB
sameersbn/postgresql   latest              24a6064fa4cd        10 days ago         142.1 MB
training/webapp        latest              31fa814ba25a        4 months ago        278.8 MB
sameersbn/redmine      2.4.2               b95b8046d47c        8 months ago        1.327 GB
root@iZ28ikebrg6Z:/var/run#

好,删除干净了。还我们一个干净的工作区间。

docker的删除有两种,一个是rm 删除容器,一个是rmi删除镜像。


rm Remove one or more containers

rmi Remove one or more images



这里有两个不同的单词,images和container。其中images很好理解,跟平常使用的虚拟机的镜像一个意思,相当于一个模版,而container则是images运行时的的状态。docker对于运行过的image都保留一个状态(container),可以使用命令docker ps来查看正在运行的container,对于已经退出的container,则可以使用docker ps -a来查看。 如果你退出了一个container而忘记保存其中的数据,你可以使用docker ps -a来找到对应的运行过的container使用docker commit命令将其保存为image然后运行。

回到之前的问题,由于image被某个container引用(拿来运行),如果不将这个引用的container销毁(删除),那image肯定是不能被删除。

所以想要删除运行过的images必须首先删除它的container。继续来看刚才的例子,


[yaxin@ubox ~] "hljs-variable">$docker ps  "hljs-operator">-a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                   NAMES
ade696        ed9c93747fe1        /bin/sh -c /usr/sbin    hours ago        Up  hours         0.0.0.0:->/tcp   test_sshd


可以看出ed9c93747fe1的image被117843ade696的container使用着,所以必须首先删除该container


[yaxin@ubox ~] "hljs-variable">$docker rm ade696
Error: container_delete: Impossible to remove a running container, please stop it first
// :: Error: failed to remove one or more containers


出现错误,这是因为该container正在运行中(运行docker ps查看),先将其关闭


[yaxin@ubox ~] "hljs-variable">$docker stop ade696
ade696



[yaxin@ubox ~] "hljs-variable">$docker rm ade696
ade696
[yaxin@ubox ~] "hljs-variable">$docker rmi ed9c93747fe1
Deleted: ed9c93747fe16627be822ad3f7feeb8b4468200e5357877d3046aa83cc44c6af
Deleted: c8a0c19429daf73074040a14e527ad5734e70363c644f18c6815388b63eedc9b
Deleted: dba4c468f0e53e5f1e5d76b8581d6740aab9f59141f783f8e263ccd7cf2a8e
Deleted: c25dc743e40af6858c34375d450851bd606a70ace5d04e231a7fcc6d2ea23cc1
Deleted: f5714a5ce764845119399ef75e652e23135cd5c54265ff8218b61ccbd33
Deleted: c8af1dc23af7a7aea0c25ba9b28bdee68caa8866f056e4f2aa2a5fa1bcb12693
Deleted: fdb2c5432e08ec6121f8dbb17e1fde17d5db4c1f149a9b702785dbf7b0f3be
Deleted: ca14274c80ac1df1333b89b2a41c0e0e3b91cd1b267b31bef852ceab3b2044
[yaxin@ubox ~]$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
CentOS65            latest              e55a74a32125         days ago          360.6 MB


可以看出,image已经被删除


本文转自 Mr_sheng 51CTO博客,原文链接:http://blog.51cto.com/sf1314/1975995


上一篇:通过pscp命令从Windows向Linux传输大文件


下一篇:c# Monitor.wait() 和sleep的区别