Last active
October 5, 2020 10:26
-
-
Save mugifly/3bb1c54b48764340eda8 to your computer and use it in GitHub Desktop.
Simple inspector for Docker container IP address (shell script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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