Sub topic 4
What Happens When a Container Runs Out of Memory
This chapter assumes you can run containers with docker run, read basic docker inspect output, and know that a hard memory limit (--memory) is enforced by Linux cgroups.
The OOM killer
A container can allocate memory until it reaches its limit. Past that point the kernel first tries to reclaim memory, for example by dropping page cache or moving pages to swap if swap is allowed. If it cannot free enough, the kernel's out-of-memory (OOM) killer ends a process inside that cgroup. The process gets SIGKILL, so it cannot catch the signal, flush buffers, or shut down cleanly.
The kill is scoped to the container's cgroup. Other containers and the host keep running. That isolation is the main reason to set a limit.
Reproducing an OOM kill
This example sets the memory limit and the memory-plus-swap limit to the same value, which disables swap for the container. The tail /dev/zero command reads an endless stream with no newlines, so it buffers everything in memory.
docker run --name hog --memory 50m --memory-swap 50m alpine sh -c 'tail /dev/zero'
docker inspect --format '{{.State.OOMKilled}} {{.State.ExitCode}}' hogThe docker run command returns after a moment. The inspect command typically prints:
true 137Reading the signs
Two fields tell you what happened.
| Field | Value | Meaning |
|---|---|---|
| `State.OOMKilled` | `true` | The kernel killed a process in the container for memory reasons. |
| `State.ExitCode` | `137` | The process ended from signal 9. The value is 128 + 9. |
Exit code 137 alone is not proof of an OOM kill. Anything that sends SIGKILL produces it, including docker kill and docker stop when the stop timeout expires. Check OOMKilled to confirm the cause.
The reverse also needs care. OOMKilled is true if the kernel killed any process in the container, even if the main process kept running. In that case the container may still be up but a child worker is gone. Look at the kernel log on the host with dmesg (or journalctl -k) for lines mentioning "Memory cgroup out of memory" to see which process was chosen.
Restart policies can hide the problem
With --restart always or on-failure, Docker starts the container again after an OOM kill. The service seems to work, but it loops through kill and restart. Watch for a rising restart count:
docker inspect --format '{{.RestartCount}} {{.State.OOMKilled}}' webOOMKilled describes only the most recent run, so the restart count is the better signal for repeated kills.
Responding to an OOM kill
- Confirm the cause with
OOMKilledand the kernel log. - Measure real usage with
docker statswhile the workload runs under realistic load. - Either raise the limit or reduce what the application uses, for example by lowering a cache size or worker count.
- Re-run and check again.
Raising the limit only helps if the application's need is bounded. If memory grows without end (a leak), a higher limit delays the kill but does not prevent it.
Exercise
A container exits with code 137, and docker inspect shows OOMKilled is false. What is the likely cause, and how would you tell it apart from a memory kill?
Show answer
Something else sent SIGKILL, such as docker kill, a docker stop that hit its timeout, or another tool on the host. The exit code 137 only means signal 9. The OOMKilled field is false, so the kernel did not kill it for memory reasons. Confirm by checking the host kernel log for cgroup out-of-memory messages (none should appear) and by reviewing recent docker stop or docker kill activity.