<p>I am trying to play with init pods. I want to use <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="noreferrer">init container</a> to create file and default container to check if file exist and sleep for a while.</p>

<p>my yaml:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod
spec:
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ -e /workdir/test.txt ]; then sleep 99999; fi']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'mkdir /workdir; echo&gt;/workdir/test.txt']
</code></pre>

<p>When I am trying to debug from alpine image I use the command to create:</p>

<blockquote>
  <p>kubectl run alpine --rm -ti --image=alpine /bin/sh</p>
</blockquote>

<pre><code>If you don't see a command prompt, try pressing enter.
/ # if [ -e /workdir/test.txt ]; then sleep 3; fi
/ # mkdir /workdir; echo&gt;/workdir/test.txt
/ # if [ -e /workdir/test.txt ]; then sleep 3; fi
/ *here shell sleeps for 3 seconds
/ #
</code></pre>

<p>And it seems like commands working as expected. </p>

<p>But on my real k8s cluster I have only CrashLoopBackOff for main container. </p>

<blockquote>
  <p>kubectl describe pod init-test-pod</p>
</blockquote>

<p>Shows me only that error:</p>

<pre><code>Containers:
  myapp-container:
    Container ID:  docker://xxx
    Image:         alpine
    Image ID:      docker-pullable://alpine@sha256:xxx
    Port:          &lt;none&gt;
    Host Port:     &lt;none&gt;
    Command:
      sh
      -c
      if [ -e /workdir/test.txt ]; then sleep 99999; fi
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
    Ready:          False
    Restart Count:  3
    Environment:    &lt;none&gt;
</code></pre>

golang
<p>The problem here is that your main container is not finding the folder you create. When your initial container completes running, the folder gets wiped with it. You will need to use a Persistent Volume to be able to share the folder between the two containers:</p>

<pre><code>kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mypvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod
spec:
  volumes:
  - name: mypvc
    persistentVolumeClaim:
      claimName: mypvc
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ -f /workdir/test.txt ]; then sleep 99999; fi']
    volumeMounts:
    - name: mypvc
      mountPath: /workdir
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'mkdir /workdir; echo&gt;/workdir/test.txt']
    volumeMounts:
    - name: mypvc
      mountPath: /workdir
</code></pre>

<p>You can as well look at <code>emptyDir</code>, so you won't need the PVC:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: init-test-pod
spec:
  volumes:
  - name: mydir
    emptyDir: {}
  containers:
  - name: myapp-container
    image: alpine
    command: ['sh', '-c', 'if [ -f /workdir/test.txt ]; then sleep 99999; fi']
    volumeMounts:
    - name: mydir
      mountPath: /workdir
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'mkdir /workdir; echo&gt;/workdir/test.txt']
    volumeMounts:
    - name: mydir
      mountPath: /workdir
</code></pre>
