问题
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 tasks when there is a code commit.
My problem here is the container will be killed abruptly during the message processing is there any way where i can stop the container killing until the process is finished and allow it to stop so that a new new container will be automatically created as i am ok with the current container processing the message with the old code.My container is running python code inside.
Thanks
回答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.
回答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 --time 30 <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.
来源:https://stackoverflow.com/questions/49557637/gracefully-stopping-ecs-container