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 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
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.
I finally solved it. Thank you @yamenk for your answer, it gave me the idea, upvoting.
Finally what I did:
-v
on docker run command.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.