---
title: "Container Diagnostics: Reading Exit Codes and Troubleshooting Crash Reasons"
date: "2026-11-17T10:45:00+07:00"
description: "Decode Linux process exit status codes (137, 139, 143, 126, 127) in Docker to quickly diagnose container crashes and OOM kills."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/reading-container-exit-codes-crash-reasons.jpg"
alt: "Diagnostic dashboard showing exit code 137 OOM killed notification"
tags: ['docker', 'containers', 'troubleshooting', 'devops']
---

When a Docker container unexpectedly stops or enters a crash loop (`CrashLoopBackOff`), the first diagnostic step is determining **why** the application inside terminated.

In Linux and Docker, every exiting process returns an 8-bit integer known as an **Exit Code** (ranging from `0` to `255`). Understanding exit codes allows you to immediately distinguish between code syntax errors, missing binary dependencies, out-of-memory kernel kills, and graceful shutdowns.

In this guide, we will break down standard Docker exit codes, explain how to retrieve them, and provide troubleshooting strategies.

---

## 1. How to Check Container Exit Codes

To inspect the exit code of a stopped container, run `docker ps -a` or `docker inspect`:

```bash
# View STATUS column showing exit status
docker ps -a --filter "status=exited"
```

### Output Example

```text
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                      NAMES
c1a2b3d4e5f6   node-app:1.0   "docker-entrypoint.s…"   10 minutes ago   Exited (137) 9 minutes ago  api-service
```

### Inspecting Exit Code and OOM Kill State via `docker inspect`

```bash
docker inspect api-service --format 'ExitCode: {{.State.ExitCode}}, OOMKilled: {{.State.OOMKilled}}, Error: {{.State.Error}}'
```

---

## 2. Common Docker Exit Codes Reference

| Exit Code | Signal / Meaning | Common Cause & Resolution |
| :--- | :--- | :--- |
| **`0`** | Success / Normal Exit | Process completed work cleanly (e.g., batch job or script finished). |
| **`1`** | General Error | Application exception, uncaught error, or invalid configuration. Check application logs. |
| **`2`** | Misuse of Shell Builtins | Shell syntax error in `CMD` or `ENTRYPOINT` wrapper script. |
| **`125`** | Docker Daemon Error | `docker run` command syntax error or daemon failure (e.g., invalid flag). |
| **`126`** | Command Cannot Execute | Permission issue on entrypoint script (`chmod +x` missing) or bad arch binary. |
| **`127`** | Command Not Found | Executable file listed in `CMD` / `ENTRYPOINT` does not exist in container `$PATH`. |
| **`137`** | `SIGKILL` (128 + 9) | Process killed by OS kernel. Usually **Out Of Memory (OOM)** killer or `docker kill`. |
| **`139`** | `SIGSEGV` (128 + 11) | Segmentation fault. Invalid memory access in C/C++/Rust compiled binary. |
| **`143`** | `SIGTERM` (128 + 15) | Graceful termination signal sent by `docker stop` or system orchestrator. |

---

## 3. Deep Dive into Critical Exit Codes

### Exit Code 137: Out of Memory (OOM) Killer

When a container consumes more RAM than allowed by its memory limit (`--memory`), or exceeds total host physical RAM + swap, the Linux kernel's **OOM Killer** intervenes and sends an uncatchable `SIGKILL` (Signal 9) to `PID 1`.

- Exit Code formula: `128 + 9 = 137`.
- **Verification**: Check `OOMKilled` boolean in `docker inspect` or check host kernel logs:

```bash
# Check kernel dmesg for OOM events
dmesg -T | grep -i oom
```

- **Solution**: Increase container memory allocation (`--memory="2g"`) or optimize application memory consumption.

### Exit Code 127: Command Not Found

Occurs when the Dockerfile `ENTRYPOINT` or `CMD` specifies a script or binary that doesn't exist at the given path.

- **Common Cause**: Writing `#!/bin/bash` in a script built on an Alpine Linux base image (which uses `/bin/sh` instead of `/bin/bash`).
- **Solution**: Install `bash` in your Dockerfile (`RUN apk add --no-cache bash`) or update the shebang line to `#!/bin/sh`.

### Exit Code 126: Permission Denied

Occurs when the entrypoint shell script exists, but lacks execute permissions (`+x`).

- **Solution**: Add `RUN chmod +x /entrypoint.sh` to your Dockerfile prior to execution.

---

## 4. Troubleshooting Workflow for Crashing Containers

Follow this 4-step checklist whenever a container fails on startup:

1. **Check Exit Code**: Run `docker inspect <container> --format '{{.State.ExitCode}}'`.
2. **Read Logs**: Stream last stderr/stdout logs with `docker logs --tail 100 <container>`.
3. **Verify OOM State**: Run `docker inspect <container> --format '{{.State.OOMKilled}}'`.
4. **Test Entrypoint Interactively**: Override entrypoint to debug:

```bash
docker run --rm -it --entrypoint sh <image_name>
```

---

## Summary

Exit codes provide immediate diagnostic insight into container crashes. Recognizing `137` as an OOM kill, `127` as a missing executable, and `126` as a permission error accelerates root-cause analysis in DevOps environments.

**Takeaway:** Combine exit code verification (`ExitCode` and `OOMKilled` from `docker inspect`) with `docker logs` to diagnose and fix container failures rapidly.

## Sources

- [Docker Docs: Engine](https://docs.docker.com/engine/)
- [GNU Linux Exit Status Documentation](https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html)
