Docker Tutorial · Docker Basics · Lesson 4 of 26

The Container Lifecycle

Create, start, stop, restart and remove containers, and what happens to their files along the way.

Create a free account to save this page, take notes and track your progress. Sign in

States

A container moves through created → running → exited, and can be restarted or removed.

  • docker stop sends SIGTERM, waits (10 seconds by default), then SIGKILL.
  • docker kill sends SIGKILL immediately.
  • docker start restarts a stopped container with its writable layer intact.
  • docker rm deletes the container and its writable layer.

Exit codes tell you why a container stopped: 0 success, 1 application error, 137 killed (128 + 9, often out of memory), 143 terminated by SIGTERM (128 + 15).

Stop, start and exit codes
trap 'docker rm -f rc-life > /dev/null 2>&1' EXIT
docker run -d --name rc-life alpine:3.24 sh -c 'echo started >> /tmp/log; sleep 300' > /dev/null
docker stop -t 1 rc-life > /dev/null
docker inspect rc-life --format 'after stop:    {{.State.Status}}, exit code {{.State.ExitCode}}'
echo "(sleep runs as PID 1 and ignores SIGTERM, so docker stop had to SIGKILL it: 128 + 9 = 137)"
docker start rc-life > /dev/null
docker exec rc-life sh -c 'echo "log lines kept across restart: $(wc -l < /tmp/log)"'
docker run --rm alpine:3.24 sh -c 'exit 3' || echo "a container that exits 3 makes docker run return $?"

Output

after stop:    exited, exit code 137
(sleep runs as PID 1 and ignores SIGTERM, so docker stop had to SIGKILL it: 128 + 9 = 137)
log lines kept across restart: 2
a container that exits 3 makes docker run return 3

Run this on your computer — see “Get Started” for setup.

Test yourself

A container stopped with exit code 137. What does that usually mean?

  1. It finished successfully
  2. It was killed with SIGKILL, often for exceeding its memory limit
  3. The image is missing
  4. A network timeout
Show answer

Answer: It was killed with SIGKILL, often for exceeding its memory limit

137 = 128 + 9 (SIGKILL); the out-of-memory killer and docker kill both send SIGKILL.

Was this page helpful?