Friday, 10 April 2020

Tinkering with Docker in Bash with a side-order of Sed and Awk

Just tidying up some documentation ....

Want to list all of the containers running on your Docker server ?

docker ps -a | sed 1d | awk '{print $1}'

6153d5b70852
5936a4a0179b
3a11d49a5230
af8705a70abe
05475e542e5d
33648fe5e9d9
d1995a3d141f
d39f9ecc3058
e6310ec76d1f

Why is this useful ?

Because one can then use this inside a script to, say, inspect all containers.

Here's an example: -

for i in `docker ps -a | sed 1d | awk '{print $1}'`; do docker inspect $i; done | grep IPv4Address

                        "IPv4Address": "10.23.2.72"
                        "IPv4Address": "10.23.2.50"
                        "IPv4Address": "10.23.2.76"
                        "IPv4Address": "10.23.2.80"
                        "IPv4Address": "10.23.2.55"
                        "IPv4Address": "10.23.2.64"

or, using jq: -

for i in `docker ps -a | sed 1d | awk '{print $1}'`; do docker inspect $i | jq .[].NetworkSettings.Networks.staticIP.IPAddress; done

"10.23.2.72"
"10.23.2.50"
"10.23.2.76"
"10.23.2.80"
"10.23.2.55"
"10.23.2.64"

for i in `docker ps -a | sed 1d | awk '{print $1}'`; do docker inspect $i | jq .[].NetworkSettings.Networks.bridge.IPAddress; done

"172.31.0.4"
"172.31.0.2"
"172.31.0.3"

PS I've been getting into jq recently, and jqplay.org has been an invaluable resource ...


No comments:

Note to self - use kubectl to query images in a pod or deployment

In both cases, we use JSON ... For a deployment, we can do this: - kubectl get deployment foobar --namespace snafu --output jsonpath="{...