有时我只想清除 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
答案1
最简单的方法是执行以下 3 个步骤:
- 停止所有容器
- 移除所有容器
- 删除所有图像
为了实现这一点,您可以利用docker ps
和命令功能通过那里或开关docker images
返回 ID 列表。这消除了这两个命令通常返回的所有内容,只为您提供一个不错的列表,如下所示:-q
--quiet
$ 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 images
都docker ps
使用-a
or--all
开关来显示所有 ID。
您可以利用此输出来构建 Docker 命令,这些命令利用其他 Docker 命令的输出,如下所示:
$ docker rm $(docker ps -qa)
打扫房子
因此,要完成删除所有内容的任务,您可以使用以下命令:
$ docker stop $(docker ps -qa)
$ docker rm $(docker ps -qa)
$ docker rmi $(docker images -qa)