title: How to Set docker container ip to environment variable dynamically on startup?
body: <p>I want to export docker container hostname as an environment variable which I can later use in my app. In my docker file I call my script "run" as last command</p>
  
  <pre><code>CMD run
  </code></pre>
  
  <p>The run file is executable and works fine with rest of commands I perform but before them I want to export container hostname to an env. variable as follows</p>
  
  <p><strong>"run" File Try 1</strong> </p>
  
  <pre><code>#!/bin/bash
  export DOCKER_MACHINE_IP=`hostname -i`
  my_other_commands
  exec tail -f /dev/null
  </code></pre>
  
  <p>But when I enter docker container and check, the variable is not set. If I use </p>
  
  <blockquote>
    <p>echo $DOCKER_MACHINE_IP</p>
  </blockquote>
  
  <p>in run file after exporting, it shows ip on console when I try </p>
  
  <blockquote>
    <p>docker logs</p>
  </blockquote>
  
  <p>I also tried sourcing another script from "run" file as follows</p>
  
  <p><strong>"run" File Try 2</strong></p>
  
  <pre><code>#!/bin/bash
  source ./bin/script
  my_other_commands 
  exec tail -f /dev/null
  </code></pre>
  
  <p>and the script again contains the export command. But this also does not set the environment variable. What I am doing wrong?</p>