Sub topic 4
Reading Exit Codes and Signals
An exit code is the number a container's main process returns when it stops. Docker records it in the container state, so it is often the quickest way to classify a failure before you read any logs in detail. This chapter explains how to read the number, how signals map onto it, and where the common traps are.
Prerequisites
This chapter assumes you can:
- Run
docker psanddocker inspect. - Read basic
docker logsoutput. - Explain what a Linux process and a signal are.
- Tell
ENTRYPOINTfromCMDin a Dockerfile.
Where the exit code comes from
A container lives as long as its main process, which is PID 1 inside the container. When that process exits, the container stops, and Docker stores the process's exit status. You can see it in two places.
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 in docker ps -a reads like Exited (137) 2 minutes ago. The number in parentheses is the exit code. docker inspect gives the same value as .State.ExitCode, plus related fields that help you interpret it.
The exit code tells you how the process ended. It does not tell you why. Treat it as a hint that narrows the search, then confirm with logs and inspect fields.
Two kinds of exit codes
Exit codes from 0 to 255 fall into two groups.
- Codes the program chose. The program called
exit(n)or returnednfrommain. The meaning belongs to that program.0means success by convention, and1is the usual generic failure. - Codes the shell or kernel produced. When a process is killed by a signal, shells report
128 + signal number. When a shell cannot run a command, it uses126or127.
Docker follows the shell convention. If PID 1 is killed by signal 9, Docker reports 137, because 128 + 9 = 137.
The common codes
| Code | Meaning | Typical cause |
|---|---|---|
| 0 | Clean exit | The command finished its job, or the main process was a short script. |
| 1 | Generic application error | An unhandled exception, a failed startup check, or a bad configuration value. |
| 2 | Misuse of a shell builtin or bad arguments | Wrong command-line options, or a script syntax error. |
| 126 | Command found but not executable | Missing execute permission, or the path is a directory. |
| 127 | Command not found | Typo in `CMD`, a binary missing from the image, or a script whose shebang interpreter is missing. |
| 137 | Killed by SIGKILL (128 + 9) | The kernel OOM killer, `docker kill`, or a stop that timed out. |
| 139 | Killed by SIGSEGV (128 + 11) | A segmentation fault in native code. |
| 143 | Terminated by SIGTERM (128 + 15) | A normal `docker stop`, where the process did not handle the signal or exited because of it. |
Exit code 0 is not always good news. A container that exits with 0 right after you start it usually means the main process was a command that finishes, such as a shell with no terminal attached, or a service that daemonized itself and left PID 1 with nothing to wait for.
Signals in a container
A signal is a small message the kernel delivers to a process. The ones you will meet most often are:
| Signal | Number | Default effect | Source in Docker |
|---|---|---|---|
| SIGTERM | 15 | Terminate (can be handled) | `docker stop`, first step |
| SIGKILL | 9 | Terminate (cannot be handled) | `docker kill`, `docker stop` after the timeout, OOM killer |
| SIGINT | 2 | Interrupt | Ctrl+C in an attached terminal |
| SIGSEGV | 11 | Crash with core dump | The process touched invalid memory |
docker stop sends SIGTERM to PID 1 and waits ten seconds by default. If the process is still alive, Docker sends SIGKILL. You can change the wait with docker stop --time 30 or the stop_grace_period setting in Compose.
The sequence is easier to see as a diagram.
Read the diagram as three outcomes. A program that traps SIGTERM and shuts down cleanly usually exits with 0. A program with no handler is terminated by the signal and reports 143. A program that ignores SIGTERM gets SIGKILL after the grace period and reports 137.
Reading 137 carefully
Code 137 has several possible causes, and the exit code alone cannot separate them. Check OOMKilled in the same inspect output.
oom=truemeans the kernel killed the process because the container went over its memory limit. The next chapters on resources cover how to fix that.oom=falsewith a recentdocker stopordocker killmeans someone or something sent the signal. A137after adocker stopmeans the process ignored SIGTERM, which is a signal-handling bug rather than a crash.oom=falsewith no action by you can still mean an external agent killed it, such as an orchestrator or a script that runsdocker kill. Checkdocker eventsfor akillevent.
A related gotcha is that OOMKilled reflects only the container's own cgroup. If the host runs out of memory and the kernel kills a process outside the memory limit, you may see 137 with oom=false. Check the host's kernel log with dmesg or journalctl -k in that case.
Why a stop gives 137 instead of 143
If every docker stop takes ten seconds and ends in 137, the main process is not receiving SIGTERM. The common reason is the shell form of CMD or ENTRYPOINT.
# Shell form: Docker runs /bin/sh -c "python app.py"
CMD python app.py
# Exec form: python is PID 1 and receives signals directly
CMD ["python", "app.py"]In shell form, /bin/sh is PID 1. Many shells do not forward SIGTERM to their child, so the application never sees it. Use exec form, or start the application with exec in a wrapper script so it replaces the shell.
#!/bin/sh
# entrypoint.sh
set -e
echo "starting"
exec "$@"There is a second issue even in exec form. PID 1 on Linux is treated specially. The kernel does not apply default signal actions to it, so a program that installs no SIGTERM handler will ignore the signal when it runs as PID 1. Many runtimes install handlers, but some do not. You can add a minimal init process with docker run --init, which runs a small supervisor as PID 1 that forwards signals and reaps zombie child processes.
Reading 126 and 127
These two codes mean the process never started, so there are usually no application logs. The only evidence is the exit code and a short message in the logs from the shell.
127means the shell could not find the command. Check the spelling, check that the binary is in the image, and checkPATH. A script with#!/bin/bashon an Alpine image, which has no bash, also gives127with a confusing "not found" message that names the script, not the interpreter.126means the file exists but cannot be run. Check its mode withls -l, and check whether you mounted a volume over the file with a mode that lacks the execute bit.
A Windows line ending is a frequent cause. A script saved with CRLF endings has a shebang of /bin/sh\r, which does not exist, so you get 127 or "no such file or directory". Convert the file to LF endings.
To investigate these, you need to run the image with a different command, which the later chapter on reproducing failed starts covers.
Exit codes above 128 that are not signals
Not every code above 128 is a signal. Some programs choose their own values. Only treat a code as a signal if subtracting 128 gives a real signal number, and ideally confirm it. A runtime that exits with 130 after Ctrl+C is reporting SIGINT (128 + 2), which fits. A value such as 200 is more likely an application-defined code, so check the application's documentation.
Also note that a program can catch a signal and then choose its own exit code. A Java application that handles SIGTERM commonly exits with 143 from the JVM, but a Node.js or Go program may exit with 0 after cleanup. So 0 after docker stop is healthy, and 143 is usually acceptable.
A short triage routine
Use this order when you see a stopped container.
- Read the code from
docker ps -aordocker inspect. - If it is
137, readOOMKilledbefore anything else. - If it is
126or127, look at the command and the files, not the logs. - If it is
1or2, read the last lines ofdocker logsfor the error. - If it is
139, look for native libraries, a mismatched architecture, or a base image change. - If it is
0and you did not expect a stop, check what the main process actually does.
Exercise
A container named api always takes about ten seconds to stop, and docker inspect then shows exit=137 and oom=false. Nobody ran docker kill. What is the most likely cause, and what are two fixes?
Show answer
The main process is not handling SIGTERM, so Docker waits for the grace period and then sends SIGKILL, which gives 128 + 9 = 137. Likely reasons are the shell form of CMD or ENTRYPOINT, where /bin/sh is PID 1 and does not forward the signal, or an application running as PID 1 with no SIGTERM handler. Fixes: use exec form (CMD ["python", "app.py"]) or exec "$@" in the entrypoint script, and optionally run with --init so a small init process forwards signals. Adding a SIGTERM handler in the application also works.
Exercise
A container exits with code 127 immediately after start, and docker logs shows exec ./start.sh: no such file or directory even though start.sh exists in the image. Name two possible causes.
Show answer
The script's shebang points to an interpreter that is missing from the image (for example #!/bin/bash on Alpine), or the script has Windows CRLF line endings so the interpreter name ends in a carriage return. Both make the kernel fail to find the interpreter, which the shell reports as not found. Install the interpreter or change the shebang, and convert the file to LF endings.