Container Lifecycle
The container lifecycle is the set of states a Docker container passes through: created, running, paused, restarting, exited, and removed. Specific docker commands and process exits trigger each transition.
States and transitions
A container is always in one state. docker create makes it without starting it, docker start runs it, and the container exits when its main process ends or is stopped. Knowing the exit code tells you whether it finished, crashed, or was killed.
Commands for each transition
docker create --name api nginx:1.27 # Createddocker start api # Runningdocker pause api # Paused, processes frozendocker unpause api # Running againdocker stop api # SIGTERM, then SIGKILL after 10sdocker kill api # SIGKILL immediatelydocker rm api # Removeddocker run -d --name api nginx:1.27 # create + start in one stepReading exit codes
| Exit code | Meaning |
|---|---|
| 0 | Clean exit, work finished |
| 1 | Application error |
| 126 | Command found but cannot execute |
| 127 | Command not found |
| 137 | SIGKILL, often out of memory or docker kill |
| 143 | SIGTERM, graceful stop |
docker ps -a --format 'table {{.Names}}\t{{.Status}}'docker inspect api --format '{{.State.OOMKilled}} {{.State.ExitCode}}'docker events --filter container=api # watch transitions liveRemember
docker stopgives the app time to shut down cleanly, whiledocker killdoes not. Apps that flush buffers on SIGTERM needdocker stop.
TipIf a container exits immediately,
docker logs <name>and the exit code explain most cases.
Frequently Asked Questions
What is the difference between a paused and a stopped container?
Pausing freezes the processes with the cgroup freezer, keeping memory and connections intact until unpause. Stopping ends the main process, so in-memory state is lost, although the container filesystem remains until removal.
Why did my container exit with code 137?
Code 137 means it received SIGKILL. The usual cause is the kernel killing it for exceeding its memory limit, which docker inspect shows as OOMKilled true, or someone running docker kill.
Do stopped containers use disk space?
Yes. Their writable layers and logs remain until you remove them. Use docker container prune or docker rm regularly, especially on CI hosts.