<p>I'm trying to run my application on Kubernetes. My docker container have environment variables such as <code>PATH</code> and <code>LD_LIBRARY_PATH</code>, which are set in Dockerfile. I tried to change them in the yaml file like this:</p>
<pre><code>env:
- name: LD_LIBRARY_PATH
  value: &quot;foo:$(LD_LIBRARY_PATH)&quot;
</code></pre>
<p>The above configuration doesn't work, I just see <code>LD_LIBRARY_PATH=foo:$(LD_LIBRARY_PATH)</code> in the pod. This method seems work for Kubernetes env variables such as <code>KUBERNETES_PORT_443_TCP_PROTO</code>, but not for docker env variables.</p>
<p>My questions are:</p>
<ol>
<li><p>I think the <code>env</code> settings in yaml are injected into docker before the running time of the container, so kubernetes cannot read the value of <code>LD_LIBRARY_PATH</code>. Therefor it can't change the variable. Do I understand it right?</p>
</li>
<li><p>How to change container environment variables with kubernetes env? I know that I can set the env variables in the <code>command</code> field of yaml file, but that seems not clean and are there other ways to do that?</p>
</li>
<li><p>If Kubernetes can't change existed envs, does it mean that the env field in yaml file is designed to add new envs only?</p>
</li>
</ol>
<p>Thank you!</p>

<p>The Kubernetes variable expansion syntax only works on things Kubernetes directly knows about.  Inside a container an environment variable could come from a couple of places (the Dockerfile <code>ENV</code> directive, the base container environment itself, setup in an entrypoint script) and Kubernetes doesn't consider any of these; it only considers things in the same container spec.  The <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#envvar-v1-core" rel="noreferrer">API definition of <code>EnvVar</code></a> hints at this:</p>
<blockquote>
<p>Variable references $(VAR_NAME) are expanded using the previous defined environment variables in the container and any service environment variables.</p>
</blockquote>
<p>You can't use this Kubernetes syntax to change environment variables in the way you're describing.  You can only refer to other things in the same <code>env:</code> block (which may come from ConfigMaps or Secrets) and the implicit variables that come from other Services.</p>
<p>(Changing path-type variables at the Kubernetes level doesn't make a lot of sense.  Since an image is self-contained, it already contains all of the commands and libraries it would need.  It's difficult in Kubernetes to inject more tools or libraries; it'd be better to install them directly in your image, ideally in <code>/usr/lib</code> or <code>/usr/local/lib</code>, but failing that you can update <code>ENV</code> in a Dockerfile similar to how you suggest here.)</p>
