如何删除我的所有Docker映像?


8

有时,我只想从docker中清除所有内容,然后以干净的状态开始。有没有一种系统的方法来删除我所有的Docker容器和映像?

货柜

$ docker ps -a | head
CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS                     PORTS               NAMES
7b055e9e5f1f        fedora:latest        /bin/sh                6 days ago          Exited (0) 6 days ago                          ecstatic_colden        
40da968258eb        fedora:latest        /bin/sh                6 days ago          Exited (127) 6 days ago                        naughty_thompson       
5be4581afad6        fedora:latest        /bin/bash              6 days ago          Exited (0) 6 days ago                          kickass_wright         
4d6c33d4be6d        fedora:latest        /bin/bash              6 days ago          Exited (0) 6 days ago                          furious_fermat         
db7a29bdbc2d        fedora:latest        /bin/bash              6 days ago          Exited (0) 6 days ago                          mad_hawking            
e6b7365690ce        fedora:latest        /bin/bash              6 days ago          Exited (0) 6 days ago                          trusting_ardinghelli   
15655c21fcd3        8018e08e6a58         /bin/sh -c 'apt-get    3 weeks ago         Exited (100) 3 weeks ago                       drunk_bardeen          
607547aabbca        f02ed0c206d5         /bin/sh -c 'apt-get    3 weeks ago         Exited (100) 3 weeks ago                       cocky_franklin         
f031f28bd29a        f02ed0c206d5         /bin/sh -c 'apt-get    3 weeks ago         Exited (100) 3 weeks ago                       stoic_mcclintock      

图片

$ docker images -a | head
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker              master              266a5746d179        2 weeks ago         1.528 GB
<none>              <none>              c1f6a4b1345c        3 weeks ago         1.526 GB
<none>              <none>              6558c36ecb3f        3 weeks ago         1.526 GB
<none>              <none>              b1b7c7a6b6a7        3 weeks ago         1.24 GB
<none>              <none>              0bf9d6bf97e9        3 weeks ago         1.182 GB
<none>              <none>              48c444f1d2e7        3 weeks ago         1.182 GB
<none>              <none>              5c5c172a0038        3 weeks ago         1.182 GB
<none>              <none>              de5e09bb86c1        3 weeks ago         1.182 GB
<none>              <none>              785aaf265f18        3 weeks ago         1.182 GB

Answers:


9

最简单的方法是使用以下3个步骤:

  1. 停止所有容器
  2. 取出所有容器
  3. 删除所有图像

为此,您可以使用docker psdocker images命令功能通过此处-q--quiet切换返回ID列表。这消除了这两个命令通常返回的所有内容,并为您提供了一个不错的列表,如下所示:

默认输出
$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS                     PORTS               NAMES
7b055e9e5f1f        fedora:latest        /bin/sh                6 days ago          Exited (0) 6 days ago                          ecstatic_colden        
40da968258eb        fedora:latest        /bin/sh                6 days ago          Exited (127) 6 days ago                        naughty_thompson      
安静的输出
$ docker ps -aq
7b055e9e5f1f
40da968258eb
5be4581afad6
4d6c33d4be6d

注意:两者docker imagesdocker ps还可以使用显示所有ID 的-a--all开关。

您可以利用此输出来构造Docker命令,以利用其他Docker命令的输出,如下所示:

$ docker rm $(docker ps -qa)

打扫房子

因此,要完成删除所有内容的任务,请使用以下命令:

$ docker stop $(docker ps -qa)
$ docker rm $(docker ps -qa)
$ docker rmi $(docker images -qa)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.