get or filter or ouput docker ps custom columns 1

get or filter or ouput docker ps custom columns

To get all container names and their IP addresses in just one single command.

docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)
If you are using docker-compose the command will be this:

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
The output will be:

/containerA - 172.17.0.4
/containerB - 172.17.0.3
/containerC - 172.17.0.2

another example:
docker ps | grep sida | tr -s ' ' | awk '{print $NF}' | xargs docker inspect $1 -f "{{.Name}} - {{json .NetworkSettings.Networks }}" | grep -i networkID
 
 The output will be:
 /docker-compose-sida_web_1 => {"docker-traefik_default":{"IPAMConfig":null,"Links":null,"Aliases":["web","de2326288433"],"NetworkID":"ebc5e603ab6b43c1b0f1853bb182ab6eb39ed86df0131d9fd0d2126cc7aaae82","EndpointID":"00e5c590339cc415eb6505e57ed0a5f009ee6d0ff323987e2c25bfd2d4f26a2c","Gateway":"172.21.0.1","IPAddress":"172.21.0.7","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:15:00:07","DriverOpts":null}}

Here is what the above code is Doing:
1. docker ps | grep sida
– This will list all the containers that have the word sida in their name.
2. tr -s ‘ ‘
– This will remove all the extra spaces in the output of the above command.
3. awk ‘{print $NF}’
– This will print the last column of the output of the above command.
4. xargs docker inspect $1 -f “{{.Name}} – {{json .NetworkSettings.Networks }}”
– This will take the output of the above command and pass it to the docker inspect command.
5. grep -i networkID
– This will filter the output of the above command and print only the lines that have the word networkID in them.

Similar Posts