jazzdd86 / alpine-flask

72 stars 51 forks source link

Calling docker stop on image results in exit 137 #5

Open rmetcalf9 opened 6 years ago

rmetcalf9 commented 6 years ago

I have created an image based on alpine-flask. When I run the image in either debug or production modes and call docker stop it waits for a while (around 10 seconds) and then exits. When I use docker ps -a to check I find that it has exited with code 137.

I believe this is because docker sends a term signal to process ID 1, it will then wait for 10 seconds and if it hasn't ended will then kill the process.

I have checked this out in the python3 debug mode. The problem here is that this image uses a script to execute python3. The term signal is sent to process id 1 (the bash script) not the python process. A solution to this is described here: https://veithen.github.io/2014/11/16/sigterm-propagation.html

Is this a possible enhancement to this project?

jazzdd86 commented 6 years ago

Very good point. I will look into it and according to your linked article, it should be quite simple to add sigterm propagation. Give me some time and I will add it.

rmetcalf9 commented 6 years ago

During my experiments I have found I need to include code similar to the following in the flask app in order to get it to process the signals correctly:

#Code required to ensure that container will exit when a signal is received
class ServerTerminationError(Exception):
    def __init__(self):
        pass
    def __str__(self):
        return "Server Terminate Error"

def exit_gracefully(signum, frame):
    print("Exit Gracefully called")
    raise ServerTerminationError()

signal.signal(signal.SIGINT, exit_gracefully)
signal.signal(signal.SIGTERM, exit_gracefully) #sigterm is sent by docker stop command
try:
    app.run(host='0.0.0.0', port=80, debug=False)
except ServerTerminationError as e:
    print("Stopped")