Skip to content

Instantly share code, notes, and snippets.

@thpham
Forked from TheDauthi/dockerhost.sh
Created March 5, 2018 21:00
Show Gist options
  • Save thpham/fff462d1fecce7c60c1a29e4df4ea609 to your computer and use it in GitHub Desktop.
Save thpham/fff462d1fecce7c60c1a29e4df4ea609 to your computer and use it in GitHub Desktop.
Dockerhost detection
#!/usr/bin/env sh
# The default hostname to set
DEFAULT_DOCKER_HOSTNAME=${DEFAULT_DOCKER_HOSTNAME-dockerhost.internal}
# A comma-delimited list of extra hostnames to add as aliases
EXTRA_DOCKER_HOSTNAMES=${EXTRA_DOCKER_HOSTNAMES-}
# Whether to export variables
EXPORT_DOCKERHOST=${EXPORT_DOCKERHOST-1}
# Whether to output to /etc/hosts
UPDATE_HOSTFILE_DOCKERHOST=${UPDATE_HOSTFILE_DOCKERHOST-1}
# Whether to echo to stdout
ECHO_DOCKERHOST=${ECHO_DOCKERHOST-1}
# Default host type
DOCKERHOST_OS="unknown"
# Default IP for Linux hosts
DOCKERHOST_IP=""
# Test Windows name
WINDOWS_HOSTNAME="docker.for.win.localhost"
# Test Mac name
MACOS_HOSTNAME="docker.for.mac.localhost"
################################################################
# These are functions to allow this script to be easily modified
# into other forms. Specifically get_dockerhost_address can be
# used to fill in DNS or used with NetworkManager.
# Get the base command we use to look up the remote
# If timeout exists, get the right timeout type
get_base_command() {
# If we can't ping, we can't test.
if ! command -v ping > /dev/null 2>&1; then
return 1
fi
# A command string we'll be building
COMMAND=""
# Check for the ability to limit the timeout
if command -v timeout >/dev/null 2>&1; then
# We still have a problem - there are two widely used incompatible
# variants of timeout. Let's see which one we have.
# env should not be a builtin, doesn't require a pipe,
# has a consistent command line test, and is unlikely to fail
if timeout -t1 env >/dev/null 2>&1; then
COMMAND="timeout -t1 "
else
COMMAND="timeout 1s "
fi
fi
# Final version of command
COMMAND="$COMMAND ping -c1 "
echo "$COMMAND"
return 0
}
# Get the remote IP address of a host
get_remote_ip() {
TEST_HOST=$1
REMOTE_EXISTS=1
IP=""
# This allows us to trim the "terminated" BusyBox signal output text
IP=$(sh -c "$BASE_COMMAND $TEST_HOST" 2>&1)
# Exit codes: 0 - found the host, 1/2 failed to find, 124/143 terminated
REMOTE_EXISTS="$?"
# If we got 127.0.0.1 or ::1, we're not inside a container
if echo "$IP" | grep -q -m1 -F '127.0.0.1' >/dev/null 2>&1; then
return 1
elif echo "$IP" | grep -q -m1 -F '(::1)' >/dev/null 2>&1; then
return 1
fi
if [ "$REMOTE_EXISTS" != "0" ]; then
return 1
fi
return 0
}
# Perform the lookups
get_dockerhost_address() {
# If we can't test, let's get out of here
if ! BASE_COMMAND=$(get_base_command); then
# This image doesn't support testing via ping
return 1
fi
if DOCKERHOST_IP=$(get_remote_ip "$WINDOWS_HOSTNAME"); then
DOCKERHOST_OS="windows"
elif DOCKERHOST_IP=$(get_remote_ip "$MACOS_HOSTNAME"); then
DOCKERHOST_OS="macosx"
else
DOCKERHOST_OS="linux"
DOCKERHOST_IP=$(ip r | grep ^default | cut -d" " -f3)
fi
DOCKERHOST_IP=$(echo "$DOCKERHOST_IP" | grep -m1 -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' )
echo "$DOCKERHOST_IP" "$DOCKERHOST_OS"
return 0
}
update_dockerhost_dns() {
# Use this format to allow additional info to be returned
# We need to use <<END instead of <<< to be ash-compatible
read DOCKERHOST_IP DOCKERHOST_OS <<END
$(get_dockerhost_address)
END
if [ "$EXPORT_DOCKERHOST" = "1" ]; then
export DOCKERHOST_IP
export DOCKERHOST_OS
fi
if [ "$ECHO_DOCKERHOST" = "1" ]; then
echo "$DOCKERHOST_IP"
fi
if [ "$EXPORT_DOCKERHOST" != "1" ]; then
return 0
fi
DOCKER_HOSTNAME_LIST="$DEFAULT_DOCKER_HOSTNAME"
if [ ! -z "$EXTRA_DOCKER_HOSTNAMES" ]; then
DOCKER_HOSTNAME_LIST="$EXTRA_DOCKER_HOSTNAMES,$DOCKER_HOSTNAME_LIST"
fi
IFS=',';
for name in $DOCKER_HOSTNAME_LIST; do
echo "$DOCKERHOST_IP $name" >> /etc/hosts
done
unset IFS
return 0
}
# If you just want to use this as output...
update_dockerhost_dns
####
# To use this as an init script, uncomment the below and replace with your executable
# Otherwise, source/exec this file.
# exec "/usr/sbin/apachectl" "-DFOREGROUND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment