I am new to Docker containers and still in the learning phase. I am trying to test the below code in my Kubernetes free account.
apiVersion: v1
kind: Pod
metadata:
name: two-containers
spec:
restartPolicy: Never
volumes:
- name: shared-data
emptyDir: {}
containers:
- name: nginx-container
image: nginx
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
- name: debian-container
image: debian
volumeMounts:
- name: shared-data
mountPath: /pod-data
command: ["/bin/sh"]
args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
this part is working fine.
However when I run the Debian container it terminates immediately after writing /pod-data/index.html
to shared volume path, and I cannot figure out why this is happening.
Has anyone come across this scenario before, who has a possible solution.
Because it executes command specified and then finishes. Script is finished, there is nothing more to do, so pod is terminated. Pods and Docker containers are supposed to be long running processes. If you need to run script that runs and then completes - you should consider using Jobs.
command: ["/bin/sh"]
args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
if you look at the debian container, you see that it is writing 'Hello from the debian container' to /pod-data/index.html.
the process is complete and there is nothing more to be done. hence the container gets terminated.
On the other hand, the nginx container runs a daemon process that runs all the time serving the web content to end users and hence it would continue to run.