Mastering Docker: The Power of Signals for Graceful Container Shutdowns

Follow our public account, reply “1024” to get 2TB of learning resources!

As we all know, if we enter the terminal of a container, we can directly use the exit command to exit and stop the container. The exit command closes the container while exiting it. If you exit the container after entering the docker container, the container will become Exited. Alternatively, you can use the shortcut ctrl+P+Q to detach from the container without stopping it.

You can also stop the container using the stop and kill commands.

docker stop container_id # normal shutdown of the container
docker kill container_id # force shutdown of the container
The differences between docker stop and docker kill are as follows:
  • When docker stop is executed, it first sends a TERM signal to the container, allowing the container to perform some protective and safety operations before exiting, and then the container automatically stops running. If the container does not stop running within a certain period, the kill -9 command is executed to forcefully terminate the container.
  • When docker kill is executed, regardless of the state of the container and what program is running, it directly executes the kill -9 command to forcefully terminate the container.

Recently, I reread the official Docker Reference documentation and found that there are many details to explore. When writing a Dockerfile, most of the time, you can just follow the template without any major issues, but if you delve deeper, it becomes even more interesting.

To discuss how to gracefully shut down a container, we must mention the concept of signals and the ENTRYPOINT and CMD instructions in the Dockerfile. Before discussing graceful shutdown, let’s first understand the basic concept of signals in Linux.

Signals

Signals are a notification mechanism for processes when events occur, sometimes referred to as software interrupts.

There are different types of signals, and Linux assigns numbers 1 to 31 to standard signals, which can be retrieved using kill -l:

# kill -l
1) SIGHUP       2) SIGINT       3) SIGQUIT      
4) SIGILL       5) SIGTRAP      6) SIGABRT      
7) SIGBUS       8) SIGFPE       9) SIGKILL
10) SIGUSR1    11) SIGSEGV     12) SIGUSR2
13) SIGPIPE    14) SIGALRM     15) SIGTERM
... ...

In reality, the list of signals exceeds 31, with some being synonyms for other names and some defined but unused. Here are a few commonly used signals:

  • 1) SIGHUP This signal is sent to the terminal control process when the terminal disconnects (hangs). The SIGHUP signal can also be used for daemon processes (e.g., init). Many daemon processes will reinitialize and reread configuration files upon receiving the SIGHUP signal.
  • 2) SIGINT When the user types the interrupt character in the terminal (usually Control-C), the terminal driver sends this signal to the foreground process group. The default behavior of this signal is to terminate the process.
  • 3) SIGQUIT When the user types the quit character on the keyboard (usually Control-“), this signal is sent to the foreground process group. By default, this signal terminates the process and generates a core dump file for debugging. The SIGQUIT signal is suitable when a process is stuck in an infinite loop or becomes unresponsive.
  • 9) SIGKILL This signal is a

Leave a Comment