What is the Docker container lifecycle?
The complete lifecycle of a docker container revolves around five phases:
- Create phase
- Running phase
- Paused phase/unpause phase
- Stopped phase
- Killed phase
Refer to the image below for the complete lifecycle of a Docker container.
Create phase
In the create phase, a docker container is created from a docker image.
docker create --name <name-of-container> <docker-image-name>
Running phase
In the running phase, the docker container starts executing the commands mentioned in the image. To run a docker container, use the docker run command.
docker run <container-id>
OR
docker run <container-name>
The docker run command creates a container if it is not present. In this case, the creation of the container can be skipped.
Paused phase
In the paused phase, the current executing command in the docker container is paused. Use the docker pause command to pause a running container.
docker pause container <container-id or container-name>
Note:
docker pauseall the processes in the container. It sends the pauses think of “pause” as suspendsSIGSTOPsignal to pause the processes in the container.
Unpause phase
In the unpause phase, the paused container resumes executing the commands once it is unpaused.
Use the
docker unpausecommand to resume a paused container.
Then, Docker sends the SIGCONT signal to resume the process.
docker unpause <container-id or container-name>
Stop phase
In the stop phase, the container’s main process is shutdown gracefully. Docker sends SIGTERM for graceful shutdown, and if needed, SIGKILL, to kill the container’s main process.
Use the docker stop command to stop a container.
docker stop <container-id or container-name>
Restarting a docker container would translate to docker stop, then docker run, i.e., stop and run phases.
Kill phase
In the kill phase, the container’s main processes are shutdown abruptly. Docker sends a SIGKILL signal to kill the container’s main process.
docker kill <container-id or container-name>