title: Docker stop shutdown container almost immediately despite `-t 120` setting
body: <p>This is how I build the docker images:</p>
  
  <pre><code>docker build -f Dockerfile -t python-trapterm .
  </code></pre>
  
  <p>and it is how I run it:</p>
  
  <pre><code> docker run -t python-trapterm:latest
  </code></pre>
  
  <p>I try to stop it with</p>
  
  <pre><code> docker stop -t 120 sharp_jones
  </code></pre>
  
  <p>I expect there is some 2 minutes of run time before the container shuts down. But it almost shuts down immediately? Why?</p>
  
  <p>This is the output from the container process:</p>
  
  <pre><code>&gt; docker run -t python-trapterm:latest
  Setting traps...
   * Starting NTP server ntpd                                              [ OK ] 
  2017-09-10 06:43:02,501 pid 26 tid 140693944227584 INFO     greatapp Registering signal handler
  2017-09-10 06:43:02,502 pid 26 tid 140693944227584 INFO     greatapp Hello!
  2017-09-10 06:43:04,507 pid 26 tid 140693944227584 INFO     greatapp Checking every 2s...
  2017-09-10 06:43:06,510 pid 26 tid 140693944227584 INFO     greatapp Checking every 2s...
  2017-09-10 06:43:08,512 pid 26 tid 140693944227584 INFO     greatapp Checking every 2s...
  [snip]
  2017-09-10 06:44:12,629 pid 26 tid 140693944227584 INFO     greatapp Checking every 2s...
  2017-09-10 06:44:14,631 pid 26 tid 140693944227584 INFO     greatapp Checking every 2s...
  Sending term to 26...
  2017-09-10 06:44:16,537 pid 26 tid 140693944227584 INFO     greatapp Handling shutdown...15
  2017-09-10 06:44:16,538 pid 26 tid 140693944227584 INFO     greatapp Checking every 2s...
  2017-09-10 06:44:16,539 pid 26 tid 140693944227584 INFO     greatapp Handling shutdown...10
  2017-09-10 06:44:16,539 pid 26 tid 140693944227584 INFO     greatapp Checking every 2s...
  </code></pre>
  
  <p>You can see the TERM signal is received at around 06:44:16 but the job already got shutdown around 06:44:14 but the process gets terminated after 06:44:16. There is not a 10 seconds wait.</p>
  
  <p>This is my docker file:</p>
  
  <pre><code>FROM ubuntu:16.04
  
  RUN apt-get update --fix-missing &amp;&amp; apt-get install -y \
    git \
    python \
    python-pip \
    ntp \
    curl vim screen lsof
  
  RUN mkdir -p /app
  
  COPY ./app /app
  
  CMD ["/app/start-app.sh"]
  </code></pre>
  
  <p>Here are the two files in my <code>app</code> directory:</p>
  
  <p><strong>app/start-app.sh</strong></p>
  
  <pre><code>#!/bin/bash
  set -eo pipefail
  
  echo "Setting traps..."
  trap 'cleanup_term' TERM
  trap 'cleanup_kill' KILL
  
  cleanup_term() {
      echo "Sending term to `jobs -p`..."
      kill -TERM `jobs -p`
      kill -USR1 `jobs -p`
  }
  
  cleanup_kill() {
      echo "Received kill..."
      kill -TERM `jobs -p`
  }
  
  
  service ntp start
  
  cd /app
  python -m greatapp &amp;
  
  wait
  </code></pre>
  
  <p>and </p>
  
  <p><strong>app/greatapp.py</strong></p>
  
  <pre><code>import os
  import time
  import logging
  import signal
  
  
  LOG_FORMAT = "%(asctime)-15s pid %(process)d tid %(thread)4d %(levelname)-8s" \
      " %(module)-8s %(message)s"
  
  logging.basicConfig(format=LOG_FORMAT)
  LOG = logging.getLogger(__file__)
  LOG.setLevel(logging.DEBUG)
  
  def docker_shutdown_handler(_signum, _frame):
      LOG.info("Handling shutdown...%s", _signum)
      # sys.exit()
  
  
  LOG.info('Registering signal handler')
  signal.signal(signal.SIGTERM, docker_shutdown_handler)
  signal.signal(signal.SIGUSR1, docker_shutdown_handler)
  
  
  def main():
      LOG.info("Hello!")
      sleep_time = 2
      while True:
          time.sleep(sleep_time)
          LOG.info("Checking every %ds...", sleep_time)
  
  
  if __name__ == '__main__':
      main()
  </code></pre>