docker本身没有批量删除的命令,要是容器或者镜像的数量太多,操作比较麻烦
解决办法:
先查询,在删除,使用xargs传参,将查询到的id,进行删除,达到批量删除的目的。(也可以用`` 或者$( ),这两个都是用作命令替换的,也就是出来的结果,是里面命令的执行结果)
前提准备:
准备部分实验镜像,容器
查看容器,镜像的命令:
查看容器
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07ff4d9bb34f centos:7.8.2003 "/bin/bash" 18 hours ago Exited (0) 18 hours ago infallible_cartwright
52b17429d6c5 centos:8.2.2004 "/bin/bash" 18 hours ago Exited (0) 18 hours ago vibrant_spence
6e907e98cabe centos:7.9.2009 "/bin/bash" 18 hours ago Exited (0) 18 hours ago sleepy_cori
944060fe0cab centos:7 "/bin/bash" 18 hours ago Exited (0) 18 hours ago competent_williams
009f399c688e centos "/bin/bash" 18 hours ago Exited (0) 18 hours ago focused_panini
7ae3dcc48494 ubuntu "/bin/bash" 19 hours ago Up 19 hours 0.0.0.0:49153->80/tcp web
99cc9cbb328c ubuntu "/bin/bash" 44 hours ago Exited (127) 44 hours ago brave_ellis
e58f0cc9d8da ubuntu "echo hello-world" 44 hours ago Exited (0) 44 hours ago hopeful_cannon
-a选项显示所有,加-p选项只输出容器ID
查看镜像
[root@localhost ~]# docker images (或者docker image ls -a 效果一样)
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 9 days ago 209MB
ubuntu latest f643c72bc252 3 weeks ago 72.9MB
centos 7 8652b9f0cb4c 4 weeks ago 204MB
centos 7.9.2009 8652b9f0cb4c 4 weeks ago 204MB
centos 8.2.2004 831691599b88 6 months ago 215MB
centos 7.8.2003 afb6fca791e0 7 months ago 203MB
加-p选项 只输出镜像ID
单独删除的命令
docker container rm 容器id
#删除容器 可简写: docker rm 容器id
docker image rm 镜像ID
#删除镜像 可简写: docker rmi 镜像ID
注意想要删除镜像,必须先删除使用此镜像的容器,否则会报错(Error response from daemon: conflict: unable to delete XXX (must be forced) - image is being used by stopped container XXXX)
按条件批量删除容器,镜像
准备的镜像有centos的,也有ubuntu的,现在目的只删除centos的镜像
1.批量停止centos容器
[root@localhost ~]# docker container ls -a |grep centos |awk '{print $1}' |xargs docker container stop
07ff4d9bb34f
52b17429d6c5
944060fe0cab
009f399c688e
使用docker container stop `docker container ls -a |grep centos |awk '{print $1}'`
或者docker container stop $(docker container ls -a |grep centos |awk '{print $1}')
效果是一样的
此命令第一步是查询所有容器,然后过滤centos的容器,在将过滤出来的容器取出容器ID,最后关闭这些容器。这样就达到了只关闭centos容器的效果。
(如果需要关闭所有容器,直接执行docker container ls -a -q |xargs docker container stop 就可以了)
2.批量删除centos容器
批量删除和批量关闭差不多,就是将命令停止(stop)换成删除(rm)
[root@localhost ~]# docker container ls -a |grep centos |awk '{print $1}' |xargs docker container rm
07ff4d9bb34f
52b17429d6c5
944060fe0cab
009f399c688e
(如果需要删除所有容器,直接执行docker container ls -a -q |xargs docker container rm 就可以了)
3.批量删除centos镜像
在删除了镜像创建的容器后,就可以进行删除镜像了
镜像中有自带的条件查询参数,后面带上条件就好
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 9 days ago 209MB
ubuntu latest f643c72bc252 3 weeks ago 72.9MB
centos 7.9.2009 8652b9f0cb4c 4 weeks ago 204MB
centos 8.2.2004 831691599b88 6 months ago 215MB
centos 7.8.2003 afb6fca791e0 7 months ago 203MB
[root@localhost ~]# docker images centos
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 9 days ago 209MB
centos 7.9.2009 8652b9f0cb4c 4 weeks ago 204MB
centos 8.2.2004 831691599b88 6 months ago 215MB
centos 7.8.2003 afb6fca791e0 7 months ago 203MB
批量删除
[root@localhost ~]# docker images centos -q |xargs docker rmi
Untagged: centos:latest
Untagged: centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
Deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859
Untagged: centos:8.2.2004
Untagged: centos@sha256:4062bbdd1bb0801b0aa38e0f83dece70fb7a5e9bce223423a68de2d8b784b43b
Deleted: sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f
Deleted: sha256:eb29745b8228e1e97c01b1d5c2554a319c00a94d8dd5746a3904222ad65a13f8
Untagged: centos:7.8.2003
Untagged: centos@sha256:8540a199ad51c6b7b51492fa9fee27549fd11b3bb913e888ab2ccf77cbb72cc1
Deleted: sha256:afb6fca791e071c66276202f8efca5ce3d3dc4fb218bcddff1bc565d981ddd1e
Deleted: sha256:fb82b029bea0a2a3b6a62a9c1e47e57fae2a82f629b2d1a346da4fc8fb53a0b6
Untagged: centos:7.9.2009
Untagged: centos@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Deleted: sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf
Deleted: sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02
删除成功
利用查询语句获取容器(镜像)ID,在通过传参达到批量删除的效果