<p>According to <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/#using-environment-variables-inside-of-your-config" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/#using-environment-variables-inside-of-your-config</a> environment variables (defined in the config) can be used in the config like this:</p>
<pre><code>...
spec:
  containers:
    env:
    - name: GREETING
      value: &quot;Warm greetings to&quot;
    ...
    args: [&quot;$(GREETING) $(HONORIFIC) $(NAME)&quot;]
</code></pre>
<p>Can I use the variables everywhere in the config? (Probably only in side container.)</p>
<p>This seems not to work:</p>
<pre><code>    env:
    - name: DBHOST
      valueFrom:
        configMapKeyRef:
          key: db-host
          name: db-config
    - name: DB_URL
      value: mysql://$(DBHOST)/my_database
</code></pre>
<p>In the container, <code>echo $DB_URL</code> showed <code>mysql://$(DBHOST)/my_database</code>, but I expected DBHOST to be replaced.</p>
<p>So does it work in all other places except in <code>env</code>?</p>
<p>Maybe I have to look for another possibility to concatenate the values from the config map.</p>

<p>In the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#container-v1-core" rel="nofollow noreferrer">detailed API documentation for a Container object</a>, some of the fields specifically call out</p>
<blockquote>
<p>Variable references $(VAR_NAME) are expanded using the container's environment.</p>
</blockquote>
<p>This is specifically documented for <code>args</code> and <code>command</code>.  Similar text is also in the documentation for <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#envvar-v1-core" rel="nofollow noreferrer">EnvVar</a>'s <code>value</code>, with the caveat there that &quot;references $(VAR_NAME) are expanded using the previous defined environment variables&quot; (order of the <code>env:</code> block matters).</p>
<p>In a couple of places you can also exec a command inside a container.  This includes <code>postStart</code> and <code>preStop</code> hooks and probes.  These ultimately become an <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#execaction-v1-core" rel="nofollow noreferrer">ExecAction</a> API object.  The command there is documented to not run inside a shell and can't directly use the <code>$(VAR_NAME)</code> syntax, but if you do run a shell explicitly, it should be able to use container-level environment variables.  (If your <code>command:</code> or Dockerfile <code>ENTRYPOINT</code> sets environment variables, these will not be visible, similar to <code>docker exec</code>.)</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata: {name: for-example}
spec:
  containers:
    - name: for-example
      env:
        - name: VAR_NAME
          value: a concrete string
        - name: COPY_OF_VAR_NAME
          value: '$(VAR_NAME)'
      command:
        - /bin/echo
        - '$(VAR_NAME)'
      args:
        - '$(VAR_NAME), again'
      livenessProbe:
        exec:
          command:
            - /bin/sh
            - -c
            - echo $VAR_NAME gets expanded by the shell
      lifecycle:
        postStart:
          exec:
            command:
              - /bin/sh
              - -c
              - echo $VAR_NAME gets expanded by the shell
</code></pre>
