When deploying a containerized application to a container management system like AWS Fargate, you tend to run your application from a shell script. Suppose your script looks like this
|
|
Here we are executing the gunicorn service with PID 1 when the container is deployed. Suppose we want to terminate the container with a docker stop <container_id>, the command will send a SIGTERM to the container. As the gunicorn process is PID 1, this signal is ignored.
The way to resolve this issue is to use exec before the command to start your application. The last line of the above shell script should be gunicorn --config config/gunicorn/$GUNICORN_CONFIG.py config.wsgi. A simple example is shown below
t.py:
|
|
entry.sh:
|
|
Dockerfile:
|
|
|
|
Both times, you can run docker stop in another window. If the container doesn’t stop within 10 seconds, then it’s killed.
Most apps do not explicitely handle SIGTERM the way t.py does. If you replace t.py with
|
|
SIGINT will work with keyboard interrupt but docker stop does not because of PID=1 issue.
If we run docker with a --init option to force a non 1 PID, the docker stop works whether we use exec in the script or not.
In order to use this feature with AWS Fargate, make a small change to your AWS::ECS::TaskDefinition in your cloudformation as shown below
|
|