0
0
DockerDebug / FixBeginner · 3 min read

How to Fix OOM Killed Container in Docker

An OOMKilled container means it ran out of memory and was stopped by the system. Fix this by increasing the container's memory limit using --memory flag or optimizing your app to use less memory.
🔍

Why This Happens

An OOMKilled error happens when your container tries to use more memory than the limit set or more than the host can provide. The system stops the container to protect itself from running out of memory.

This usually occurs when the container runs a process that needs more memory than allowed or leaks memory over time.

bash
docker run --memory=100m my-app
Output
Error: container killed due to out of memory (OOMKilled)
🔧

The Fix

Increase the memory limit for your container to give it enough space to run. Use the --memory flag with a higher value when starting the container. Also, check your app for memory leaks or heavy memory use.

bash
docker run --memory=500m my-app
Output
Container runs without OOMKilled error
🛡️

Prevention

To avoid OOMKilled errors in the future, always set appropriate memory limits based on your app's needs. Monitor container memory usage regularly and optimize your app to use memory efficiently. Use tools like docker stats to watch memory in real time.

Consider adding swap memory or using Kubernetes resource requests and limits if running in a cluster.

⚠️

Related Errors

Other errors related to memory include:

  • Container restarting due to repeated OOM kills.
  • High CPU usage caused by memory swapping.
  • Docker daemon out of memory affecting all containers.

Fixes usually involve adjusting resource limits or optimizing the app.

Key Takeaways

OOMKilled means your container ran out of memory and was stopped.
Increase container memory limit with --memory flag to fix the issue.
Monitor memory usage regularly to prevent future OOM kills.
Optimize your application to use less memory and avoid leaks.
Use container orchestration tools to manage resource limits effectively.