Skip to content

Instantly share code, notes, and snippets.

@am-
Created April 7, 2017 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save am-/922bc089f4848f33b301e9c320f3bc90 to your computer and use it in GitHub Desktop.
Save am-/922bc089f4848f33b301e9c320f3bc90 to your computer and use it in GitHub Desktop.
Dynamically determine the IP address of a Docker container and change the /etc/hosts accordingly
#!/bin/sh
# Assumption 1: $CONTAINER and $HOST are properly set
# Assumption 2: The host is already set in /etc/hosts.
# Assumption 3: In /etc/hosts, the IP address and host are separated by a tab
IP=`docker exec -t $CONTAINER ip addr | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep 172`
sudo sed -i "/$HOST/s/[^\t]*/$IP/" /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment