Skip to content

Instantly share code, notes, and snippets.

@akiessling
Forked from djdevin/docker-local-hosts.sh
Created March 22, 2019 21:11
Show Gist options
  • Save akiessling/61e13f36ab285de17a0c4447cb9bacb0 to your computer and use it in GitHub Desktop.
Save akiessling/61e13f36ab285de17a0c4447cb9bacb0 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