Skip to main content

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.

stateDiagram-v2 [*] --> Created: docker create Created --> Running: docker start Running --> Paused: docker pause Paused --> Running: docker unpause Running --> Exited: process ends or docker stop Running --> Restarting: restart policy Restarting --> Running Exited --> Running: docker start Exited --> [*]: docker rm

Commands for each transition

Bash
docker create --name api nginx:1.27 # Created
docker start api # Running
docker pause api # Paused, processes frozen
docker unpause api # Running again
docker stop api # SIGTERM, then SIGKILL after 10s
docker kill api # SIGKILL immediately
docker rm api # Removed
docker run -d --name api nginx:1.27 # create + start in one step

Reading 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
Bash
docker ps -a --format 'table {{.Names}}\t{{.Status}}'
docker inspect api --format '{{.State.OOMKilled}} {{.State.ExitCode}}'
docker events --filter container=api # watch transitions live
Remember

docker stop gives the app time to shut down cleanly, while docker kill does not. Apps that flush buffers on SIGTERM need docker stop.

Tip

If 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.

Found a mistake, an outdated step or a broken link?