DEV Community

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

Stefan Natter πŸ‡¦πŸ‡ΉπŸ‘¨πŸ»β€πŸ’» on May 21, 2020

Once in a while, you may need your Docker host's IP address. For instance, you need to be able to connect to the host network from inside a Docker ...
Collapse
 
thiagorb profile image
Thiago RomΓ£o Barcala

Another alternative is to add the entry below to your Linux host's /etc/hosts:

172.17.0.1 host.docker.internal
Enter fullscreen mode Exit fullscreen mode

Then you can simple define the environments as:

  DB_UPSTREAM: http://host.docker.internal:3000
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jay_67 profile image
Jay

Is 172.17.0.1 host ip?

If it is means every time host's ip address changes for some reason maybe network switching to WIFI to LAN we need to update this and rerun our compose?

Collapse
 
waterloomatt profile image
Matt Skelton • Edited

Running WSL (Ubuntu 20.04 LTS), I added this to my .bashrc file.

export DOCKER_GATEWAY_HOST="`/sbin/ip route|awk '/dev eth0 proto kernel/ { print  $9}'`"
Enter fullscreen mode Exit fullscreen mode

This runs /sbin/ip route, searches for the line containing "dev eth0 proto kernel", then grabs the 9th element from that line which corresponds to the IP address.

Then, in docker-compose.yml I referenced it here,

environment:
    - ...
    - PUBLIC_HOST=$DOCKER_GATEWAY_HOST
    - ...
Enter fullscreen mode Exit fullscreen mode
Collapse
 
alxgrk profile image
Alexander Girke

In case you are dealing with several Docker bridge networks, it's important to get the gateway IP of the correct network:

$ docker network inspect my-network -f "{{ (index .IPAM.Config 0).Gateway }}"
172.27.0.1
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ahmadharis4u profile image
Ahmad Haris • Edited

I tried this method:
$ export DOCKER_GATEWAY_HOST=172.17.0.1
put a new env docker container:
HOST_IP=${DOCKER_GATEWAY_HOST:-host.docker.internal}

When the docker runs, it shows HOST_IP=host.docker.internal

$ docker exec <container> env | grep HOST
HOST_IP=host.docker.internal
Enter fullscreen mode Exit fullscreen mode

The same is picked up by my python code.
Does this (host.docker.internal) also needs to be resolved to get the actual host IP?

Collapse
 
riochndr profile image
Rio Chandra

Thank you, you give me new insight. I do a lot research about this too and write on my gist github. problem connect container to host

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 ;-)

Collapse
 
jveillet profile image
Jeremie Veillet

Nice trick!! I I was searching for a solution like this since I moved to an Ubuntu machine at work. Thanks a ton! πŸ‘

Collapse
 
natterstefan profile image
Stefan Natter πŸ‡¦πŸ‡ΉπŸ‘¨πŸ»β€πŸ’»

Hi Jeremie,

Thanks for the feedback. I’m happy I could help you. ✌️

Collapse
 
horaciodegiorgi profile image
Horacio Degiorgi • Edited
export DOCKER_GATEWAY_HOST="`hostname -I` |awk '{print $1}'  `" 
Enter fullscreen mode Exit fullscreen mode

to automatize the iP (only get the first)

Collapse
 
natterstefan profile image
Stefan Natter πŸ‡¦πŸ‡ΉπŸ‘¨πŸ»β€πŸ’»

Thanks for the feedback!

Collapse
 
aguedeney profile image
Alan Guedeney

This is broken (at least on MacOS). I don’t get the host’s IP address. I get 192.168.65.3 for host.docker.internal. Am I missing something?

Collapse
 
neftedollar profile image
Roman Melnikov

As I understand 'host.docker.internal' inside docker default network will be bonded to your host machine. If you can access with 'host.docker.internal' your host ports then everything is ok

Collapse
 
alexmikhalev profile image
AlexMikhalev

Great tip, way simpler and works out of the box without any need for AWK/SED.

Collapse
 
nialljoemaher profile image
Niall Maher

Nice! This could definitely come in handy!

Collapse
 
natterstefan profile image
Stefan Natter πŸ‡¦πŸ‡ΉπŸ‘¨πŸ»β€πŸ’»

Thanks Niall. It does - resolved some issues for our team.

Collapse
 
uebmaster profile image
Uebmaster

this could be used it in linux for production?