As I’ve mentioned on previous articles, I run my Docker development environment on a Windows 10 laptop (even though I mostly do Linux containers).
At some point the list of stopped containers has grown so much that it has become a chore to delete them, so I resorted to PowerShell to help with that.
This is my current container list (all are stopped, so they don’t show up in docker ps without the ‘-a’ flag):
~$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f44431f978c1 gliderlabs/alpine:3.6 "/bin/sh" 21 hours ago Exited (0) 21 hours ago unruffled_booth 638c280e73db 724d3e35f78d "/run/azdcparser" 22 hours ago Exited (0) 22 hours ago awesome_galileo a96eac16ed1d 724d3e35f78d "/run/azdcparser" 22 hours ago Exited (0) 22 hours ago flamboyant_dijkstra 5ac68b120003 a872cca33ace "/bin/sh -c 'go bu..." 22 hours ago Exited (1) 22 hours ago thirsty_wescoff 96f3941e7ca1 a872cca33ace "/bin/sh -c 'go bu..." 22 hours ago Exited (1) 22 hours ago loving_nobel 384f7107ee39 a872cca33ace "/bin/sh -c 'go bu..." 22 hours ago Exited (1) 22 hours ago admiring_lewin c2242d24f41e 15be2628844d "/bin/sh -c 'go bu..." 22 hours ago Exited (1) 22 hours ago focused_edison 277b02f27a8f gliderlabs/alpine:3.6 "/bin/sh" 22 hours ago Exited (0) 21 hours ago hungry_turing 29751587fc07 fa61e3181b66 "iperf3 -s" 47 hours ago Exited (1) 21 hours ago agitated_shaw 0bcd479cd39f 3dc304f3485c "iperf3" 47 hours ago Exited (1) 47 hours ago serene_aryabhata 12ae59f40374 e3be54b9f1b0 "iperf3 -s" 47 hours ago Created sad_lamport dce01cc829a9 gliderlabs/alpine:3.6 "/bin/sh" 2 days ago Exited (1) 2 days ago hardcore_lewin 623c84e66182 8df1607e2471 "/run/qperf localh..." 13 days ago Exited (1) 13 days ago hungry_chandrasekhar 43b7b4ba7595 8df1607e2471 "/run/qperf -lp 4000" 13 days ago Exited (137) 13 days ago relaxed_hugle 385b1ec0090a 8df1607e2471 "/run/qperf localh..." 13 days ago Exited (1) 13 days ago agitated_northcutt 8f685034f69a a89aa05f54bd "/run/qperf localh..." 13 days ago Exited (1) 13 days ago optimistic_colden 79e83d736d35 a89aa05f54bd "/run/qperf 127.0...." 13 days ago Exited (1) 13 days ago vigorous_joliot 388d8bcdb0cf a89aa05f54bd "/run/qperf 127.0...." 13 days ago Exited (0) 13 days ago boring_easley d8a2d82e68c6 a89aa05f54bd "/run/qperf 127.0...." 13 days ago Exited (1) 13 days ago wizardly_lamarr 01945c2e4283 a89aa05f54bd "/run/qperf -lp 4000" 13 days ago Exited (137) 13 days ago dreamy_williams c202e8e138d2 a89aa05f54bd "/run/qperf -lp 4000" 13 days ago Created focused_goldberg d8e1d7c34075 a89aa05f54bd "/run/qperf 127.0...." 13 days ago Exited (1) 13 days ago peaceful_agnesi 6f4849a1db29 a89aa05f54bd "/run/qperf" 13 days ago Exited (137) 13 days ago relaxed_jepsen 72f3cc9d97e1 a89aa05f54bd "/run/qperf 127.0...." 13 days ago Exited (1) 13 days ago vibrant_hugle b01f2b7b5d0b a89aa05f54bd "/run/qperf" 13 days ago Exited (137) 13 days ago naughty_liskov 89d8ac1175e5 a89aa05f54bd "/run/qperf 127.0...." 13 days ago Exited (1) 13 days ago eloquent_heyrovsky d5096aa832eb a89aa05f54bd "/run/qperf" 13 days ago Exited (137) 13 days ago frosty_turing 56f4e45bc71f gliderlabs/alpine:3.6 "/bin/sh" 13 days ago Exited (0) 13 days ago wonderful_wilson 12097fa661f8 gliderlabs/alpine:3.6 "/bin/sh" 13 days ago Exited (0) 13 days ago tender_galileo b65ba891ea3b gliderlabs/alpine:3.6 "/bin/sh" 13 days ago Exited (0) 13 days ago trusting_fermat a3e5e569e2ab dd6f76d9cc90 "bash" 2 weeks ago Exited (0) 2 weeks ago peaceful_murdock 1dee003eecca 725dcfab7d63 "/hello" 2 weeks ago Exited (0) 2 weeks ago pensive_almeida 64d42e53b8bb 9790c7acb7b1 "/run/ntttcp -s -m..." 2 weeks ago Exited (0) 2 weeks ago tender_ardinghelli 708deb979d3a 9790c7acb7b1 "/run/ntttcp -r" 2 weeks ago Created zealous_noether cd61c163a9fc 9790c7acb7b1 "/run/ntttcp -r" 2 weeks ago Created vigilant_wing dff7e2e31358 9790c7acb7b1 "/run/ntttcp -s -m..." 2 weeks ago Exited (0) 2 weeks ago heuristic_yonath 1bb8e2932dbb 9790c7acb7b1 "/run/ntttcp -s -m..." 2 weeks ago Exited (0) 2 weeks ago elated_elion 8fbfa3cb053d 9790c7acb7b1 "/run/ntttcp -r" 2 weeks ago Exited (137) 2 weeks ago pedantic_ardinghelli 9df45e245063 9790c7acb7b1 "/run/ntttcp -r" 2 weeks ago Exited (137) 2 weeks ago condescending_banach f1b2cbaab606 9790c7acb7b1 "/run/ntttcp -s10 ..." 2 weeks ago Exited (255) 2 weeks ago vigilant_wright
A proper PITA.
Deleting one by one with “docker rm xxxx” wasn’t going to cut it. Most scripts or one-liners out there rely on Unix tools like awk and sed, so I could resort to WSL for that, but I decided to use PowerShell because PowerShell is just flippin’ amazing.
Much to my surprise, Stuart Leeks has been already there and done it. Stuart wrote a little helper that parses ‘docker ps’ output and converts it into a powerful PowerShell object. Here’s how simple it is to use:
~$ $containers = docker ps -a | ConvertFrom-Docker
Now I can play with the data as I please, e.g.:
~$ $containers | where { $_.Image -like "*alpine*" } | ft Image Ports Names Command Created ContainerId Status ----- ----- ----- ------- ------- ----------- ------ gliderlabs/alpine:3.6 unruffled_booth "/bin/sh" 22 hours ago f44431f978c1 Exited (0) 22 hours ago gliderlabs/alpine:3.6 hungry_turing "/bin/sh" 22 hours ago 277b02f27a8f Exited (0) 22 hours ago gliderlabs/alpine:3.6 hardcore_lewin "/bin/sh" 2 days ago dce01cc829a9 Exited (1) 2 days ago gliderlabs/alpine:3.6 wonderful_wilson "/bin/sh" 13 days ago 56f4e45bc71f Exited (0) 13 days ago gliderlabs/alpine:3.6 tender_galileo "/bin/sh" 13 days ago 12097fa661f8 Exited (0) 13 days ago gliderlabs/alpine:3.6 trusting_fermat "/bin/sh" 13 days ago b65ba891ea3b Exited (0) 13 days ago
Great!
Now back to what we wanted to do, which is basically to nuke all containers in a programmatic way:
~$ ForEach ($container in $containers) { docker rm $container.ContainerId } f44431f978c1 638c280e73db a96eac16ed1d 5ac68b120003 96f3941e7ca1 384f7107ee39 (...)
Did it really work? Let’s have a quick look:
~$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Pretty clean, isn’t it?
Once you have parsed your data made a PSObject, the possibilities are endless.
Idea for the readers: You could run an scheduled task that cleaned up “Exited” containers every few hours, then run through “docker images” output and correlate which ones weren’t use so you could save up some disk space.