AWK - It's my "new" best friend forever ....
here's a quick spot of tidying up completed/unwanted Docker containers, using the power of AWK: -
docker rm `docker ps -a|grep Exit|awk '{print $1}'`
So we're executing the result of: -
docker ps -a|grep Exit
e717fa412d6a hello-world "/hello" 19 seconds ago Exited (0) 18 seconds ago romantic_chatterjee
761e4ec73bb5 docker/whalesay "cowsay 'Awwwww, tha…" About an hour ago Exited (0) About an hour ago jolly_sanderson
950cd9684ee6 docker/whalesay "cowsay 'Aww Shucks …" About an hour ago Exited (0) About an hour ago clever_kare
d0d444d6f4f1 docker/whalesay "cowsay Aww" About an hour ago Exited (0) About an hour ago pedantic_dirac
5c20a293793f docker/whalesay "cowsay Awwww Shucks…" About an hour ago Exited (0) About an hour ago dreamy_bell
d67c6d05cc24 docker/whalesay "cowsay Awwww Shucks" About an hour ago Exited (0) About an hour ago quizzical_curran
7507f0df1db9 docker/whalesay "cowsay Awwww" About an hour ago Exited (0) About an hour ago lucid_wright
77997983c07b docker/whalesay "cowsay" About an hour ago Exited (0) About an hour ago festive_nobel
1b34cc7f227d docker/whalesay "cowsay boo" About an hour ago Exited (0) About an hour ago eager_khorana
517237d924bf docker/whalesay "cowsay boo" 27 hours ago Exited (0) 27 hours ago naughty_ramanujan
6b6bb56464a9 docker/whalesay "/bin/bash" 27 hours ago Exited (0) 27 hours ago dreamy_gauss
3d0a99dfd9a4 hello-world "/hello" 27 hours ago Exited (0) 27 hours ago frosty_hypatia
In other words, a list of all the containers in the Exited status .....
... and then parsing the returned list to ONLY give us the container ID: -
docker ps -a|grep Exit|awk '{print $1}'
e717fa412d6a
761e4ec73bb5
950cd9684ee6
d0d444d6f4f1
5c20a293793f
d67c6d05cc24
7507f0df1db9
77997983c07b
1b34cc7f227d
517237d924bf
6b6bb56464a9
3d0a99dfd9a4
Note that the grep pulls out everything with the word "Exit" thus ignoring the title line: -
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
We can then feed that into AWK to give us the container IDs.
We then use the power of the back tick ( ` ) to allow us to run the docker rm command over the output from the previous command: -
rm `docker ps -a|grep Exit|awk '{print $1}'`
e717fa412d6a
761e4ec73bb5
950cd9684ee6
d0d444d6f4f1
5c20a293793f
d67c6d05cc24
7507f0df1db9
77997983c07b
1b34cc7f227d
517237d924bf
6b6bb56464a9
3d0a99dfd9a4
which clears the field: -
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Nice !
No comments:
Post a Comment