Search code examples
dockerdnsenvironment-variablescontainershost

Docker. Add dynamic host ip to env var on container


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.


Solution

  • 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.