Stop, start, restart, and kill

Next up, we have the stop, start, restart, and kill commands. We have already used the start command to resume a container with a status of Exited. The stop command works in exactly the same way as when we used Ctrl + C to detach from your container running in the foreground. Run the following:

$ docker container stop nginx2

With this, a request is sent to the process for it to terminate, called a SIGTERM. If the process has not terminated itself within a grace period, then a kill signal, called a SIGKILL, is sent. This will immediately terminate the process, not giving it anytime to finish whatever is causing the delay, for example, committing the results of a database query to disk.

Because this could be bad, Docker has given you the option of overriding the default grace period, which is 10 seconds, by using the -t flag; this is short for --time. For example, running the following command will wait up to 60 seconds before sending a SIGKILL, should it need to be sent to kill the process:

$ docker container stop -t 60 nginx3

The start command, as we have already experienced, will start the process back up; however, unlike the pause and unpause commands, the process in this case starts from scratch rather than starting from where it left off.

$ docker container start nginx2 nginx3

The restart command is a combination of the following two commands; it stops and then starts the container ID or name you pass it. Also, like stop, you can pass the -t flag:

 

$ docker container restart -t 60 nginx4

Finally, you also have the option sending a SIGKILL immediately to the container by running the kill command:

$ docker container kill nginx5
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset