Skip to content

Instantly share code, notes, and snippets.

@aurelijusrozenas
Last active August 16, 2022 16:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurelijusrozenas/dca99f4e592949d4d68920d67e049a32 to your computer and use it in GitHub Desktop.
Save aurelijusrozenas/dca99f4e592949d4d68920d67e049a32 to your computer and use it in GitHub Desktop.
Displays docker ps list with additional column of IP address.
#!/bin/bash
# Displays docker ps list with additional column of IP address.
(
# take default docker ps output
docker ps "$@" | while read -r line; do
# replace separators with | to use later with `column`
echo -n "$line" | sed 's/ \{2,\}/|/g' | tr '\t' '|';
containerId=$(echo $line | column | awk '{print $1}')
# header
if [ $containerId == 'CONTAINER' ]; then
echo -e "|IP ADDRESS"
else
# take IP from docker inpect add to the line end
echo -e -n '|'
echo $containerId | xargs --no-run-if-empty docker inspect --format='{{range $net, $conf := .NetworkSettings.Networks}}{{$net}}:{{$conf.IPAddress}} {{end}}';
fi
done
) | column -t -s '|'
@aurelijusrozenas
Copy link
Author

Working for me on ubuntu 16.04. I anyone has any ideas, how to optimize it - I am all ears.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment