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.
States
A container moves through created → running → exited, and can be restarted or removed.
docker stopsendsSIGTERM, waits (10 seconds by default), thenSIGKILL.docker killsendsSIGKILLimmediately.docker startrestarts a stopped container with its writable layer intact.docker rmdeletes 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).
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?
It finished successfullyIt was killed with SIGKILL, often for exceeding its memory limitThe image is missingA 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.