Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a docker container restart work?

When the docker container has, for example, 100 processes running and we restart the container using docker restart container-name how can docker restart all these 100 processes?

Will it store all the processes details and start them again?

Edit:

The following can be considered as specific question that I want to be answered:

After starting a container, I login to the container using docker exec -it container-name bash and I start some background processes. Then I exit the container and do a docker restart or a stop followed by a start. Will I get those manually started processes running again automatically?

like image 975
Pardhu Avatar asked Dec 22 '22 17:12

Pardhu


1 Answers

A Docker container has one primary process. docker restart does two things:

  1. It does the equivalent of docker stop. It sends SIGTERM to its primary process (only); if that doesn't terminate within 10 seconds, it sends SIGKILL. If the primary process still has children, they also get forcibly terminated.
  2. It does the equivalent of docker start. In the container that already exists, it runs the container's command, constructed by concatenating its entrypoint and command lists.

Note that both of these focus on one process. If that process is a dedicated process manager like supervisord, the docker stop sequence will hopefully cause the supervisor to gracefully stop every process it's managing and you'll be okay. If it's a shell script that starts a bunch of background processes and ignores them, they'll just get killed with no warning. Similarly, if the main container process knows how to start its children then docker start will cause that to happen again, but if it was an interactive shell and you hand-started background processes, they are lost.

Docker doesn't have any sort of "snapshot" mechanism. The general model is that containers always start from a clean and known state, and can reconstruct whatever they need from there. It fits better with this model to docker rm a stopped container and docker run a new one than to try to docker start it again.

like image 105
David Maze Avatar answered Jan 05 '23 00:01

David Maze