Sub topic 4
Reading exit codes and signals
This chapter assumes you can run containers with docker run, know the difference between ENTRYPOINT and CMD, and understand that Linux processes can receive signals. Here you will learn to turn a container's exit code into a first hypothesis about why it stopped.
Where the exit code comes from
A container lives as long as its main process, the one started by ENTRYPOINT and CMD, which runs as PID 1 inside the container. When that process ends, the container exits. Docker records the process's exit code and keeps it after the container stops.
You can read it in two ways:
docker ps -a --filter status=exited --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} error={{.State.Error}}' webThe Status column shows text such as Exited (137) 2 minutes ago. The inspect command shows the same number plus whether the kernel's OOM (out-of-memory) killer stopped the container.
The two ranges
An exit code is a number from 0 to 255. It falls into one of two groups:
- 0 to 127: the program chose the number itself by calling
exit(n)or returning frommain.0means success. Any other value means the program reports an error, and its meaning depends on the program. - 128 and above: usually
128 + N, whereNis the number of the signal that killed the process. For example,137is128 + 9, which is SIGKILL.
Common codes
| Code | Meaning | First thing to check |
|---|---|---|
| 0 | The process finished normally | Is it a one-shot job? A server that exits with 0 may have run the wrong command. |
| 1 | General application error | `docker logs` for a stack trace or error message |
| 126 | The command was found but cannot be executed | File permissions, missing execute bit, wrong architecture |
| 127 | The command was not found | Typo in `CMD`, missing binary, wrong `PATH` |
| 137 | Killed by SIGKILL (9) | `OOMKilled` in `inspect`, or `docker kill` / `docker stop` timeout |
| 139 | Killed by SIGSEGV (11) | Segmentation fault, often a native library or architecture mismatch |
| 143 | Terminated by SIGTERM (15) | A normal `docker stop`, or an orchestrator shutting it down |
Telling 137 apart
Exit code 137 has more than one cause, so check these in order:
- Run
docker inspectand readoom. If it istrue, the container exceeded its memory limit. The next chapters cover this in detail. - If
oomisfalse, look for adocker killor adocker stopthat timed out. By defaultdocker stopsends SIGTERM, waits 10 seconds, then sends SIGKILL. - If neither applies, something outside the container may have killed it, such as the host's OOM killer under host-wide memory pressure.
Why a stop can end in 137 instead of 143
Docker delivers signals to PID 1 only. Linux gives PID 1 special treatment: it receives no default signal handling, so a process that has not installed a SIGTERM handler ignores the signal. A server run directly as PID 1 may therefore ignore docker stop until the 10-second timeout, and then die with 137.
The shell form makes this worse. The shell becomes PID 1 and may not pass the signal to your program. Use the exec form, or add --init to run a small init process that forwards signals:
docker run --init --name web myimage:latestCommon mistakes
- Assuming a non-zero code always means a bug. 143 after
docker stopis expected. - Reading a code from a container that was restarted.
ExitCodeshows only the most recent exit, so checkRestartCounttoo. - Treating 127 as an image problem when the real cause is the shell form resolving a different
PATH.
Exercise
A container named api shows Exited (137). docker inspect reports oom=false. Give two possible causes and the first command you would run to narrow them down.
Show answer
The container may have been stopped with docker stop or docker kill after ignoring SIGTERM (a stop timeout ends in SIGKILL), or something outside the container, such as the host's OOM killer, killed it. Run docker events --filter container=api --filter event=die (or check docker logs api for a shutdown message) to see whether a stop or kill preceded the exit.
Exercise
A container exits with code 127 right after start. The CMD is CMD ["serve", "--port", "8080"]. Where do you expect to see the failure reported, and what is the likely cause?
Show answer
With the exec form there is no shell, so the daemon reports the failure in State.Error (visible with docker inspect) rather than through the shell's exit code. The likely cause is that the serve binary is missing from the image or is not on PATH.