Start and Stop Container

Generally, service processes such as apache2, Tomcat and MySQL will not stop directly, so docker run and docker start will not exit directly in this case

docker run -i -t –name nginx nginx

The -i -t parameter is fundamental to providing an interactive shell

  • -i ensures that stdin in the container is open and can be attached with docker attach
  • -t assigns a pseudo TTY terminal to the created container so that the newly created container can provide an interactive shell

If you want to interfere with the process in Linux system, you need to send some specific signals

There are dozens of these signals, so we won’t list them one by one

There are three common ways to stop or terminate a process,

  • CTRL + C sends SIGINT signal, which will and will only send to the process in the currently active window and all the subprocesses of the process (process tree)
  • Kill PID sends SIGTERM, which can be blocked, processed, and ignored
  • Kill – 9 PID sends sigkill. Sigkill signal cannot be blocked, processed, or ignored. It stops the specified process directly and does not affect other child processes and parent processes. However, the parent process of the child process will be changed from the process that was killed to the init process

SIGTERM and SIGINT signals will be processed according to the logic defined by the program, such as the following Java program

import sun.misc.Signal;
import sun.misc.SignalHandler;
public class Test {
 public static void main(String[] args) {
  System.out.println("Signal handling example.");
  SignalHandler handler = new MySignalHandler();
  // kill命令
  Signal termSignal = new Signal("TERM");
  Signal.handle(termSignal, handler);
  // ctrl+c命令
  Signal intSignal = new Signal("INT");
  Signal.handle(intSignal, handler);
  try {
  } catch (InterruptedException e) {
   System.out.println("Interrupted: " + e.getMessage());
class MySignalHandler implements SignalHandler {
 public void handle(Signal signal) {
  System.out.println("Signal handler called for signal " + signal);
  try {
   System.out.println("Handling " + signal.getName());
  } catch (Exception e) {
   System.out.println("handle|Signal handler" + "failed, reason "
     + e.getMessage());

When using Ctrl + C to send SIGINT signal to it, it just outputs information and does not stop itself

After using kill PID to send SIGTERM signal, it only outputs information and does not stop itself

Finally, the sigkill signal of kill – 9 PID directly kills the system

To stop a container, the most common method is docker stop

The stop command essentially sends SIGTERM signal to the process (such as nginx process). Most applications can correctly respond to this signal and start to stop themselves

If there is a special case, you need to use docker kill container_ Name to send sigkill

Send a Message