Skip to content

Instantly share code, notes, and snippets.

@fortejas
Last active April 13, 2022 08:34
Show Gist options
  • Save fortejas/92dd1144333c471bea53b5532b554635 to your computer and use it in GitHub Desktop.
Save fortejas/92dd1144333c471bea53b5532b554635 to your computer and use it in GitHub Desktop.
Python script to catch the Docker SIGTERM signal

Python - Catch SIGTERM signal

Overview

This Docker image will catch the SIGTERM signal and then take STOP_TIMEOUT seconds to cleanup.

Getting Started

$ # Build the Docker image.
$ docker build -t slow-stop .

$ # Run the container, when the SIGTERM is received spend 10 seconds doing "cleanup".
$ docker run -dit -e STOP_TIMEOUT=10 -p 8000:8000 slow-stop:latest
FROM python:3-onbuild
EXPOSE 8000
CMD ["./server.py"]
#!/usr/bin/env python3
"""
Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://www.apache.org/licenses/LICENSE-2.0
or in the "license" file accompanying this file. This file is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License.
"""
import time
import signal
import os
from multiprocessing import Process
from flask import Flask, jsonify
STOP_TIMEOUT = int(os.environ.get('STOP_TIMEOUT', '10'))
PORT = int(os.environ.get('PORT', '8000'))
APP = Flask(__name__)
@APP.route('/')
def home():
return jsonify(**{'status': 'ok'}), 200
def run():
APP.run(host='0.0.0.0', port=PORT)
def signal_handler(sig, _frame):
"""Handling the SIGTERM event"""
print(f'Received signal {sig} - stopping gracefully')
count = STOP_TIMEOUT
while count > 0:
print(f'cleaning up: {count}')
time.sleep(1)
count -= 1
print('Finished cleanup...')
def main():
# Create a process and pipe to pass messages to the process.
process = Process(target=run)
process.start()
signal.signal(signal.SIGTERM, signal_handler)
signal.pause()
process.join()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment