GrepBoard/Manuals
605 manuals ·

Docker --memory and --cpus Limits: OOMKilled, Exit Code 137

·Tested on Docker Engine 29.9.2 · Ubuntu 24.04 (cgroup v2) · python:3.12-slim · 2026-10·Test scope ↓·6 steps

Docker containers have no memory or CPU limit by default. Verify --memory and --cpus with inspect, diagnose exit 137 OOMKilled, and use docker update.

Per the Docker docs, a container has no resource constraints by default. If one container eats all host memory, the kernel OOM killer may kill any process, so first read the current limits with docker inspect, then set --memory and --cpus. If a container died with exit 137, check OOMKilled to see whether its memory limit did it.

[1] Check the current limits. This container was created without options:

console
$ docker inspect --format 'Memory={{.HostConfig.Memory}} MemorySwap={{.HostConfig.MemorySwap}} NanoCpus={{.HostConfig.NanoCpus}}' web
output
Memory=0 MemorySwap=0 NanoCpus=0

0 means unlimited. What is actually enforced lives in the cgroup v2 files inside the container. Started with --memory 128m --memory-swap 128m --cpus 0.5:

console
$ docker run --rm -m 128m --memory-swap 128m --cpus 0.5 python:3.12-slim cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/memory.swap.max /sys/fs/cgroup/cpu.max
output
134217728050000 100000

memory.max is in bytes, memory.swap.max 0 means no swap, and cpu.max 50000 100000 means 50 ms out of every 100 ms (0.5 CPU).

[2] Decide from usage. MEM USAGE / LIMIT in docker stats is usage against the limit. Set the limit at peak usage plus headroom — the official docs give no fixed ratio. To keep production containers out of it, this step and [5] use a separate test container, web-test (it only runs sleep, so usage is tiny):

console
$ docker run -d --name web-test -m 128m --memory-swap 128m --cpus 0.5 python:3.12-slim sleep 60$ docker stats --no-stream --format 'table {{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.PIDs}}' web-test
output
CPU %     MEM USAGE / LIMIT   MEM %     PIDS0.00%     444KiB / 128MiB     0.34%     1

If MEM % sits near 100% at busy times, raise the limit or cut the app's memory. If PIDS keeps growing, look at --pids-limit too (listed in the update docs).

[3] How the memory flags combine (Docker docs):

SettingWhat the container gets
-m 512m only512 MB RAM + 512 MB swap if the host has swap (about double in total)
-m 512m --memory-swap 512m512 MB RAM, no swap
-m 512m --memory-swap 1g512 MB RAM + 512 MB swap
--memory-swap -1unlimited swap (as much as the host has)
--memory-reservation 256msoft limit applied only under host memory pressure, not a guarantee

People often miss that -m alone adds the same amount of swap. Below, a container given only -m 64m got MemorySwap at double (134217728). The minimum is 6m.

[4] Tell whether exit 137 was memory. A test container, oom-test, gets a 64 MB limit and is asked to allocate 200 MB:

console
$ docker run --name oom-test -m 64m python:3.12-slim python -c "a = bytearray(200 * 1024 * 1024)"; echo "exit=$?"exit=137$ docker inspect --format 'OOMKilled={{.State.OOMKilled}} ExitCode={{.State.ExitCode}} Memory={{.HostConfig.Memory}} MemorySwap={{.HostConfig.MemorySwap}}' oom-testOOMKilled=true ExitCode=137 Memory=67108864 MemorySwap=134217728

Once you have checked it, remove the dead test container with docker rm oom-test.

137 is 128 + 9 (SIGKILL). OOMKilled=true means the container hit its own memory limit. If it is false with 137, something sent KILL from outside — docker kill, or docker stop timing out. Either way the app dies without an error message of its own. If the whole host ran out of memory, read the kernel log as in find the OOM killer's victim.

The docs say to use --oom-kill-disable only on containers that also have -m. Without a limit, host processes may get killed instead.

[5] Change limits at runtime. docker update changes them without recreating the container. The target is web-test, started in [2] with -m 128m --memory-swap 128m --cpus 0.5:

console
$ docker update --memory 256m --memory-swap 256m --cpus 1 web-test$ docker exec web-test cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max268435456100000 100000

**Raising only --memory fails.** Once a swap limit is set, --memory cannot exceed --memory-swap, so pass both:

console
$ docker update --memory 512m web-testError response from daemon: Cannot update container …: Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time

Lowering the limit to --memory 32m was accepted without error. This container was using 444 KiB, though, so 32 MB was still above its usage — what the kernel does (reclaim or an OOM kill) when you go below actual usage was not tested. On a production container, do not go below the peak usage from [2]. Values set with docker update stay on that container only; your compose file or run script is unchanged, so recreating the container brings the old limits back. To make it stick, change those too (deploy.resources.limits and so on).

[6] Roll back. Write down the docker inspect values from [1] before changing anything and run the same command with the old values. To remove limits entirely, recreating the container without the flags is the reliable route. Remove the test container with docker rm -f web-test.

Logs are a resource too

The default json-file driver does no rotation by default, per the docs. A memory limit will not stop logs from filling the disk. Pass --log-opt max-size=1m --log-opt max-file=3 when creating the container:

console
$ docker inspect --format '{{.HostConfig.LogConfig.Type}} {{json .HostConfig.LogConfig.Config}}' webjson-file {"max-file":"3","max-size":"1m"}

Setting it as a daemon default (daemon.json), and why existing containers must be recreated, is covered in Docker log max-size. Docker's docs recommend the local driver to prevent disk exhaustion.

#docker#memory-limit#cpus#oomkilled#exit-137#docker-update#docker-stats