Skip to content

Instantly share code, notes, and snippets.

@dinhnv
Created November 19, 2020 05:03
Show Gist options
  • Save dinhnv/a3e02a9eeb8fc0000312e7edc04c2684 to your computer and use it in GitHub Desktop.
Save dinhnv/a3e02a9eeb8fc0000312e7edc04c2684 to your computer and use it in GitHub Desktop.
resolve ip for host.docker.internal DNS entry
# How to use:
# add to Dockerfile, otherwise add to entrypoint script. For example:
# RUN mkdir -p /bin/docker-entrypoint/ \
# && cp /tmp/scripts/docker-entrypoint/* /bin/docker-entrypoint/ \
# && chmod +x -R /bin/docker-entrypoint/ \
# ;
# ENTRYPOINT ["/bin/docker-entrypoint/resolve-docker-host-ip.sh", ...]
set -e
HOST_DOMAIN="host.docker.internal"
# check if the host exists - this will fail on linux
if dig ${HOST_DOMAIN} | grep -q 'NXDOMAIN'
then
# resolve the host IP
HOST_IP=$(ip route | awk 'NR==1 {print $3}')
# and write it to the hosts file
echo "$HOST_IP\t$HOST_DOMAIN" >> /etc/hosts
fi
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment