<p>I have a <code>yaml</code>manifest with <code>parallelism: 2</code>, including one <code>initContainers</code>. The command in the <code>initContainers</code>, therefore, runs two times and cause problems to the main command. How can I make it run only once?</p>
<p>Here are the important parts of the <code>yaml</code></p>
<pre><code>kind: Job
apiVersion: batch/v1
metadata:
  name: bankruptcy
spec:
  parallelism: 2
  template:
    metadata:
      labels:
        app: bankruptcy
    spec:
      restartPolicy: Never
      containers:
        - name: bankruptcy
          image: &quot;myimage&quot;
          workingDir: /mount/
          command: [&quot;bash&quot;,&quot;./sweep.sh&quot;]
          resources:
            limits:
              nvidia.com/gpu: 1
      initContainers:
        - name: dev-init-sweep
          image: 'myimage'
          workingDir: /mount/
          command: ['/bin/bash']
          args:
            - '--login'
            - '-c'
            - 'wandb sweep ./sweep.yaml 2&gt;&amp;1 | tee ./wandb/sweep-output.txt; echo `expr &quot;$(cat ./wandb/sweep-output.txt)&quot; : &quot;.*\(wandb agent.*\)&quot;` &gt; ./sweep.sh;'
</code></pre>

user1571823
<p>An initContainer runs once per Pod.
You can't make the initContainer run only once for a given number of pods. But you could implement a guard as part of your initContainer that detects that another one has already started and just returns without performing an own operation or waits until a condition is met.</p>
<p>You have to implement it yourself, though, there is no support from Kubernetes for this.</p>
