Skip to content Skip to sidebar Skip to footer

Gracefully Stopping Ecs Container

I am having some docker container which listens on RabbitMQ and process the message received. I have a code pipeline which kicks off the rebuilding of the image and updating the ta

Solution 1:

ECS by default sends a SIGTERM:

StopTask

Stops a running task.

When StopTask is called on a task, the equivalent of docker stop is issued to the containers running in the task. This results in a SIGTERM and a default 30-second timeout, after which SIGKILL is sent and the containers are forcibly stopped. If the container handles the SIGTERM gracefully and exits within 30 seconds from receiving it, no SIGKILL is sent.

Note

The default 30-second timeout can be configured on the Amazon ECS container agent with the ECS_CONTAINER_STOP_TIMEOUT variable. For more information, see Amazon ECS Container Agent Configuration in the Amazon Elastic Container Service Developer Guide.

Knowing this, you can add a simple check in your app to catch the SIGTERM, and react appropriately.

Solution 2:

Two ways to achieve a graceful stop in Docker:

Using docker stop

When issuing a docker stop command to a container, Docker fires a SIGTERM signal to the process inside the container, and waits for 10 seconds before cleaning up the container.

You can specify the timeout other than 10s:

docker stop --time30 <CONTAINER>

You need to ensure that the process handles the SIGTERM signal properly. Otherwise it will be rudely killed by a SIGKILL signal.

Using docker kill

By default the docker kill command sends a SIGKILL signal to the process. But you can specify another signal to be used:

docker kill --signal SIGQUIT <CONTAINER>

Also ensure that the process handles the specified signal properly. Worse than docker stop, the docker kill command does not have a timeout behavior.

Post a Comment for "Gracefully Stopping Ecs Container"