<p>I tried to set up pod which use init container to create file. If the file does not exist it should terminate immediately. Here is my yaml file</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod4
spec:
  volumes:
  - name: my-pvc
    persistentVolumeClaim:
      claimName: mypvc
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ **-f /workdir2/test.txt** ]; then exit 0; fi']
    volumeMounts:
    - name: mypvc
      mountPath: /workdir
  initContainers:
  - name: init-service
    image: busybox:1.28
    command: ['sh', '-c', '**mkdir /workdir; echo&gt;/workdir/test.txt**']
    volumeMounts:
    - name: my-pvc
      mountPath: /workdir 
</code></pre>

<p>After i create yaml file i see this output.</p>

<pre><code>init-test-pod4   0/1     Completed   1          11s
init-test-pod4   0/1     CrashLoopBackOff   1          22s
init-test-pod4   0/1     Completed          2          24s
init-test-pod4   0/1     CrashLoopBackOff   2          36s
init-test-pod4   0/1     Completed          3          51s
init-test-pod4   0/1     CrashLoopBackOff   3          61s
init-test-pod4   0/1     Completed          4          99s
init-test-pod4   0/1     CrashLoopBackOff   4          111s
</code></pre>

<p>How to change the command which terminate the pod immediately after it did not find right file ?</p>

O.Man
<p>Default <code>restartPolicy</code> for a Kubernetes pod is <code>always</code>. Init-Container keeps restart when a given task fails. To prevent this, explicitly we should mention <code>restartPolicy: Never</code> in pod's manifest. But still, it won't be terminated. The init-container state will be moved to <code>Completed</code>. </p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod4
spec:
  restartPolicy: Never
  volumes:
  - name: my-pvc
    emptyDir: {}
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ **-f /workdir2/test.txt** ]; then exit 0; fi']
    volumeMounts:
    - name: my-pvc
      mountPath: /workdir
  initContainers:
  - name: init-service
    image: busybox:1.28
    command: ['sh', '-c', '**mkdir /workdir; echo&gt;/workdir/test.txt**']
    volumeMounts:
    - name: my-pvc
      mountPath: /workdir
</code></pre>

<hr>

<pre><code>kubectl get po init-test-pod4
NAME             READY   STATUS      RESTARTS   AGE
init-test-pod4   0/1     Completed   0          10m
</code></pre>
