DEV Community

Yash
Yash

Posted on

Docker Out of Memory: How to Diagnose and Fix OOM Kills

Docker Out of Memory: How to Diagnose and Fix OOM Kills

Your container keeps dying and you don't know why. No error in the app. No crash message. Just — gone.

Nine times out of ten, the kernel killed it for using too much memory.

Here's how to know for sure and fix it.

Confirm It's an OOM Kill

# Check the container's last exit code
docker inspect <container_name> | grep -A 5 '"State"'
Enter fullscreen mode Exit fullscreen mode

An OOM kill shows "OOMKilled": true:

"State": {
    "Status": "exited",
    "Running": false,
    "OOMKilled": true,
    "ExitCode": 137
}
Enter fullscreen mode Exit fullscreen mode

Exit code 137 = killed by signal. Combined with OOMKilled: true = out of memory.

Also check kernel logs:

sudo dmesg | grep -i "oom\|killed process" | tail -20
Enter fullscreen mode Exit fullscreen mode

You'll see something like:

Out of memory: Kill process 12345 (node) score 847 or sacrifice child
Killed process 12345 (node) total-vm:2048000kB, anon-rss:1834000kB
Enter fullscreen mode Exit fullscreen mode

Check Current Memory Usage

# Live container stats
docker stats --no-stream

# Check if a memory limit is set
docker inspect <container_name> | grep -i memory
Enter fullscreen mode Exit fullscreen mode

If MemoryLimit is 0, there's no limit set — the container can eat all available RAM.

Set a Memory Limit

# Run with limit
docker run -m 512m your-image

# Or update a running container
docker update --memory 512m --memory-swap 512m <container_name>
Enter fullscreen mode Exit fullscreen mode

Or in docker-compose.yml:

services:
  app:
    image: your-image
    deploy:
      resources:
        limits:
          memory: 512M
Enter fullscreen mode Exit fullscreen mode

Find the Memory Leak in Your App

# Get a shell in the container
docker exec -it <container_name> sh

# Check process memory inside container
top
# or
cat /proc/meminfo
Enter fullscreen mode Exit fullscreen mode

For Node.js apps specifically:

# Check heap usage
node --max-old-space-size=256 server.js

# Add memory monitoring
node -e "setInterval(() => console.log(process.memoryUsage()), 5000)" &
Enter fullscreen mode Exit fullscreen mode

Restart Policy as a Temporary Bandaid

docker run --restart=unless-stopped -m 512m your-image
Enter fullscreen mode Exit fullscreen mode

This doesn't fix the leak but keeps the service alive while you investigate.

Prevention Checklist

  • [ ] Always set --memory limits in production
  • [ ] Set up alerting when container memory > 80%
  • [ ] Add heap dump tooling to Node/Java apps
  • [ ] Monitor with docker stats in a loop: watch -n 5 docker stats --no-stream

I built ARIA to solve exactly this.
Try it free at step2dev.com — no credit card needed.

Top comments (0)