0

I'm using this code to inject the docker host ip into my container. It seems to work but I'm not sure exactly what ip route list does and if this will work on other environments and if the ip param will always be 9th.

docker_host_ip=$(ip route list | grep docker0 | awk '{print $9}')

docker_compose="
  version: '3.5'
  services:
    app:
      extra_hosts:
        - dockerhost:$docker_host_ip
"

echo "$docker_compose" | docker-compose \
  -f docker-compose.yml \
  -f /dev/stdin \
  up

1 Answers1

1

This looks reliable. This ip route list shows all the configured routes on your system and the src attribute restricts the potential sources for the target IPs, which is the docker0 IP in this case (look here for more info about src: ip route show src field).

Another way would be to get the network interface details via ifconfig docker0 or ip a show dev docker0.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
Juraj Martinka
  • 431
  • 3
  • 7