✦ For everyone, free.

Practical knowledge for real and everyday life

Home

6.3.4 Container Resource Limits

A focused guide to Container Resource Limits, connecting core concepts with practical Docker and container operations.

Container resource limits constrain how much CPU, memory, and other host resources a given container is allowed to consume, preventing any single container from monopolizing shared resources and starving other containers (or the host itself) of what they need.

Why Resource Limits Matter on a Shared Host

Without limits, a single misbehaving or unexpectedly resource-hungry container could consume enough CPU or memory to degrade or even crash other containers and processes running on the same host.

docker run -d --memory=512m --cpus=1.0 myapp:1.0

This container is constrained to at most 512MB of memory and the equivalent of one full CPU core, regardless of how much it might otherwise attempt to consume.

Setting Memory and CPU Limits Together

Most production deployments specify both memory and CPU limits together, reflecting a complete resource budget for the container.

docker run -d --memory=1g --memory-reservation=512m --cpus=2.0 myapp:1.0

--memory-reservation sets a soft limit the container is encouraged to stay within under contention, while --memory sets the hard limit it cannot exceed.

Observing Actual Resource Usage Against Configured Limits

Comparing a container's actual resource consumption against its configured limits helps confirm whether the limits are appropriately sized — neither so tight that the application is constantly constrained, nor so loose that they provide no meaningful protection.

docker stats myapp
What Happens When a Limit Is Exceeded

Exceeding a memory limit typically results in the container's process being killed (commonly visible as exit code 137); exceeding a CPU limit instead simply throttles the container's CPU usage rather than terminating it.

docker inspect myapp --format '{{.State.ExitCode}}'

An exit code of 137 here is a strong signal the container was killed for exceeding its memory limit.

Why Container Resource Limits Matter

Appropriately configured resource limits are essential for reliable, predictable behavior on any host running multiple containers, preventing the noisy-neighbor problem where one container's excessive resource consumption degrades the performance or stability of others sharing the same host.

Content in this section