The process in the container exits gracefully

Original link: http://ponder.work/2022/07/10/gracefully-shutdown-container/

When using docker, we often encounter the problem of resource cleanup when the process exits, such as ensuring that the current request processing is completed, and then exiting the program.

When executing docker stop xxx , docker will send a SIGTERM signal to the main process (pid=1) if the process does not exit within a certain period of time (the default is 10s), it will further send SIGKILL to kill the program directly. This signal can neither be captured nor cannot be ignored.

General web frameworks or rpc frameworks integrate SIGTERM signal handlers, so there is generally no need to worry about graceful exit.
But if you have multiple programs in your container (called a fat container, which is generally not recommended), then you need to do some operations to ensure that all programs exit gracefully.

signals

Signal is an inter-process communication mechanism, which provides an asynchronous software interrupt to the application program, so that the application program has the opportunity to accept commands (ie signals) sent by other programs or terminals.

After the application receives the signal, there are three processing methods: ignore, default, or catch.

Common signals:

Signal name number of signals describe default action
SIGHUP 1 When the user logs out of Linux, the foreground process group and the background process with output to the terminal will receive the SIGHUP signal. For a daemon that is disconnected from the terminal, this signal is used to tell it to re-read the configuration file. kill process
SIGINT 2 Program termination (interrupt) signal, issued when the user types Ctrl+C. kill process
SIGQUIT 3 Similar to SIGINT, but controlled by the QUIT character (usually Ctrl/). kill the process and dump core
SIGFPE 8 Emitted when a fatal arithmetic operation error occurs. Not only floating point errors, but also all other arithmetic errors such as overflow and division by 0. kill the process and dump core
SIGKILL 9 Used to immediately end the execution of the program. This signal cannot be blocked, handled and ignored. kill process
SIGALRM 14 Clock timing signal, which calculates actual time or clock time. The alarm function uses this signal. kill process
SIGTERM 15 Usually used to ask the program to exit normally; the kill command generates this signal by default. kill process

Dockerfile

Take supervisor as an example, the Dockerfile is as follows

 1
2
3
4
5
6
7
8
 FROM centos:centos7
ENV PYTHONUNBUFFERED= 1 TZ=Asia/Shanghai
RUN yum -y install epel-release && \
yum -y install supervisor && \
yum -y clean all && rm -rf /var/cache

COPY ./ /root/
ENTRYPOINT [ "/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf" ]

trap

Normally, other programs started by the supervisor when the container exits will not receive the SIGTERM signal, causing the subroutine to exit directly.

Here, trap is used to wrap the exception handling of the program

 1
 trap <siginal handler> <signal 1> <signal 2> ...

Create a new initialization script, init.sh

 1
2
3
4
5
6
7
 #!/bin/sh

/usr/bin/supervisord -n -c /etc/supervisord.conf &

trap "supervisorctl stop all && sleep 3" TERM INT

wait

Modify ENTRYPOINT as follows

 1
 ENTRYPOINT [ "sh" , "/root/init.sh" ] 

refer to

This article is reprinted from: http://ponder.work/2022/07/10/gracefully-shutdown-container/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment