Sub topic 15
Choosing values and avoiding common mistakes
This chapter assumes you can run containers with docker run, and that you know the --memory, --memory-swap, --cpus and --cpu-shares flags from earlier chapters. It also assumes you have seen how an out-of-memory (OOM) kill shows up in docker inspect. Here you will pick numbers for those flags and avoid the mistakes that cause most limit-related incidents.
Start from measurement
Do not guess limits. Run the workload under realistic load with no limit, then record usage with docker stats. Watch peak memory, not the average. Set the memory limit 20 to 30 percent above the observed peak, so normal spikes do not trigger an OOM kill.
docker stats --no-stream --format 'table {{.Name}}\t{{.MemUsage}}\t{{.CPUPerc}}'For CPU, the average over a busy period is a good starting point for --cpus. A CPU cap throttles the container but never kills it, so a limit that is slightly too low costs latency instead of availability.
Memory and CPU fail differently
| Resource | When the limit is exceeded | Typical symptom |
|---|---|---|
| Memory (`--memory`) | The kernel kills a process | Exit code 137, `OOMKilled true` |
| CPU (`--cpus`) | The container is throttled | Slow responses, no crash |
Because of this difference, be more generous with memory than with CPU. A tight memory limit causes outages. A tight CPU limit causes slowness.
Choosing the CPU control
- Use
--cpuswhen you need a predictable ceiling, for example on a shared build server. - Use
--cpu-shareswhen you want idle CPU to stay usable but need priority under contention. - Use
--cpuset-cpusfor pinning, such as keeping a latency-sensitive service off cores used by other work.
Common mistakes
- Setting
--memory-swapbelow--memory. Docker rejects this. Remember that--memory-swapis the total of memory plus swap, not swap alone. - Forgetting swap. With
--memory 256mand no--memory-swap, the container can use up to 256 MiB of swap on top of RAM when the host has swap enabled. It may slow down instead of being killed. Set--memory-swapequal to--memoryfor a strict limit. - Using
--cpu-sharesas a cap. Shares do nothing on an idle host, so a container with low shares can still use every core. - Ignoring the runtime. Some runtimes size heaps or thread pools from the host's totals, not from the container's limits. A process that believes it has 64 GiB will exceed a 512 MiB limit and be killed. Use runtime flags that read the container limit, or set sizes explicitly.
- Setting a reservation above the limit.
--memory-reservationmust be lower than--memory, or it has no effect. - Treating reservation as a guarantee. It is a soft limit used under host memory pressure. It does not reserve memory.
- Not testing the limit. After setting values, confirm them with
docker inspectand watch behavior under load.
A worked example
Suppose a web service peaks at 180 MiB and averages 0.3 CPU cores. A reasonable starting point is:
docker run -d --name api \
--memory 256m --memory-swap 256m \
--memory-reservation 200m \
--cpus 0.5 \
nginx:alpineThe memory limit leaves about 40 percent headroom over the peak. Swap is disabled, so a leak ends in a visible OOM kill instead of slow paging. The CPU cap sits above the average, so ordinary load is not throttled. After a week, review docker stats and adjust.
Exercise
A container has --memory 512m and no --memory-swap. The host has swap enabled. Under a memory leak, the container becomes very slow but is not killed for a long time. Why, and what change makes it fail fast?
Show answer
When --memory-swap is not set, the container can use swap equal to its memory limit, so it can use up to about 1 GiB in total and spends its time paging. Setting --memory-swap 512m (equal to --memory) disables swap, so the kernel kills the process at 512 MiB and docker inspect reports OOMKilled true with exit code 137.