Sub topic 1
Why containers stop: exit codes and the main process
A restart policy reacts to one event: a container stopping. Before you choose a policy, you need to know why containers stop and how Docker records the reason. This chapter covers the main process, exit codes, and the other ways a container can end.
Prerequisites
This chapter assumes you can:
- Run containers with
docker runand list them withdocker ps -a. - Read a process exit code, where 0 means success and any other value means failure.
- Use a shell well enough to run short one-line commands.
The main process decides the container's life
A container runs one main process, called PID 1 inside the container. This is the command from CMD or ENTRYPOINT, or the command you pass to docker run. The container is running exactly as long as that process is running. When it exits, the container stops, even if other processes are still running inside it.
Docker saves the main process's exit code in the container's state. A restart policy reads that code and the way the container stopped, then decides what to do.
docker run --name ok alpine sh -c 'exit 0'
docker run --name bad alpine sh -c 'exit 3'
docker inspect -f '{{.State.Status}} {{.State.ExitCode}}' ok badThe output is exited 0 for ok and exited 3 for bad. Both containers are stopped. Only the exit code tells you whether the run counted as a success.
Common exit codes
The exit code comes from the main process, so your application chooses most of the values. A few have a standard meaning.
| Code | Meaning | Typical cause |
|---|---|---|
| 0 | Success | The program finished its work |
| 1 | General error | An unhandled error in the application |
| 125 | Docker failed to run the container | Invalid `docker run` option |
| 126 | Command found but not executable | Missing execute permission |
| 127 | Command not found | Typo or missing binary in the image |
| 137 | Killed by SIGKILL (128 + 9) | `docker kill`, or the out-of-memory killer |
| 143 | Ended by SIGTERM (128 + 15) | `docker stop` when the process handles the signal and exits |
Codes above 128 mean the process died from a signal. Subtract 128 to get the signal number.
Other ways a container stops
A container does not only stop because the program finished or crashed.
- You run
docker stop. Docker sends SIGTERM, waits ten seconds by default, then sends SIGKILL. - The container exceeds its memory limit and the kernel's out-of-memory (OOM) killer ends it with code 137.
- The Docker daemon stops or restarts, which stops the containers it manages.
You can tell an OOM kill from a manual docker kill because Docker records it separately:
docker inspect -f '{{.State.ExitCode}} oom={{.State.OOMKilled}}' badflowchart TDCommon mistakes
A shell script used as the entrypoint can hide problems. If the script starts your program without exec, the shell is PID 1 and may not pass signals to the program. Then docker stop waits the full timeout and ends in code 137 instead of a clean shutdown.
A container that exits with 0 immediately is not necessarily broken. It may simply have no long-running process, as with docker run alpine echo hi. Restart policies only help for programs that are meant to keep running.
Exercise
A container shows exit code 137 and OOMKilled is true. What stopped it, and what does the number 137 tell you?
Show answer
The kernel's out-of-memory killer ended it because it exceeded its memory limit. 137 is 128 + 9, so the process died from signal 9 (SIGKILL), not from its own exit call.