Back to Tech Q&A
Tech Q&A

Docker Container Memory Limits

Dec 10, 2024
DockerDevOpsMemoryContainers
Question

My container keeps getting OOM killed. How do I debug this?

Answer

Run `docker stats` to monitor real-time memory usage. Also check `docker inspect <container_id>` for the memory limit configuration. Look at `/sys/fs/cgroup/memory/memory.max_usage_in_bytes` inside the container for peak usage.

Question

Should I set memory limits on all containers?

Answer

Yes, always. Without limits, one runaway container can starve others. Set both memory and memory-swap limits explicitly. A good starting point is 2-4x what the container typically uses.

Question

What's the difference between memory and memory-swap limits?

Answer

Memory limit is RAM only. Memory-swap is RAM + swap combined. If you set --memory=512m and --memory-swap=1g, the container can use 512MB RAM and 512MB swap. Set them equal to disable swap entirely.

Question

How do I see why a container was killed?

Answer

Check `docker inspect <container_id> --format='{{.State.OOMKilled}}'`. Also check system logs with `dmesg | grep -i kill` for OOM killer activity. In Kubernetes, use `kubectl describe pod` to see termination reasons.