apiVersion: v1
kind: Pod
metadata:
name: init-container-test
spec:
containers:
- name: application-container
image: alpine
command: [‘sh’, ‘-c’, ‘if [ -f /work/sharedfile.txt ]; then sleep 99999; else exit; fi’]
volumeMounts:
- name: workdir-volume
mountPath: /work
initContainers:
- name: init-container
image: busybox:1.28
command: [‘sh’, ‘-c’, ‘mkdir /work; echo>/work/sharedfile.txt’]
volumeMounts:
- name: workdir-volume
mountPath: / work
volumes:
- name: workdir-volume
emptyDir: {}
ballu
2
Try this out
apiVersion: v1
kind: Pod
metadata:
name: init-container-test
spec:
containers:
- name: application-container
image: alpine
command: [“/bin/sh”, “-c”, “if [ -f /work/sharedfile.txt ];then sleep 9999;else exit 1;fi”]
volumeMounts:
- name: workdir-volume
mountPath: /work
initContainers:
- name: busybox
image: busybox
command: [“/bin/sh”, “-c”, “touch /work/sharedfile.txt”]
volumeMounts:
- name: workdir-volume
mountPath: /work
volumes:
- name: workdir-volume
emptyDir: {}
Try this, it works
apiVersion: v1
kind: Pod
metadata:
name: init-demo
spec:
initContainers:
- name: init-container
image: nginx
volumeMounts:
- name: init-vol
mountPath: /workdir
command: [‘/bin/sh’, ‘-c’, ‘mkdir workdir; touch /workdir/test.txt’]
containers:
- name: main-container
image: httpd
volumeMounts:
- name: init-vol
mountPath: /workdir
command: [‘/bin/sh’, ‘-c’, ‘if [ -f /workdir/test.txt ]; then sleep 9999; fi’]
volumes:
- name: init-vol
emptyDir: {}