Last active
August 23, 2020 15:13
-
-
Save djdevin/2c607bca340f529b717cb58f10a8b07d to your computer and use it in GitHub Desktop.
docker-local-hosts.sh
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 -e | |
# Usage: ./docker-local-hosts.sh containername mylocalname | |
# Get IP of container and strip newlines. | |
INSPECT=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $1) | |
IP=${INSPECT//[$'\t\r\n']} | |
if test -z "$IP"; then | |
echo "Container IP for $1 not found." | |
exit | |
fi | |
# Get hostname or use container name as default. | |
HOSTNAME="${2:-$1}" | |
# Remove line with previous container. | |
sudo sed -i "/\s#$1/d" /etc/hosts | |
# Add new hosts line. | |
echo "$IP $HOSTNAME #$1" | sudo tee -a /etc/hosts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment