Meraj Rasool
Meraj Rasool

Reputation: 751

Docker shutdown hook or support for graceful exit

I am running a docker container(s) via ECS. When launched the container runs a process which will perform some specific actions. These actions may take a few minutes to complete whenever required.

The ECS will be running multiple EC2 instances in a cluster (and each instance running multiple containers). These containers' launch will be auto scaled based on some specific metrics we are reporting to CloudWatch.

The problem comes when we need to scale down the containers. We need to gracefully shutdown the containers (as they would be running the process) and I would not like it to kill the container without completing the running process on it.

I have seen and Docker doesn't support shutdown hooks https://github.com/moby/moby/issues/2100

One way I was thinking to do this is to scale down the no. of instances by application itself by keeping track of running instances and their status. I was just wondering if there is any other solution / idea in this regard?

Upvotes: 2

Views: 3663

Answers (1)

Yuri Mir
Yuri Mir

Reputation: 21

While docker does not support shutdown hooks, it is possible to use SIGINT and SIGTERM signals for main docker process. Just define PID of process and use this in entrypoint.sh:

trap "{ echo Received SIGTERM; kill -s SIGTERM $MAIN_PROCESS_ID; wait $MAIN_PROCESS_ID; }" SIGTERM
trap "{ echo Received SIGINT; kill -s SIGINT $MAIN_PROCESS_ID; wait $MAIN_PROCESS_ID; }" SIGINT

Upvotes: 2

Related Questions