Skip to content

Instantly share code, notes, and snippets.

@dimitrovs
Last active September 17, 2017 20:36
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dimitrovs/493678fd86c7cdf0c88312d9ddb4906b to your computer and use it in GitHub Desktop.
Save dimitrovs/493678fd86c7cdf0c88312d9ddb4906b to your computer and use it in GitHub Desktop.
Add the IP of a dockerhost to the /etc/hosts file of a Docker container if not already there.
#!/bin/bash
if [[ -z $(grep dockerhost /etc/hosts) ]]
then
echo `/sbin/ip route|awk '/default/ { print $3 }'` dockerhost >> /etc/hosts
fi
@battlesnake
Copy link

I wish I could star this multiple times, and it's also taught me a bit more AWK

@jhmartin
Copy link

I'm seeing $3 as the subnet default-gateway and $7 as the local dockerhost interface IP.

@sburnwal
Copy link

This gives the gateway ip and not the docker host ip.

@dimitrovs
Copy link
Author

@sburnwal but docker host is the gateway, isn't it? In what kind of config is docker host not the gateway of docker containers?

@gavvvr
Copy link

gavvvr commented Sep 17, 2017

Note that if your host is Mac, the 172.17.0.1 will be a wrong answer. Use dedicated DNS name docker.for.mac.localhost instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment