Why does my Docker container keep restarting with exit code 137?
Exit code 137 means the process was killed from outside. When the kernel kills a container for going over its memory limit, docker events shows an oom event and then die with exit code 137. With a restart policy Docker brings it straight back, so docker inspect describes the fresh run and the service just looks like it is flapping. docker stats shows the limit; the app's logs show what it was doing when it died. In this incident a new limit of 48 MiB was below the 90 MiB a month-end report loads.
What it looks like
- docker ps shows the container up for only a few seconds.
- docker events shows oom and die with exit code 137, and the restart count climbs.
- Requests that load a lot of data fail; small requests mostly work.
- Memory limits were changed shortly before.
Commands that find the cause
docker events --since 1m --until 0s --filter container=returns-api --filter event=oom --filter event=die --format '{{.Action}} {{index .Actor.Attributes "exitCode"}}' | tail -6Docker's own record of the last minute: oom means the kernel hit the container's memory limit, die is the process ending, with its exit code. 137 is a kill from outside the process, not a crash of its own.
docker stats --no-stream returns-apiOne sample of usage. MEM USAGE / LIMIT is current memory against the container's cap; the cap is what the kernel enforces.
docker psSTATUS shows how long each container has been up. A few seconds on a service that should run all day means it is being restarted.
docker inspect -f 'restarts={{.RestartCount}} started={{.State.StartedAt}}' returns-apiHow often Docker has restarted the container and when the current run began. After a restart, the State fields describe the new run, so they cannot tell you how the last one ended.
docker logs --tail 8 returns-apiWhat the API printed before each restart. A report that starts and never says done was cut off mid-way.
What the evidence looked like
Recorded on a real system when this incident was captured for the game.
$ docker events --since 1m --until 0s --filter container=returns-api --filter event=oom --filter event=die --format '{{.Action}} {{index .Actor.Attributes "exitCode"}}' | tail -6
oom
die 137
oom
die 137
oom
die 137Root cause
The limits tidy-up capped returns-api at 48 MiB while one report loads about 90 MiB, so the kernel killed it; 256 MiB fixed it.
The fix
Raise the API's memory limit to fit a report, and its swap limit with it:
docker update --memory 256m --memory-swap 512m returns-apiHow to prevent it
Size each container's limit from what it loads at peak, with room, rather than one number for the host. Alert on restart counts so a crash loop behind a restart policy is noticed.
More incident guides
- Docker container exits immediately after a config change
- Docker port mapping not working, empty reply
- Docker container cannot connect to the database host
- Docker volume permission denied
- CrashLoopBackOff after a config change
- Service returns 503 while every pod is Running
- OOMKilled and exit code 137
- Pods fail to start after a Secret change
- Requests fail after adding a NetworkPolicy
- Healthy pods restarted by a liveness probe
- Slow service with low CPU and no restarts
- Rollout stuck on ImagePullBackOff
- Pods stuck Pending with insufficient CPU
- Linux disk full: no space left on device
- systemd service keeps restarting after a deploy
- Address already in use: find what holds a port on Linux
- Linux name or service not known after a config change
- Too many open files in a systemd service