学习目的:
在实习期间,为了测试在镜像中C++和python的一些功能和bug,不得不反复打出许多容器。
每次修复完一个bug,运行docker ps -a能列出七八个容器。一个容器占用的磁盘空间小则几百兆,多则几个G。所以为了节省磁盘空间,不得不把多余的容器删除。
那么问题就来了,删除一个容器需要运行两个命令
docker stop container_id // container_id是容器的id,通过docker ps -a查看
docker rm container_id
要运行这两个命令,则要反复列出容器列表,复制粘贴... 机械运动和敲键盘很麻烦
但awk好像可以把命令运行结果打印和添加格式
好好利用awk命令,一键获取所有容器id,一键删除,是不是很方便?
docker ps -a
可以看到STATUS下位Exited都需要删除,对应container_id也是我们需要格式化和列出的的
e3145ee5d01f c63f2ae5b185 "./AlgorithmicIntegr…" About a minute ago Up About a minute admiring_hoover
286c51de5dd0 c63f2ae5b185 "./AlgorithmicIntegr…" 22 minutes ago Exited (0) 21 minutes ago admiring_robinson
e0cec759dc54 c63f2ae5b185 "./AlgorithmicIntegr…" 24 minutes ago Up 24 minutes cool_darwin
57cb412fcdb8 ec024683a9cb "./AlgorithmicIntegr…" 45 hours ago Exited (0) 45 hours ago xenodochial_bassi
bea7512b32d1 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 4 days ago charming_faraday
f6493cdd7a4c 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 7 days ago gifted_beaver
d6c2fbeece4d 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 7 days ago nervous_vaughan
112f65839ad0 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 7 days ago confident_jones
3091e5e0e0df 7c885251d1f5 "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 2 weeks ago busy_ganguly
01abc933581a 7c885251d1f5 "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 2 weeks ago exciting_lumiere
08ec1ac6c986 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (0) 2 weeks ago competent_rhodes
0d1890d0c976 7c885251d1f5 "./AlgorithmicIntegr…" 2 weeks ago Exited (0) 2 weeks ago confident_solomon
eebb137e716e 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (0) 2 weeks ago lucid_dirac
d9b0361a0214 smartsteel:1.0 "dotnet HttpApi.dll" 2 months ago Up 2 weeks 0.0.0.0:30888->80/tcp DOCKER_HTTPAPI_CONTAINER_NAME
ef3dffdd96b3 mysql:5.7 "docker-entrypoint.s…" 2 months ago Up 2 weeks 33060/tcp, 0.0.0.0:30887->3306/tcp mysql5.7
利用管道化命令,列出所有退出的容器信息
docker ps -a | awk '/Exited/'
# awk 'Exited' 可以列出输入信息中,满足包含字符串’Exited‘的行;\\之间为正则表达式
命令管道化由 '|' 实现, '|' 之前的结果为 '|' 之后的输入,显然我们需要将所有容器信息作为awk命令的输入,运行结果:
oot@cv-note3:~# docker ps -a | awk '/Exited/'
286c51de5dd0 c63f2ae5b185 "./AlgorithmicIntegr…" 3 hours ago Exited (0) 3 hours ago admiring_robinson
57cb412fcdb8 ec024683a9cb "./AlgorithmicIntegr…" 2 days ago Exited (0) 2 days ago xenodochial_bassi
bea7512b32d1 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 5 days ago charming_faraday
f6493cdd7a4c 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 7 days ago gifted_beaver
d6c2fbeece4d 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 7 days ago nervous_vaughan
112f65839ad0 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 7 days ago confident_jones
3091e5e0e0df 7c885251d1f5 "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 2 weeks ago busy_ganguly
01abc933581a 7c885251d1f5 "./AlgorithmicIntegr…" 2 weeks ago Exited (139) 2 weeks ago exciting_lumiere
08ec1ac6c986 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (0) 2 weeks ago competent_rhodes
0d1890d0c976 7c885251d1f5 "./AlgorithmicIntegr…" 2 weeks ago Exited (0) 2 weeks ago confident_solomon
eebb137e716e 116e0b9947ea "./AlgorithmicIntegr…" 2 weeks ago Exited (0) 2 weeks ago lucid_dirac
提取出第一列container_id
docker ps -a | awk /Exited/ | awk {print $1}
运行结果如下:
root@cv-note3:~# docker ps -a | awk '/Exited/' | awk '{print $1}'
286c51de5dd0
57cb412fcdb8
bea7512b32d1
f6493cdd7a4c
d6c2fbeece4d
112f65839ad0
3091e5e0e0df
01abc933581a
08ec1ac6c986
0d1890d0c976
eebb137e716e
不足之处
如果有的容器NAMES或COMMANDS中包含Exited,那岂不是会误删
这样做只有确定到STATUS那一列中利用正则表达式匹配Exited
显然找到Exited在第7列(以空格为分割点)
# 改进版本
docker ps -a | awk '$7 ~ /Exited/ {print $1}
# ~ 为使用正则表达式标志 !~为不适用正则表达式
# 利用'$7=="Exited"'也可以
root@cv-note3:~# docker ps -a | awk '$7 ~ /Exited/ {print $1}'
286c51de5dd0
57cb412fcdb8
bea7512b32d1
f6493cdd7a4c
d6c2fbeece4d
112f65839ad0
3091e5e0e0df
01abc933581a
08ec1ac6c986
0d1890d0c976
eebb137e716e
最后再将一列转换为一行(因为每一行没有换行符号\,也可以在每一行增加换行符号)
docker ps -a | awk '$7 ~ /Exited/ {print $1}' | awk '{printf " %s ",$1}'
#输出
286c51de5dd0 57cb412fcdb8 bea7512b32d1 f6493cdd7a4c d6c2fbeece4d 112f65839ad0 3091e5e0e0df 01abc933581a 08ec1ac6c986 0d1890d0c976 eebb137e716e
大功告成,删除容器!
286c51de5dd0 57cb412fcdb8 bea7512b32d1 f6493cdd7a4c d6c2fbeece4d 112f65839ad0 3091e5e0e0df 01abc933581a 08ec1ac6c986 0d1890d0c976 eebb137e716e root@cv-note3:~# clear
root@cv-note3:~# docker stop 286c51de5dd0 57cb412fcdb8 bea7512b32d1 f6493cdd7a4c d6c2fbeece4d 112f65839ad0 3091e5e0e0df 01abc933581a 08ec1ac6c986 0d1890d0c976 eebb137e716e
286c51de5dd0
57cb412fcdb8
bea7512b32d1
f6493cdd7a4c
d6c2fbeece4d
112f65839ad0
3091e5e0e0df
01abc933581a
08ec1ac6c986
0d1890d0c976
eebb137e716e
root@cv-note3:~# docker rm 286c51de5dd0 57cb412fcdb8 bea7512b32d1 f6493cdd7a4c d6c2fbeece4d 112f65839ad0 3091e5e0e0df 01abc933581a 08ec1ac6c986 0d1890d0c976 eebb137e716e
286c51de5dd0
57cb412fcdb8
bea7512b32d1
f6493cdd7a4c
d6c2fbeece4d
112f65839ad0
3091e5e0e0df
01abc933581a
08ec1ac6c986
0d1890d0c976
eebb137e716e