title: How to set system-wide environment variables in dockerfile from a file inside the docker container?
body: <p>I am setting up a cross-compile environment as a docker container. During the build process I install a SDK for an embedded device:</p>
  
  <pre><code>ADD sdk.tar /opt
  WORKDIR /opt/
  RUN sdkinstall.sh
  </code></pre>
  
  <p>which installs the sdk in a folder <code>/opt/sdk/</code></p>
  
  <p>Now I have a file <code>/opt/sdk/envsetup</code> that contains some environment variables:</p>
  
  <pre><code>export SDKTARGETSYSROOT=/opt/sdk/sysroots/cortexa9hf-vfp-neon-gad-linux-gnueabi
  export PATH=/opt/sdk/sysroots/x86_64-gadsdk-linux/usr/bin:/opt/sdk/sysroots/x86_64-gadsdk-linux/usr/bin/arm-gad-linux-gnueabi:$PATH
  export PKG_CONFIG_SYSROOT_DIR=$SDKTARGETSYSROOT
  
  .... and so on
  </code></pre>
  
  <p>Usually, when I would run a shell in the container I would do <code>source /opt/sdk/envsetup</code> and then compile stuff from the shell.</p>
  
  <p>However, what I am trying to achieve is that all those environment variables are present system wide inside the docker container and are taken into account when I for example do something like <code>docker run -it xyz:latest make &lt;somedir&gt;</code></p>
  
  <p>I am testing this by calling <code>docker run -it xyz:latest echo $SDKTARGETSYSROOT</code> and I was expecting the container to print /opt/sdk/sysroots/cortexa9hf-vfp-neon-gad-linux-gnueabi`</p>
  
  <p>What I tried to achieve this so far: </p>
  
  <ul>
  <li>Adding the vars to /etc/environment: <code>RUN cat /opt/sdk/environment-setup-cortexa9hf-vfp-neon-gad-linux-gnueabi | cut -d" " -f 2 &gt;&gt; /etc/environment</code></li>
  <li>Adding the vars to /etc/profile, /root/.profile and /root/.bashrc: <code>RUN echo -e "# Auto source some variables\nsource /opt/sdk/environment-setup-cortexa9hf-vfp-neon-gad-linux-gnueabi" &gt;&gt; [...]</code></li>
  <li>Adding one var manually in the dockerfile via <code>ENV SDKTARGETSYSROOT /opt/sdk/sysroots/cortexa9hf-vfp-neon-gad-linux-gnueabi</code>, just to test the behaviour</li>
  <li><code>.</code> or <code>/bin/bash -c 'source'</code> the file <code>/opt/sdk/envsetup</code></li>
  </ul>
  
  <p>neither of those led to my expected result. How can I achieve those variables get setup properly?</p>
  
  <p>As the file that contains the variables is unknown at the beginning of the <code>docker build</code> and gets created during the sdk installation inside of the container, I do not really like the idea of adding its content manually into the dockerfile via ENV, even if that worked, which did not.</p>