I was trying to create a pod using yaml file, that can run few commands as soon as it started. The yaml files looks like:
kind: Pod
apiVersion: v1
metadata:
name: testmultiplecmds
spec:
restartPolicy: Never
containers:
- name: nginxmultiplecmds
image: nginx
command:
- "/bin/bash"
- -c
- "ls -ltr"
- "/bin/bash"
- -c
args:
- "date ; sleep 36"
but everytime I run this, it only outputs the results of /bin/bash -c ls -ltr, which is basically the first command and everything else is ignored. I tried multiple combination like:
kind: Pod
apiVersion: v1
metadata:
name: testmultiplecmds
spec:
restartPolicy: Never
containers:
- name: nginxmultiplecmds
image: nginx
command:
- "/bin/bash"
- -c
args:
- "date ;"
- "sleep 30"
but in this also, it just prints out the date and does not sleep. But when I do:
kind: Pod
apiVersion: v1
metadata:
name: testmultiplecmds
spec:
restartPolicy: Never
containers:
- name: nginxmultiplecmds
image: nginx
command:
- "/bin/bash"
- -c
args:
- "date ; sleep 30"
it works.
So my question is, Why we cannot run multiple commands (specified in different lines as different items not under single string) even though commands and args takes []String and if we cannot run multiple commands, then why they take []String, they should simply take String?