Skip to content

Instantly share code, notes, and snippets.

@djdevin
Last active August 23, 2020 15:13
Show Gist options
  • Save djdevin/2c607bca340f529b717cb58f10a8b07d to your computer and use it in GitHub Desktop.
Save djdevin/2c607bca340f529b717cb58f10a8b07d to your computer and use it in GitHub Desktop.
docker-local-hosts.sh
#!/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