title: Docker. Add dynamic host ip to env var on container
body: <p>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 <code>-e</code> on docker run command, it remains "static" (always the same, the one of that moment) on the container. </p>
  
  <p>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.</p>
  
  <p>This special container has the <code>--restart=always</code> 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:</p>
  
  <ul>
  <li>The "normal" lauch</li>
  </ul>
  
  <p>The end of my Dockerfile:</p>
  
  <pre><code>....
  ....
  ENTRYPOINT ["/etc/bind/entrypoint.sh"]
  CMD ["/usr/sbin/named", "-g", "-c", "/etc/bind/named.conf", "-u", "bind"]
  </code></pre>
  
  <p>Entrypoint file (the regex works fine if the var could have the right value):</p>
  
  <pre><code>#!/bin/bash
  sed -ri "s/IN A.*/IN A $HOSTIP/" /etc/bind/db.my.zone
  exec "$@"
  </code></pre>
  
  <p>Docker run cmd:</p>
  
  <pre><code>docker run --name myContainer -d --restart=always -p 53:53 -p 53:53/udp myImage
  </code></pre>
  
  <ul>
  <li>What I tried:</li>
  </ul>
  
  <p>I guess the entrypoint is ok and shouldn't be modified if I can provide to it a var with the right value.</p>
  
  <p>If I put a <code>-e</code> on docker run command, it is "hardcoded" forever with the same ip always even if the host is on different networks:</p>
  
  <pre><code>docker run -e HOSTIP=$(ifconfig eth0 | grep netmask | awk '{print $2}') --name myContainer \
  -d --restart=always -p 53:53 -p 53:53/udp myImage
  </code></pre>
  
  <p>I tried unsuccessfully also modifying the CMD on Dockerfile:</p>
  
  <pre><code>CMD (export HOSTIP=$(ifconfig eth0 | grep netmask | awk '{print $2}'));/usr/sbin/named -g -c /etc/bind/named.conf -u bind
  </code></pre>
  
  <p>Is possible to achieve something like this? Thanks.</p>