error docker

Docker Exit Code 137 - Out of Memory / Killed (SIGKILL)

Understanding Docker exit code 137 - the container was forcefully killed, usually due to exceeding memory limits or by docker stop timeout.

What It Means

Docker exit code 137 (128 + 9) indicates that the container received SIGKILL. This is most commonly caused by the container exceeding its memory limit (OOMKilled), but it can also occur when docker stop times out and escalates from SIGTERM to SIGKILL.

SIGKILL cannot be caught or handled — the container is immediately terminated.

Common Causes

  • Container exceeded its memory limit (OOMKilled)
  • docker stop timed out waiting for graceful shutdown, then sent SIGKILL
  • docker kill was explicitly used
  • Kubernetes killed the pod for exceeding memory limits
  • Host system ran out of memory and Linux OOM killer targeted the container
  • CI/CD system killed the container for exceeding resource limits

How to Fix

Check if OOM killed

# Check if the container was OOMKilled
docker inspect <container-id> --format='{{.State.OOMKilled}}'

# Check container stats before it died
docker stats --no-stream

# Check host system OOM events
dmesg | grep -i "oom\|killed process"

Increase memory limits

# Docker run with higher memory limit
docker run -m 4g my-image
docker run --memory=4g --memory-swap=4g my-image
# Docker Compose
services:
  app:
    image: my-image
    deploy:
      resources:
        limits:
          memory: 4G
        reservations:
          memory: 2G
# Kubernetes
containers:
  - name: app
    resources:
      limits:
        memory: "4Gi"
      requests:
        memory: "2Gi"

Fix application memory usage

// Node.js - Set heap size limit
// node --max-old-space-size=3072 app.js  (3GB)

// Monitor memory usage
setInterval(() => {
  const used = process.memoryUsage();
  console.log(`RSS: ${Math.round(used.rss / 1024 / 1024)}MB`);
  console.log(`Heap: ${Math.round(used.heapUsed / 1024 / 1024)}MB`);
}, 5000);
# Python - Use generators instead of loading everything into memory
# Bad: loads entire file into memory
data = open('huge_file.csv').readlines()

# Good: process line by line
with open('huge_file.csv') as f:
    for line in f:
        process(line)

Fix docker stop timeout

# Give more time for graceful shutdown (default is 10s)
docker stop --time=30 <container-id>
# Handle SIGTERM in your application (see exit code 143)
# Use exec form so signals reach your app
CMD ["node", "app.js"]

# Set STOPSIGNAL if your app expects a different signal
STOPSIGNAL SIGTERM

Monitor container resources

# Real-time resource monitoring
docker stats

# Set up resource alerts in Docker Compose
# Use a monitoring stack (Prometheus + Grafana)