<p>What I found out so far:</p>
<ul>
<li>A &quot;docker stop&quot; sends a SIGTERM to process ID 1 in the container.</li>
<li>The process ID 1 in the container is the java process running tomcat.*)</li>
<li>Yes, tomcat itself shuts down gracefully, but not do so the servlets.</li>
<li>Servlets get killed after 2 seconds, even if they are in the middle of processing a reguest(!!)</li>
</ul>
<p>*) Side note:
Although our container entrypoint is  [ &quot;/opt/tomcat/bin/catalina.sh&quot;, &quot;run&quot; ], but
in catalina.sh the java process is started via the bash buildin &quot;exec&quot; command,
and therefore the java process <em>replaces</em> the shell process and hereby becomes the new process id 1.
(I can verify this by exec into the running container and do a &quot;ps aux&quot; in there.)
Btw, I am using tomcat 7.0.88.</p>
<p>I found statements about tomcat doing gracefull shutdown by default (<a href="http://tomcat.10.x6.nabble.com/Graceful-Shutdown-td5020523.html" rel="nofollow noreferrer">http://tomcat.10.x6.nabble.com/Graceful-Shutdown-td5020523.html</a> - &quot;any in-progress connections will complete&quot;), but all I can see is that the SIGTERM which is sent from docker to the java process results in hardly stopping the ongoing execution of a request.</p>
<p>I wrote a little rest servlet to test this behaviour:</p>
<pre><code>import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.core.Response.Status;

@Path(&quot;/&quot;)
public class SlowServerRes
{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path(&quot;test1&quot;)
    public Response test1(@QueryParam(&quot;sleep&quot;) final int sleepDurationSec)
    {
        long received = System.currentTimeMillis();
        System.out.println(&quot;+++++++++++++++++++++ received request at &quot; + received);
        
        for (int i=1; i &lt;= sleepDurationSec; i++) {         
            System.out.println(&quot;  ++++ Sleeping for 1 sec (&quot;+i+&quot;)&quot;);
            try { Thread.sleep(1000); }
            catch (InterruptedException e) { 
                System.out.println(&quot;   Sleep was interrupted at second &quot; + i + &quot; ... ignoring/continue sleeping.&quot;); 
            }
        }
        
        long finished = System.currentTimeMillis();
        String result = &quot;received: &quot; + received + &quot;  finished: &quot; + finished;
        System.out.println(&quot;+++++++++++++++++++++ &quot; + result);
        Response response = Response.status(Status.OK).entity(result).build();
        return response;
    }
}
</code></pre>
<p>After intensive googling I finally came across this posting:
<a href="http://grokbase.com/t/tomcat/users/113nayv5kx/tomcat-6-graceful-shutdown" rel="nofollow noreferrer">http://grokbase.com/t/tomcat/users/113nayv5kx/tomcat-6-graceful-shutdown</a></p>
<p>So the grace period that is given to tomcat is NOT propagated as a grace period for the servlets.
I wonder weather this makes much sense, but it looks like this is the fact.
So the only way to give servlets the possibility to properly end their ongoing requests is to change
the &quot;unloadDelay&quot; (<a href="https://tomcat.apache.org/tomcat-7.0-doc/config/context.html" rel="nofollow noreferrer">https://tomcat.apache.org/tomcat-7.0-doc/config/context.html</a>).</p>
<p>However, I did not find the right place in the tomcat config files to define a non-default unloadDelay. In case this matters, my main concern is about jersey servlets (org.glassfish.jersey.servlet.ServletContainer).</p>
<p>Or maybe there are other possibilities, which I don't see by now?</p>
<p>(I added kubernetes to the tags list, because this may be of major concern especially for Kubernetes, as it relocates (docker stop-&gt;SIGTERM) containers quite often, just to keep the load balanced.)</p>

Thomas Schütt
<p>Now I found the answer here: <a href="https://stackoverflow.com/a/11154770/2081279">https://stackoverflow.com/a/11154770/2081279</a></p>

<p>It worked for me under linux with </p>

<pre><code>&lt;Context path="/myapp" unloadDelay="10000"/&gt; 
</code></pre>

<p>but only with upper letter "C"ontext.</p>
