<p>I have some issue with kubernetes yaml.</p>
<p>I want to execute touch to make file and copy it to a folder but the container stopped.</p>
<pre><code>containers:
  - name: 1st
    image: alpine
    volumeMounts:
    - name: sharedvolume
      mountPath: /usr/share/sharedvolume
    command: [&quot;/bin/sh&quot;]
    args: [&quot;-c&quot;, &quot;cd ~/ &amp;&amp; touch file.txt &amp;&amp; cp file.txt /usr/share/sharedvolume&quot;]
</code></pre>
<p>I googled through many stackoverflow answers and tried them but nothing worked. I also tried to combine the args on commands but still didn't work.</p>

arsaphone
<p>First of all, please keep in mind that a container exits when its main process exits.<br />
After container in a Pod exits, the <code>kubelet</code> restarts it or not depending on the <code>spec.restartPolicy</code> field of the Pod (see: <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy" rel="nofollow noreferrer">Container restart policy documentation</a>).</p>
<hr />
<p>As a workaround, you can try adding the <code>sleep infinity</code> command to the end of the <code>args</code> field (it should work in most cases).</p>
<p>Take a look at the example below:<br />
<strong>NOTE:</strong> I just want you to pay attention to the <code>sleep infinity</code> at the end of the <code>args</code> filed.</p>
<pre><code>$ cat alpine.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: alpine
  name: alpine
spec:
  containers:
  - image: alpine
    name: alpine
    command: [&quot;/bin/sh&quot;]
    args: [&quot;-c&quot;, &quot;cd ~/ &amp;&amp; touch file.txt &amp;&amp; cp file.txt /usr/share/sharedvolume &amp;&amp; sleep infinity&quot;]
    volumeMounts:
    - name: sharedvolume
      mountPath: /usr/share/sharedvolume
...

$ kubectl apply -f alpine.yml
pod/alpine created

$ kubectl get pods
NAME     READY   STATUS    RESTARTS   AGE
alpine   1/1     Running   0          12s

$ kubectl exec -it alpine -- sh

/ # df -h | grep &quot;/usr/share/sharedvolume&quot;
/dev/sdb                  7.8G     36.0M      7.8G   0% /usr/share/sharedvolume

/ # ls /usr/share/sharedvolume
file.txt    lost+found
</code></pre>
<p>As you can see the <code>alpine</code> Pod is running, so it works as expected.</p>
<hr />
<p>As I wrote before, adding the <code>sleep infinity</code> is a kind of workaround. It's worth considering what the main process of this container should be.<br />
Useful information on how to use the <code>alpine</code> image can be found in the <a href="https://hub.docker.com/_/alpine" rel="nofollow noreferrer">How to use this image reference</a>.</p>
