Python how to recieve SIGINT in Docker to stop service?

  docker, python, sigint, sigkill, signals

I’m writing a monitor service in Python that monitors another service and while the monitor & scheduling part works fine, I have a hard time figuring out how to do a proper shutdown of the service using a SIGINT signal send to the Docker container. Specifically, the service should catch the SIGINT from either a docker stop or a Kubernetes stop signal, but so far it doesn’t. I have reduced the issue to a minimal test case which is easy to replicate in Docker:

import signal
import sys
import time

class MainApp:

    def __init__(self):
        self.shutdown = False
        signal.signal(signal.SIGINT, self.exit_gracefully)
        signal.signal(signal.SIGTERM, self.exit_gracefully)

    def exit_gracefully(self, signum, frame):
        print('Received:', signum)
        self.shutdown = True

    def start(self):
        print("Start app")

    def run(self):
        print("Running app")

    def stop(self):
        print("Stop app")

if __name__ == '__main__':

    app = MainApp()

    # This boolean flag should flip to false when a SIGINT or SIGTERM comes in...
    shutdown = app.shutdown

    while not shutdown:

    else: # However, this code gets never executed ...

And the corresponding Dockerfile, again minimalistic:

FROM python:3.8-slim-buster
COPY test/ .
CMD [ "python", "" ]

I opted for Docker because the Docker stop command is documented to issue a SIGINT signal, waits a bit, and then issues a SIGKILL. This should be an ideal test case.

However, when starting the docker container with an interactive shell attached, and stopping the container from a second shell, the stop() code never gets executed. Verifying the issue, a simple:

$ docker inspect -f '{{.State.ExitCode}}' 64d39c3b

Shows exit code 137 instead of exit code 0.

Apparently, one of two things is happening. Either the SIGTERM signal isn’t propagated into the container or Python runtime and this might be true because the exit_gracefully function isn’t called apparently otherwise we would see the printout of the signal. I know that you have to be careful about how to start your code from within Docker to actually get a SIGINT, but when adding the stop signal line to the Dockerfile, a global SIGINT should be issued to the container, at least to my humble understanding reading the docs.

Or, the Python code I wrote isn’t catching any signal at all. Either way, I simply cannot figure out why the stop code never gets called. I spent a fair amount of time researching the web, but at this point, I feel I’m running circles, Any idea how to solve the issue of correctly ending a python script running inside docker using a SIGINT signal?

Thank you


Source: Docker Questions