Skip to content

Instantly share code, notes, and snippets.

@mugifly
Last active October 5, 2020 10:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mugifly/3bb1c54b48764340eda8 to your computer and use it in GitHub Desktop.
Save mugifly/3bb1c54b48764340eda8 to your computer and use it in GitHub Desktop.
Simple inspector for Docker container IP address (shell script)
#!/bin/bash
# Simple inspector for Docker container IP address
# https://gist.github.com/mugifly/3bb1c54b48764340eda8
print_help () {
echo -e "USAGE: docker-container-ip.sh KEYWORD\n"
echo "KEYWORD: A keyword string for target container."
echo " e.g. dokku.mysql.foobar"
echo -e "\n[Example usecases]"
echo " Connect to MySQL database in container:"
echo " 1. Run the command: $ docker-container-ip.sh mysql"
echo " 2. You got the ip address -- e.g. 172.17.0.1"
echo " 3. Connect to it: $ mysql -h 172.17.0.1 -u foo -p"
}
keyword=$1
if [ $# -ne 1 ]; then
echo -e "Please set a keyword as argument! such as container name.\n"
print_help
exit 255
fi
container_ids=$(docker ps | grep $keyword | grep -o -e '^\S*')
for id in $container_ids
do
name=$(docker inspect --format="{{ .Name }}" $id)
ip=$(docker inspect --format="{{ .NetworkSettings.IPAddress }}" $id)
echo "${id} ${name} = ${ip}"
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment