SnowOps Night Shift

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

  1. docker events --since 1m --until 0s --filter container=returns-api --filter event=oom --filter event=die --format '{{.Action}} {{index .Actor.Attributes "exitCode"}}' | tail -6

    Docker'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.

  2. docker stats --no-stream returns-api

    One sample of usage. MEM USAGE / LIMIT is current memory against the container's cap; the cap is what the kernel enforces.

  3. docker ps

    STATUS shows how long each container has been up. A few seconds on a service that should run all day means it is being restarted.

  4. docker inspect -f 'restarts={{.RestartCount}} started={{.State.StartedAt}}' returns-api

    How 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.

  5. docker logs --tail 8 returns-api

    What 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 137

Root 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-api

How 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