Sub topic 14
Limiting memory and CPU
This chapter assumes you can start containers with docker run, list them with docker ps, and read basic Linux resource terms such as memory and CPU cores. By default, a container can use as much of the host's memory and CPU as the kernel allows. One runaway container can then slow down or crash everything else on the machine. Resource limits stop that.
How Docker enforces limits
Docker uses Linux control groups (cgroups) to cap what a container's processes can consume. You set limits with flags on docker run. The two most common are --memory and --cpus.
docker run -d --name limited --memory 256m --cpus 0.5 --restart unless-stopped nginx:1.27This command does three things:
--memory 256mcaps the container at 256 MB of RAM.--cpus 0.5allows the container half of one CPU core's time.--restart unless-stoppedrestarts the container after a crash or a daemon restart, unless you stopped it yourself.
Memory and CPU behave differently
The two limits fail in different ways, and this is the main thing to understand.
| Resource | What happens at the limit | Visible symptom |
|---|---|---|
| Memory | The kernel kills a process in the container (OOM kill) | Container exits, often with code 137 |
| CPU | The container is throttled and runs slower | Higher latency, no crash |
Memory cannot be "slowed down", so exceeding it ends the process. CPU time can be shared out, so exceeding it only delays work.
Checking that limits apply
Use docker stats to see live usage against the limit. The --no-stream flag prints one snapshot and exits.
docker stats --no-stream limitedThe MEM USAGE / LIMIT column should show a limit of about 256 MiB. To read the configured values directly, use docker inspect with a format string. Docker stores memory in bytes and CPU in billionths of a core (nano CPUs).
docker inspect --format 'mem={{.HostConfig.Memory}} nanocpus={{.HostConfig.NanoCpus}}' limitedFor the container above, this prints mem=268435456 nanocpus=500000000.
Seeing an out-of-memory kill
You can trigger an OOM kill on purpose with a very small limit and a process that allocates more than that. The tail /dev/zero command keeps reading into memory until the kernel stops it.
docker run --name oomtest --memory 20m busybox tail /dev/zero
docker inspect --format 'oom={{.State.OOMKilled}} exit={{.State.ExitCode}}' oomtest
docker rm oomtestThe second command should report oom=true exit=137. Exit code 137 is 128 plus signal 9 (SIGKILL).
Common mistakes
- Setting the memory limit too close to normal usage. Applications spike during startup and garbage collection. Leave headroom, and measure with
docker statsfirst. - Combining a tight limit with
--restart always. A container that is OOM-killed on every start will restart in a loop. Readdocker logsand raise the limit or fix the leak. - Expecting
--cpusto pin the container to specific cores. It limits total CPU time, not which cores are used. Use--cpuset-cpusif you need pinning. - Assuming a runtime sees the limit. Some older language runtimes size their heap from the host's memory, not the container's. Check your runtime's container support.
Cleaning up
docker stop limited
docker rm limitedExercise
A container started with --memory 128m --restart unless-stopped keeps restarting every few seconds. docker inspect shows OOMKilled=true. Name two things you could do, and say what you would check first.
Show answer
Check docker logs and docker stats to see how much memory the application actually needs. Then either raise the --memory limit to fit its real usage with some headroom, or fix or configure the application so it uses less memory (for example, a smaller cache or heap size). The restart policy is only repeating the crash, so it is not the cause.
Exercise
You run a CPU-heavy job with --cpus 1 on a host with 4 cores. Will the container crash if the job wants more CPU? What will you observe instead?
Show answer
No. CPU limits throttle rather than kill. The job runs slower because it is held to the equivalent of one core's time, and docker stats shows CPU usage near 100%.