docker run and the container lifecycle
What docker run really does, the states a container moves through, how stop, kill and rm differ, and what an exit code like 137 is telling you.
docker run and the container lifecycle
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- docker run is docker create plus docker start, with an image pull first if the image is missing and an attach unless you pass -d.
- A container is in one state at a time: created, running, paused, restarting, removing, exited or dead. A stopped container still exists, with its writable layer, until you remove it.
- docker stop sends the image's stop signal (SIGTERM by default), waits 10 seconds on Linux, then sends SIGKILL. docker kill sends SIGKILL straight away.
- A container lives exactly as long as its PID 1. When that process exits, the container exits, whatever else is running inside.
- Exit codes 125, 126 and 127 mean Docker, a non-executable command or a missing command. Codes above 128 mean death by signal, so 137 is SIGKILL and 143 is SIGTERM.
- --rm removes the container and its anonymous volumes when it exits, which keeps one-off containers from piling up.
Containers that won’t stay up, and won’t go away
Two complaints come up in the first week with Docker. “My container exits as soon as it starts.” And, a few weeks later, “docker ps -a lists 200 containers I never asked for.” Both come from the same thing: a container has a lifecycle with more states than “on” and “off”, and docker run quietly walks it through several of them.
What docker run does
Per the docker run reference, it’s a sequence of API calls:
POST /containers/create. If the image isn’t local, pull it first.--pull missingis the default, andalwaysorneverchange that.POST /containers/{id}/start.- Attach to the output, unless you passed
-d, and wait for the exit.
You can do the steps by hand, which is useful when you want to inspect or copy files into a container before it starts:
docker create --name api -p 8080:8080 notes-api:1.4.2 # state: created
docker cp ./seed.sql api:/seed.sql # still created
docker start api # state: running
The states
The Engine API defines a container’s State.Status as one of seven values:
| State | Meaning |
|---|---|
created |
Configured, writable layer allocated, never started |
running |
PID 1 is alive |
paused |
Every process frozen by the cgroup freezer (docker pause) |
restarting |
Exited and waiting for its restart policy to start it again |
removing |
docker rm in progress |
exited |
PID 1 has ended. The container, its logs and its writable layer remain |
dead |
Removal failed part-way, usually a busy mount. Only rm is left |
exited is the one that piles up. A stopped container isn’t gone. It keeps its writable layer, its logs and its name, which is why the second docker run --name api fails with a name conflict. docker rm api removes it, docker run --rm removes it automatically on exit (along with its anonymous volumes), and docker container prune clears all stopped ones. The cleanup lesson covers the rest.
A container is its PID 1
A container runs as long as its main process, PID 1 inside its PID namespace, runs. When PID 1 exits, the kernel kills everything else in the namespace and the container moves to exited. That explains the “exits immediately” complaint. docker run ubuntu runs bash with no stdin, so bash reads end-of-file and quits. A web server started with a flag that daemonizes it does the same: the launcher exits and takes the container with it. Apps in containers run in the foreground.
Stopping: stop, kill, pause
docker stop is the polite version. It sends the image’s StopSignal (SIGTERM unless the Dockerfile’s STOPSIGNAL says otherwise, as nginx sets SIGQUIT), waits a grace period, then sends SIGKILL. The grace period is --timeout on the command, --stop-timeout on the container, the daemon’s default-stop-timeout option added in Engine 29.7, or 10 seconds on Linux. -1 waits forever.
docker kill sends SIGKILL at once, or another signal with -s. docker pause freezes the processes without signalling them at all.
The catch is PID 1. The kernel doesn’t apply default signal actions to a namespace’s init, so a PID 1 with no SIGTERM handler simply ignores the signal, and every docker stop takes the full 10 seconds before the SIGKILL. The usual causes are a shell-form CMD, where /bin/sh is PID 1 and doesn’t pass the signal on, or an app that never installed a handler. docker run --init puts Docker’s tiny docker-init in front as PID 1 to forward signals and reap zombies. CMD, ENTRYPOINT and PID 1 goes deeper.
Reading exit codes
docker ps -a shows Exited (137) 3 minutes ago. The number is information. From Docker’s run docs:
- 125: Docker itself failed, such as an unknown flag or a bad option.
- 126: the command exists but can’t be invoked, such as
docker run busybox /etc. - 127: the command wasn’t found in the image.
- Anything else is PID 1’s own exit code.
Codes above 128 follow the shell convention: 128 plus the signal number. I checked the arithmetic with ordinary processes on this Mac, since there’s no Docker here:
$ sleep 100 & kill -TERM $!; wait $!; echo $?
143
$ sleep 100 & kill -KILL $!; wait $!; echo $?
137
$ sh -c nosuchcmd; echo $?
127
$ sh -c /etc; echo $?
126
So 143 means SIGTERM ended it and nothing handled it, and 137 means SIGKILL, from docker kill, from a docker stop whose grace period ran out, or from the OOM killer. docker inspect -f '{{.State.OOMKilled}}' api tells the last two apart. A script that traps SIGTERM and exits 0 is how a graceful shutdown looks: on this Mac, a shell script with trap '...; exit 0' TERM printed its cleanup message and returned 0.
About the video
The embed is all of Geekific’s “Docker Containers Lifecycle | create, start, run, stop and kill a container”, September 2023, 8 minutes. Its chapters cover creating, starting and running containers, container logs, then stopping and killing them. That’s the create, start, stop and kill path of this lesson, demonstrated. It doesn’t cover the full state list, PID 1’s signal handling or exit codes, which the lesson adds, and it predates Engine 29’s default-stop-timeout. English was judged from its “English (US) original” audio and manual English captions. The DevOps Directive chapter in deeper is a 29-minute tour of the most useful docker run flags.
What was run: the exit-code examples ran with bash and sh on macOS, which follow the same 128-plus-signal convention as Linux shells. Nothing ran in Docker. The state list comes from the Engine API’s swagger.yaml, and the stop, kill, --rm and exit-status behaviour from the Docker CLI reference and run docs, checked 2026-11-28.
Where this fits
Next, the three ways to interact with a running container: interactive, detached and exec.
Check yourself
docker stop takes exactly ten seconds for your container every time. What's going on?
PID 1 isn't handling SIGTERM, often because a shell-form CMD wraps the app in /bin/sh, or because PID 1 ignores signals it has no handler for. Docker waits the 10-second grace period and then sends SIGKILL.
A container exited with code 137 and you never ran docker kill. What are the likely causes?
137 is 128 + 9, death by SIGKILL. Either docker stop's grace period ran out, or the kernel's OOM killer stopped it, which docker inspect shows as State.OOMKilled true.
What's the difference between docker rm and docker rmi?
docker rm removes a container, its writable layer and its config. docker rmi removes an image, and only if no container, even a stopped one, still uses it.
Why does docker run ubuntu exit immediately while docker run -it ubuntu gives you a shell?
The image's command is bash. Without -i there's no stdin, so bash reads end-of-file and exits, and the container exits with it. With -it, bash has an open stdin and a terminal, so it waits for input.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.