<p>I'm using the simplest docker ever:</p>

<pre><code>FROM ubuntu
COPY script.sh /script.sh
CMD /script.sh
</code></pre>

<p>Where all the script does is:</p>

<pre><code>#!/bin/bash

function sigterm() {
    echo "Got SIGTERM"
    exit
}

trap sigterm SIGTERM

i=1
while true; do
    echo "$(date +%H:%M:%S) | $((i++)) | $HOSTNAME"
    sleep 1
done
</code></pre>

<p>I'm running this container in Minikube, <strong>but I can't get it to catch any SIGTERM from kubernetes.</strong> </p>

<p>I tried deleting the pod/deployment or to scale it up and down. In no case it got SIGTERM before being deleted. It respects the <code>terminationGracePeriodSeconds</code> But doesn't seem to run the <code>preStop</code> command or send the SIGTERM before killing the pod.</p>

<p>Is that due to using minikube? or am I doing something else wrong?</p>

<p>(the deployment is not part of a service, it's just a deployment)
(SSH into the pod and manually <code>kill</code>-ing it works as expected)</p>

Amir Mehler
<p>Reading <a href="https://docs.docker.com/engine/reference/builder/#cmd" rel="noreferrer">the Dockerfile documentation on CMD</a></p>
<blockquote>
<p>The CMD instruction has three forms:</p>
<ul>
<li>CMD [&quot;executable&quot;,&quot;param1&quot;,&quot;param2&quot;] (exec form, this is the preferred form)</li>
<li>CMD [&quot;param1&quot;,&quot;param2&quot;] (as default parameters to ENTRYPOINT)</li>
<li>CMD command param1 param2 (shell form)</li>
</ul>
<p>If you use the shell form of the CMD, then the  will execute in /bin/sh -c</p>
</blockquote>
<p>So you are using the shell form, which means your command is /bin/sh -c script.sh. Then, when kubernetes sends a SIGTERM to the container, is not the script.sh process the one receiving the signal, but the /bin/sh process. That's why you don't see the &quot;Got SIGTERM&quot; message.</p>
<p>When creating a Dockerfile, make sure you use the exec form. Otherwise the application will be started as a subcommand of /bin/sh -c, which does not pass signals. The container’s PID1 will be the shell, your application will not receive any signals.</p>
<p>Try changing your Dockerfile to use the exec form</p>
<pre><code>FROM ubuntu
COPY script.sh /script.sh
CMD [&quot;/script.sh&quot;]
</code></pre>
