Sub topic 3
Container lifecycle and states
This chapter explains the states a Docker container moves through and the commands that cause each change. It assumes you can use a terminal, know what a process is, and have Docker Engine or Docker Desktop installed. You should also know that a container is created from an image.
A container is a process with a state
A container exists as long as Docker keeps its record, and it runs only while its main process is alive. That main process is the command the image defines, or the one you pass to docker run. When it exits, the container stops. The container is not deleted, and its filesystem changes stay on disk until you remove it.
docker ps shows the state of each container in its STATUS column. The main states are:
| State | Meaning |
|---|---|
| created | The container exists but has never been started. |
| running | The main process is alive. |
| paused | The processes are frozen but still in memory. |
| restarting | Docker is restarting the container under a restart policy. |
| exited | The main process ended. The container still exists. |
| dead | A removal failed part way. Remove it again. |
How states change
docker run is a shortcut for docker create followed by docker start. You can run the steps separately to see the states:
docker create --name web nginx:1.27
docker ps -a --filter name=web
docker start web
docker ps --filter name=webThe first listing shows Created. The second shows Up followed by how long the container has been running. Add -a to docker ps to include containers that are not running. Without it, created and exited containers are hidden.
Stopping and exit codes
docker stop sends SIGTERM to the main process and waits 10 seconds by default. If the process is still alive after that, Docker sends SIGKILL. docker kill sends SIGKILL at once. A process that handles SIGTERM can close files and connections cleanly, so prefer docker stop.
Every exited container records an exit code. Read it with docker ps -a (for example Exited (137)) or with docker inspect:
docker run --name quick alpine sh -c "exit 3"
docker inspect --format '{{.State.Status}} {{.State.ExitCode}}' quickThis prints exited 3. Some codes are common:
0: the process finished normally.1and other small numbers: the application reported an error.137: the process receivedSIGKILL, fromdocker kill, a stop timeout, or the out-of-memory killer.143: the process ended afterSIGTERM.
Common mistakes
- Expecting an exited container to be gone. It still uses disk space and keeps its name. Starting a new container with the same name fails until you run
docker rm. Usedocker run --rmfor throwaway containers. - Restarting to get a fresh start.
docker startresumes the same container with its previous filesystem changes. To get a clean one, remove the container and run a new one from the image. - Confusing pause with stop. A paused container keeps its memory and its processes. A stopped container loses its processes.
Exercise
You run docker run --name job alpine echo done. The command prints done and returns. Which state is the container in now, and which command lets you see it?
Show answer
It is in the exited state with exit code 0. docker ps -a lists it, because plain docker ps shows only running containers. It stays on disk until you run docker rm job.
Exercise
A container shows Exited (137). What does the code tell you, and name one possible cause.
Show answer
The main process was ended by SIGKILL (128 + 9). Possible causes are docker kill, a docker stop that passed its timeout, or the kernel's out-of-memory killer.