Fix "exited with code 137" (OOMKilled) in Docker and Kubernetes
web-1 exited with code 137
- Reason: OOMKilled
- ERROR: failed to solve: process "/bin/sh -c npm run build" did not complete successfully: exit code: 137
- Memory cgroup out of memory: Killed process 4127 (node) total-vm:4318820kB, anon-rss:1046528kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:3164kB oom_score_adj:0
- command terminated with exit code 137
- "OOMKilled": true
Code 137 means the process was killed by SIGKILL (128 + 9), almost always by the kernel because the container exceeded its memory limit (OOMKilled). Confirm with docker inspect or kubectl describe pod, measure the real usage, then raise the limit or reduce the memory the application uses.
What this error means
An exit code above 128 means death by signal: 137 = 128 + 9, which is SIGKILL. That signal cannot be caught: no stack trace, no application log, the process just vanishes. Most often it is the kernel OOM killer: the container cgroup memory reached its limit (docker run -m, Compose mem_limit, Kubernetes resources.limits.memory), or the Docker Desktop VM ran out of RAM. Docker then records State.OOMKilled: true and Kubernetes shows Reason: OOMKilled. If OOMKilled is false, the SIGKILL came from somewhere else: docker kill, or a docker stop whose grace period expired.
Common causes
- Limit too low for the real need: JVM, Next.js or Vite build, loading an ML model, large import.
- Leak or unbounded growth: in-memory cache never cleared, whole file loaded into RAM, too many workers (Gunicorn, PHP-FPM
pm.max_children). - Runtime tuned above the limit: Node
--max-old-space-sizeor JVM-Xmxlarger than the container memory. - Docker Desktop VM too small (macOS, Windows): all containers and builds share the memory set in Settings > Resources.
- Not a memory problem: the application ignores
SIGTERM(often a shell-formCMD, whereshis PID 1) and Docker kills it after 10 seconds.
Step-by-step fix
-
Confirm it really is an OOM
true 137confirms an OOMKilled. On a Linux host, the kernel log shows which process was killed and at what usage. With Docker Desktop that log lives inside the VM: rely ondocker inspect.docker compose ps -a docker inspect --format '{{.State.OOMKilled}} {{.State.ExitCode}}' $(docker compose ps -aq web) sudo dmesg -T | grep -iE 'killed process|out of memory' -
Measure the real usage
Compare
MEM USAGEandLIMITduring the load that triggers the crash. On Kubernetes,kubectl topneeds metrics-server, andkubectl describe podshowsLast State: TerminatedwithReason: OOMKilled.docker stats --no-stream kubectl top pod --containers kubectl describe pod api-7d9f8b6c5d-x2kqp -
Docker and Compose: adjust the limit
Give the container a limit above the measured peak, with some margin.
docker composeappliesdeploy.resources.limitseven outside Swarm;mem_limit: 1gworks too. On the command line:docker run -m 1g myapp:latest. On macOS and Windows, also raise the VM memory in Docker Desktop.services: web: image: myapp:latest deploy: resources: limits: memory: 1g -
Kubernetes: set requests and limits
The
limitis the ceiling above which the container is killed; therequestis used to place it on a node. Base them on measured usage, not on a random value, then apply withkubectl apply.resources: requests: memory: "512Mi" cpu: "250m" limits: memory: "1Gi" -
Make the runtime fit under the limit
Set the heap below the container limit to leave room for the rest of the process, and reduce the number of workers if each one uses a lot. Example for a 1 GB limit.
export NODE_OPTIONS=--max-old-space-size=768 export JAVA_TOOL_OPTIONS=-XX:MaxRAMPercentage=75.0 gunicorn --workers 2 --bind 0.0.0.0:8000 myproject.wsgi:application -
If OOMKilled is false: handle SIGTERM
The container was killed while stopping. Use the exec form of
CMDso the application is PID 1 and receivesSIGTERM, handle that signal in the code, and extend the delay if a clean shutdown takes time (stop_grace_period: 30sin Compose,terminationGracePeriodSecondsin Kubernetes).CMD ["node", "server.js"]
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Always set a memory limit and an alert around 80% (for example on
container_memory_working_set_byteswith Prometheus). - Process large files as streams (streams, cursors, batches) rather than loading everything into memory.
- Run a load test before setting limits, and review them after every significant change (new dependency, new model, more workers).
Frequently asked questions
Does exit code 137 always mean out of memory?
No: 137 only means SIGKILL. Check State.OOMKilled (Docker) or Reason: OOMKilled (Kubernetes). If it is false, look for a docker kill, a stop that exceeded the grace period or a failing liveness probe.
Why is there no error message in my application logs?
Because SIGKILL stops the process instantly, without giving it time to write anything. The only traces are in docker inspect, kubectl describe or the kernel log.
How do I fix "exit code: 137" during docker build?
A build step (often npm run build) ran out of memory. Raise the memory of Docker Desktop or of the CI runner, or cap the Node heap with NODE_OPTIONS=--max-old-space-size=... in that step.
Useful tools for this error
More Docker & DevOps errors
- Fix "permission denied while trying to connect to the Docker daemon socket"
- Fix "Bind for 0.0.0.0:8080 failed: port is already allocated" (Docker)
- Fix "no space left on device" (Docker and Linux)
- Fix "exec format error" in Docker (arm64 / amd64)
- Fix "Cannot connect to the Docker daemon. Is the docker daemon running?"
- Fix "connect() failed (111: Connection refused) while connecting to upstream" (Nginx 502)
- Fix "CrashLoopBackOff" / "Back-off restarting failed container" (Kubernetes)