Fix Docker Container Exit Code 137 (Complete Guide)

Docker containers exiting with code 137 is one of the most confusing issues for beginners and even experienced users.
You may see:
Exited (137)
Or:
docker container exited with code 137
This usually means your container was killed due to memory limits.
In this guide, you’ll learn how to fix Docker exit code 137, understand the root cause, and prevent it in production.
What is Exit Code 137?
Exit code 137 means:
128 + 9 (SIGKILL)
This indicates the container was forcefully terminated.
Main Causes
- Out of Memory (OOM)
- Manual kill (
kill -9) - System resource limits
- Docker memory restrictions
Step 1: Check Container Logs
docker logs container_id
Look for:
- Memory errors
- Application crashes
Step 2: Check System Memory
free -h
If RAM is low → likely OOM issue.
Step 3: Check OOM Killer Logs
dmesg | grep -i kill
Example:
Killed process 1234 (node) out of memory
Step 4: Increase Memory Limits
Run container with more memory:
docker run -m 1g nginx
Step 5: Remove Memory Limits
If using Docker Compose:
deploy:
resources:
limits:
memory: 512M
Increase it or remove it.
Step 6: Optimize Application Memory Usage
- Reduce cache
- Optimize queries
- Limit worker processes
Step 7: Add Swap Memory
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Step 8: Restart Container
docker restart container_id
Step 9: Monitor Resource Usage
docker stats
Real-World Example
Problem:
Node.js container crashes.
Fix:
docker run -m 2g node-app
Advanced Debugging
Inspect container
docker inspect container_id
Check limits
cat /sys/fs/cgroup/memory/memory.limit_in_bytes
Best Practices
1. Always Set Memory Limits Carefully
Too low → crashes
Too high → resource waste
2. Monitor Continuously
Use Grafana dashboards.
3. Use Lightweight Images
Alpine-based images reduce memory usage.
4. Optimize Containers
- Remove unused packages
- Use multi-stage builds
Security Considerations
- Prevent resource abuse
- Limit container access
- Monitor usage
When to Avoid This Entire Problem
Managing memory manually is complex.
👉 Use managed hosting:

Benefits:
- Auto-scaling
- Memory optimization
- Zero manual tuning
Troubleshooting Checklist
- Logs checked?
- Memory usage verified?
- OOM logs confirmed?
- Limits increased?
- Swap added?
Final Thoughts
Exit code 137 is not a bug, it’s a signal.
Your system is telling you:
👉 “Not enough memory”
Fix Summary:
✔ Increase memory
✔ Optimize app
✔ Add swap
✔ Monitor usage
Once fixed, your containers will run smoothly.
👉 Want hassle-free hosting?

