<p>I am trying to create a little Pod example with two containers that share data via an emptyDir volume. In the first container I am waiting a couple of seconds before it gets destroyed.</p>

<p>In the postStart I am writing a file to the shared volume with the name "started", in the preStop I am writing a file to the shared volume with the name "finished".</p>

<p>In the second container I am looping for a couple of seconds outputting the content of the shared volume but the "finished" file never gets created. Describing the pod doesn't show an error with the hooks either. </p>

<p>Maybe someone has an idea what I am doing wrong</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: shared-data-example
  labels:
    app: shared-data-example
spec:
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:

  - name: first-container
    image: ubuntu
    command: ["/bin/bash"]
    args: ["-c", "for i in {1..4}; do echo Welcome $i;sleep 1;done"]
    imagePullPolicy: Never
    env:
    - name: TERM
      value: xterm
    volumeMounts:
    - name: shared-data
      mountPath: /myshareddata
    lifecycle:
      preStop:
        exec:
          command: ["/bin/sh", "-c", "echo First container finished &gt; /myshareddata/finished"]
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo First container started &gt; /myshareddata/started"]

  - name: second-container
    image: ubuntu
    command: ["/bin/bash"]
    args: ["-c", "for i in {1..20}; do ls /myshareddata;sleep 1;done"]
    imagePullPolicy: Never
    env:
    - name: TERM
      value: xterm
    volumeMounts:
    - name: shared-data
      mountPath: /myshareddata
  restartPolicy: Never
</code></pre>

<p>It is happening because the final status of your pod is <code>Completed</code> and applications inside containers stopped without any external calls.</p>

<p>Kubernetes runs <code>preStop</code> hook only if pod resolves an external signal to stop. Hooks were made to implement a graceful custom shutdown for applications inside a pod when you need to stop it. In your case, your application is already gracefully stopped by itself, so Kubernetes has no reason to call the hook.</p>

<p>If you want to check how a hook works, you can try to create <code>Deployment</code> and update it's image by <code>kubectl rolling-update</code>, for example. In that case, Kubernetes will stop the old version of the application, and <code>preStop</code> hook will be called.</p>
