I am trying to understand what is the difference between the commands docker stop ContainerID
and docker pause ContainerID
. According to this page both of them are used to pause an existing Docker container.
The docker pause
command suspends all processes in the specified containers. On Linux, this uses the cgroups freezer. Traditionally, when suspending a process the SIGSTOP signal is used, which is observable by the process being suspended
https://docs.docker.com/engine/reference/commandline/pause/
The docker stop
command. The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL.
https://docs.docker.com/engine/reference/commandline/stop/#options
SIGTERM
is the termination signal. The default behavior is to terminate the process, but it also can be caught or ignored. The intention is to kill the process, gracefully or not, but to first allow it a chance to cleanup.
SIGKILL
is the kill signal. The only behavior is to kill the process, immediately. As the process cannot catch the signal, it cannot cleanup, and thus this is a signal of last resort.
SIGSTOP
is the pause signal. The only behavior is to pause the process; the signal cannot be caught or ignored. The shell uses pausing (and its counterpart, resuming via SIGCONT) to implement job control.
SIGSTOP
will be destroyed, and you cannot SIGCONT
them. A reboot kills the main root process from which all other processes descend and restarts it. If you suspend (or "sleep") your computer, the process tree is preserved and can be resumed. –
Helbon What is the difference between docker stop
and docker pause
?
docker stop
: SendSIGTERM
(termination signal), and if neededSIGKILL
(kill signal)docker pause
: SendSIGSTOP
(pause signal)
SIGTERM
: The default behavior is to terminate the process, but it also can be caught or ignored. The intention is to kill the process, gracefully or not, but first give it a chance to clean up.
SIGKILL
: The only behavior is to kill the process, immediately. As the process cannot catch the signal, it cannot clean up; thus, this is a signal of last resort.
SIGSTOP
: The only behavior is to pause the process; the signal cannot be caught or ignored. The shell uses pausing (and its counterpart, resuming via SIGCONT) to implement job control
When to use docker stop
and docker pause
?
docker stop
: When you wish to clear up memory or delete all of the processes' -cached- data. Simply put, you no longer care about the processes in the container and are comfortable with killing them.docker pause
: When you only want to suspend the processes in the container; you do not want them to lose data or state.
Example:
Consider a container with a counter. Assume the counter has reached 3000. Running docker stop
will cause the counter to lose its value and you will be unable to retrieve it. Using docker pause
, on the other hand, will maintain the counter state and value.
Hope it's clear now!
And addition to the answers added earlier
running docker events
after docker stop
shows events
- kill (signal 15): where signal 15 = SIGTERM
- die
- stop
running docker events
after docker pause
shows only one event
- pause
Also docker pause
would still keep memory portion while the container is paused. This memory is used when the container is resumed. docker stop
releases the memory used after the container is stopped.
This table has even more details.
docker pause
pauses (i.e., sends SIGSTOP
) pauses (read: suspends) all the processes in a container[s].
docker stop
stops (i.e., sends SIGTERM
, and if needed SIGKILL
) to the conrainer[s]'s main process.
When the running container is issued with the docker pause command, the SIGSTOP signal is passed which allows the processes inside the container (basically the container itself) to be in a paused state. So when the docker unpause is issued, SIGCONT signal is passed to the container processes to restore the container proceses.
When the docker stop command is issued to the running container, the SIGTERM signal is passed to the container processes to stop and stops the container.
Hence when the docker pause is issued to a container, and the docker service is restarted, the cgroups allocated to it is released. (as the SIGTERM is passed to all the container processes) So after the restart, the unpause would not be helpful as the containers are stopped.
© 2022 - 2024 — McMap. All rights reserved.