title: How to source a script with environment variables in a docker build process?
body: <p>I'm creating an image that has a similar problem like the following docker project:</p>
  
  <h3>Dockerfile</h3>
  
  <pre><code>FROM alpine:3.9.3
  
  COPY ./env.sh /env.sh
  RUN source /env.sh
  CMD env
  </code></pre>
  
  <h3>env.sh</h3>
  
  <pre><code>TEST=test123
  </code></pre>
  
  <p>I built the image with </p>
  
  <pre><code>docker build -t sandbox .
  </code></pre>
  
  <p>and run it with</p>
  
  <pre><code>docker run --rm sandbox
  </code></pre>
  
  <p>The output is</p>
  
  <pre><code>HOSTNAME=72405c43801b
  SHLVL=1
  HOME=/root
  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  PWD=/
  </code></pre>
  
  <p>My environment variable is missing.</p>
  
  <p>In the real project I have to source a longer complex script for the installation for IBM DB2 client that also sets environment variables. How can I achieve it without reading the whole installation process and setting all variables with <code>ENV</code> in the dockerfile?</p>
  
  <p>EDIT: 
  In the real project the file <code>env.sh</code> is created as part of the installation process and it is not available from outside of the container. The environment variables are set depending on the system it is executed on. If I run it on the host it will set wrong variables in the guest.</p>
  
  <p>Part of the real script is</p>
  
  <pre><code>if [ -f ${INST_DIR?}/tools/clpplus.jar ]; then
      AddRemoveString CLASSPATH ${INST_DIR?}/tools/clpplus.jar a
  fi
  
  if [ -f ${INST_DIR?}/tools/antlr-3.2.jar ]; then
      AddRemoveString CLASSPATH ${INST_DIR?}/tools/antlr-3.2.jar a
  fi
  
  if [ -f ${INST_DIR?}/tools/jline-0.9.93.jar ]; then
      AddRemoveString CLASSPATH ${INST_DIR?}/tools/jline-0.9.93.jar a
  fi
  
  if [ -f ${INST_DIR?}/java/db2jcc.jar ]; then
      AddRemoveString CLASSPATH ${INST_DIR?}/java/db2jcc.jar a
  fi
  
  if [ -f ${INST_DIR?}/java/db2jcc_license_cisuz.jar ]; then
      AddRemoveString CLASSPATH ${INST_DIR?}/java/db2jcc_license_cisuz.jar a
  fi
  </code></pre>
  
  <p>It checks the installation and sets the variables depending on this. Since on the host is no DB2 installation the variables wouldn't be set.</p>