<p>Given is a microservice that after some time needs to quit itself. This is no error condition but (in that case) normal behavior.
After exiting it should be automatically restarted.</p>

<p>So currently I have a script <code>run_app.sh</code>:</p>

<pre><code>#!/usr/bin/env bash
while true; do ./app ; done
</code></pre>

<p>And in the <code>Dockerfile</code> (inheriting <code>FROM ubuntu:16.04</code>) I run it like this:</p>

<pre><code>CMD ["./run_app.sh"]
</code></pre>

<p>It "works" but since <code>app</code> does not have PID 1, it does not receive SIGTERM etc. coming from Kubernetes, which is needed for a graceful shutdown during rolling updates etc.</p>

<p>Using <code>while true; do exec ./app ; done</code> in <code>run_app.sh</code> does not solve the problem since the loop no longer exists when <code>app</code> finished and no restart is performed.</p>

<p>How can I restart the app automatically inside the container without restarting the container / pod every time it quits, but still have the advantages of PID 1?</p>

<p>Well, your loop <em>does</em> restart your app, so this is not your problem.  Your problem is that the signal sent to the docker container is not propagated into the container.  Docker just isn't (AFAIK) not meant to be used like this, so it doesn't propagate signals to its app.</p>

<p>You have two ways of handling this:</p>

<ol>
<li><p>You can teach the signal sender (Kubernetes or whatever) to instead of sending a signal to the Docker container do something more elaborate to get the information to the app inside the container.  I guess this is not easy (if possible at all).</p></li>
<li><p>You can migrate the looping shell script outside of the container and let Kubernetes (or whatever) run this script instead.  <em>Within</em> the loop you then can start the Docker container with your app inside.  In this case you will need to catch the SIGTERM in the outside looping shell script (<code>help trap</code>) and either send a SIGTERM to the Docker container directly or to the app within the Docker container (using <code>Docker exec</code> or similar).</p></li>
</ol>
