<p>I have a pod with multiple init containers and one main container. One of the init container create a sh file with some export commands like:</p>
<pre><code>export Foo=Bar
</code></pre>
<p>I want to source the file so it creates the env variable like this:</p>
<pre><code> containers:
    - name: test
      command:
        - &quot;bash&quot;
        - &quot;-c&quot;
      args:
        - &quot;source /path/to/file&quot;
</code></pre>
<p>It doesn't create the env variable. But if I run the source command directly in the container it works. What is the best way to do this using the command option in the pod definition?</p>

Nisman
<p>If you are looking for create the <code>sh</code> in the init container with the variables and then use in the &quot;main container&quot; here is a quick example:</p>
<p><strong>manifest</strong></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: mypod
  labels:
    name: mypod
spec:
  initContainers:
  - name: my-init-container
    image: alpine:latest
    command: [&quot;sh&quot;, &quot;-c&quot;, &quot;echo export Foo=bar &gt; /shared/script.sh &amp;&amp; chmod +x /shared/script.sh&quot;]
    volumeMounts:
      - name: shared
        mountPath: /shared
  containers:
  - name: mycontainer
    image: mycustomimage
    resources:
      limits:
        memory: &quot;32Mi&quot;
        cpu: &quot;100m&quot;
    volumeMounts:
      - name: shared
        mountPath: /shared
  volumes:
    - name: shared
</code></pre>
<p><strong>Dockerfile</strong></p>
<pre><code>FROM alpine:latest

COPY entrypoint.sh .

ENTRYPOINT [&quot;./entrypoint.sh&quot;]
CMD ...
</code></pre>
<p><strong>entrypoint.sh</strong></p>
<pre class="lang-bash prettyprint-override"><code>#!/bin/sh
. /shared/script.sh
env
exec &quot;$@&quot;
</code></pre>
<p><strong>logs</strong></p>
<pre class="lang-bash prettyprint-override"><code>$ kubectl logs pod/mypod
&lt;...&gt;
Foo=bar
&lt;...&gt;
</code></pre>
<p>As you can see we can created a script file in the init container with <code>Foo=bar</code> variable and source the file in the &quot;main container&quot;, the script is there the volume <code>shared</code> mounted in both containers.</p>
<p>Most of the situations we use configMaps/secrets/vaults and inject that as variables in the containers as the others answers mentioned. I recommend checking if those can solve your problem first.</p>
