title: How to set environment variable in container from Kubernetes?
body: <p>I want to set an environment variable (I'll just name it <code>ENV_VAR_VALUE</code>) to a container during deployment through Kubernetes.</p>
  
  <p>I have the following in the pod yaml configuration:</p>
  
  <pre><code>...
  ...
      spec:
        containers:
        - name: appname-service
          image: path/to/registry/image-name
          ports:
          - containerPort: 1234
          env:
          - name: "ENV_VAR_VALUE"
            value: "some.important.value"
  ...
  ...
  </code></pre>
  
  <p>The container needs to use the <code>ENV_VAR_VALUE</code>'s value.<br>
  But in the container's application logs, it's value always comes out empty.<br>
  So, I tried checking it's value from inside the container:</p>
  
  <pre><code>$ kubectl exec -it appname-service bash
  root@appname-service:/# echo $ENV_VAR_VALUE
  some.important.value
  root@appname-service:/# 
  </code></pre>
  
  <p>So, the value was successfully set.</p>
  
  <p>I imagine it's because the environment variables defined from Kubernetes are set <strong>after</strong> the container is already initialized.</p>
  
  <p>So, I tried overriding the container's CMD from the pod yaml configuration:</p>
  
  <pre><code>...
  ...
      spec:
        containers:
        - name: appname-service
          image: path/to/registry/image-name
          ports:
          - containerPort: 1234
          env:
          - name: "ENV_VAR_VALUE"
            value: "some.important.value"
          command: ["/bin/bash"]
          args: ["-c", "application-command"]
  ...
  ...
  </code></pre>
  
  <p>Even still, the value of <code>ENV_VAR_VALUE</code> is still empty during the execution of the command.<br>
  Thankfully, the application has a restart function<br>
  -- because when I restart the app, <code>ENV_VAR_VALUE</code> get used successfully.<br>
  -- I can at least do some other tests in the mean time.</p>
  
  <h3>So, the question is...</h3>
  
  <blockquote>
    <p>How should I configure this in Kubernetes so it isn't a tad too late in setting the environment variables?</p>
  </blockquote>
  
  <p>As requested, here is the Dockerfile.<br>
  I apologize for the abstraction...</p>
  
  <pre><code>FROM ubuntu:18.04
  
  RUN apt-get update &amp;&amp; apt-get install -y some-dependencies
  
  COPY application-script.sh application-script.sh
  
  RUN ./application-script.sh
  
  # ENV_VAR_VALUE is set in this file which is populated when application-command is executed
  COPY app-config.conf /etc/app/app-config.conf
  
  CMD ["/bin/bash", "-c", "application-command"]
  </code></pre>