How to Fix Docker Container Exited with Code 137
In this tutorial, you'll learn about How to Fix Docker Container Exited with Code 137. We cover key concepts, practical examples, and best practices.
The Problem
Your Docker container stops with exit code 137:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 my-app "node server.js" 5 seconds ago Exited (137) 3 seconds ago angry_curie
Exit code 137 means the container was killed by SIGKILL (signal 9, offset 128 = 137). This almost always happens when the container exceeds its memory limit and the OOM (Out of Memory) killer terminates it.
Quick Fix
Step 1: Check the OOM killer log
docker inspect a1b2c3d4e5f6 | grep -i oom
Expected:
"OOMKilled": true
Step 2: Check container memory usage
docker stats a1b2c3d4e5f6 --no-stream
Look at the MEM USAGE / LIMIT column. If it is near the limit, OOM is the cause.
Step 3: Increase the memory limit
Run the container with a higher memory limit:
docker run -m 2g --memory-reservation 1g my-app
Or update Docker Compose:
services:
my-app:
image: my-app
deploy:
resources:
limits:
memory: 2G
reservations:
memory: 1G
Step 4: Fix the memory leak
If memory usage grows indefinitely, your application has a memory leak. Use tools to find it:
# Node.js: heap dump
node --inspect server.js
# Python: tracemalloc
python3 -m tracemalloc server.py
# Java: jmap
jmap -heap <pid>
Step 5: Set memory limits in Docker Compose
docker-compose up -d
Alternative Solutions
Remove the memory limit entirely for testing:
docker run --memory=0 my-app
This allows unlimited memory usage but is not recommended for production.
Inspect Container Configuration
docker inspect <container-id> --format '{{json .Config}}' | python3 -m json.tool
# {
# "Hostname": "abc123",
# "Env": ["PATH=/usr/local/bin:..."],
# "Cmd": ["node", "app.js"]
# }
Use docker inspect to examine the full configuration of a container. This reveals misconfigurations in environment variables, command arguments, and network settings that may not appear in logs.
Additional Troubleshooting
# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"
If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.
Prevention
- Set appropriate memory limits based on application profiling.
- Add memory monitoring and alerting.
- Implement application-level memory limits.
- Use
--memory-reservationto set soft limits. - Profile memory usage under load before deploying.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro