DEV Community

Discussion on: Docker Tip - How to use the host's IP Address inside a Docker container on macOS, Windows, and Linux

Collapse
 
ibejohn818 profile image
John Hardy

if you need the host.docker.internal (for example, nginx proxy to your host network), use an entrypoint script (IE: ENTRYPOINT['/entrypoint.sh'])

" install iputils-ping & iproute2 (or like packages)
" check if the host name resolves,
" if not, then get the gateway of the current network (which is the host)
" and set it in the containers host file

!/bin/sh

HOST_DOMAIN="host.docker.internal"
ping -q -c1 $HOST_DOMAIN > /dev/null 2>&1
if [ $? -ne 0 ]; then
HOST_IP=$(ip route | awk 'NR==1 {print $3}')
echo -e "$HOST_IP\t$HOST_DOMAIN" >> /etc/hosts
fi

Cheers ;-)