3

I have a very special scenario. A virtual machine containing some docker containers. One of this containers needs to know the host ip. The problem is if I pass the host ip on container build or using -e on docker run command, it remains "static" (always the same, the one of that moment) on the container.

That vm can be on a laptop and the laptop is moving from different networks and the vm host ip can be different each reboot.

This special container has the --restart=always and is not built or "docker run" again... only once. And as I said, I need the host's ip on each reboot to configure the service inside the container on it's entrypoint because the container has a bind dns server which must load a zone with some dns entries that must be pointing to itself (the host's ip). An environment var would be great if possible. These are my data:

  • The "normal" lauch

The end of my Dockerfile:

....
....
ENTRYPOINT ["/etc/bind/entrypoint.sh"]
CMD ["/usr/sbin/named", "-g", "-c", "/etc/bind/named.conf", "-u", "bind"]

Entrypoint file (the regex works fine if the var could have the right value):

#!/bin/bash
sed -ri "s/IN A.*/IN A $HOSTIP/" /etc/bind/db.my.zone
exec "$@"

Docker run cmd:

docker run --name myContainer -d --restart=always -p 53:53 -p 53:53/udp myImage
  • What I tried:

I guess the entrypoint is ok and shouldn't be modified if I can provide to it a var with the right value.

If I put a -e on docker run command, it is "hardcoded" forever with the same ip always even if the host is on different networks:

docker run -e HOSTIP=$(ifconfig eth0 | grep netmask | awk '{print $2}') --name myContainer \
-d --restart=always -p 53:53 -p 53:53/udp myImage

I tried unsuccessfully also modifying the CMD on Dockerfile:

CMD (export HOSTIP=$(ifconfig eth0 | grep netmask | awk '{print $2}'));/usr/sbin/named -g -c /etc/bind/named.conf -u bind

Is possible to achieve something like this? Thanks.

OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51

2 Answers2

3

There is the file /proc/net/tcp on the host machine that shows all the opened sockets. In particular, the second column is the local_address of the host interface.

The values in this column are store as little-endian four-byte hexadecimal numbers. To convert these to IP addresses take a look here

Thus when starting you container, you can mount this file from the host onto the container -v /proc/net/tcp:/host-tcp and read the host ip addresses which will be constantly reflected in this file.

yamenk
  • 46,736
  • 10
  • 93
  • 87
  • I upvoted it because it gave me the idea, mapping a volume file. Your method `/proc/net/tcp` didn't worked for me, but your idea is good. I'll post my solution. Thank you. – OscarAkaElvis Aug 26 '17 at 11:33
1

I finally solved it. Thank you @yamenk for your answer, it gave me the idea, upvoting.

Finally what I did:

  • I created a simple script on host which is getting host ip and writting it into another file.
  • I set that script to be launched on every host boot before docker start.
  • I mapped the file with the ip into the container using -v on docker run command.
  • I set my entrypoint to get the ip from that file containing the ip and modifying with sed the needed container config files

Everything working! if I boot the vm (the host machine) on another different network, it gets the ip and the container is able to reconfigure itself before starting with the new ip.

OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51