Resource Limits (Docker)

Docker resource limits are configuration directives that cap the CPU and memory a container can consume, preventing a single container from exhausting host resources and causing the entire server to become unresponsive.
DockerResource LimitsMemoryCPUOOMContainerDevOps

Without resource limits, any container can consume all available CPU and memory on the host. A memory leak in one container can OOM-kill other containers or the host kernel. A runaway process in one container can starve all other containers of CPU time.

Resource limits in Docker Compose are defined under the deploy.resources key (Compose v3) or the top-level mem_limit/cpus keys (Compose v2). Compose v3 limits require Docker Swarm mode unless you use the --compatibility flag with docker-compose.

Memory Limits

Memory limits prevent OOM conditions. Set both memory (hard limit) and memory_reservation (soft limit). When the hard limit is reached, Docker kills the container with OOM. When the soft limit is reached, Docker throttles memory allocation.

CPU Limits

cpus: "0.5" limits the container to 50% of one CPU core. cpu_shares is a relative weight — containers with higher shares get more CPU time under contention but are not hard-capped. For consistent limits, prefer cpus over cpu_shares.

Related Tools

Fix Guides

Frequently Asked Questions

What happens if I don't set memory limits on Docker containers?
A container with a memory leak or runaway process can consume all available RAM on the host. The Linux OOM killer will then terminate processes — often killing other containers or system services rather than the container at fault.
How do I set resource limits in docker-compose.yml?
In Compose v3 with Swarm: use deploy.resources.limits. Without Swarm, use the Compose v2 format: mem_limit: 512m and cpus: '0.5' at the service level. ConfigClarity's Docker Auditor flags services with no resource limits defined.
What is a good starting memory limit for a web service?
Start conservative — 256m to 512m for small services, 1g to 2g for larger applications. Monitor with docker stats under normal load and adjust. The OOM risk of setting limits too low is lower than the availability risk of not setting them.

Related Fix Guides