CPU and Memory Controls
CPU and Memory Controls
Containers share the host kernel, so limits are cgroup settings you must set yourself:
docker run -d --name api \
--cpus 1.5 \
--memory 512m --memory-swap 512m \
--cpu-shares 512 \
registry.example.com/api:1.4
--cpus throttles to 1.5 cores on average; --memory is a hard ceiling — exceeding it triggers the OOM killer instead of letting one service evict the host's page cache. --memory-swap bounds RAM plus swap together; setting it equal to memory disables swap for the container. --cpu-shares only matters under contention: it weights fair-share CPU, it does not cap usage.
Processes, Descriptors, and I/O
Processes, Descriptors, and I/O
docker run -d --pids-limit 256 \
--ulimit nofile=65536:65536 \
--device-read-bps /dev/sda: 10mb \
myapp:1.4
--pids-limit caps the PID namespace so a fork bomb dies inside the container instead of exhausting host PIDs. nofile raises open-file ceilings for proxies and Node servers that otherwise hit EMFILE under load. On cgroup v2, I/O is shaped with --storage-opt or io.max per device; --device-read-bps caps throughput for a bound device. Verify with cat /sys/fs/cgroup/... inside the container or docker stats.
Network, Logging, and Pitfalls
Network, Logging, and Pitfalls
Network mode. Bridge adds NAT overhead per connection; --network host removes it but exposes every port and skips Docker's port mapping — a throughput win with a blast-radius cost.
Logs fill disks. The default json-file driver grows without bound; cap it:
docker run --log-opt max-size=10m --log-opt max-file=5 myapp:1.4
Swap. Unlimited swap hides memory leaks until the host thrashes; bound it.
Interview: size a container from observed docker stats numbers, state which limit prevents which failure (OOM, EMFILE, fork bomb, disk-full), and defend any limit you would leave unset.
Quiz
1. What happens when a container exceeds its --memory limit?
2. When does --cpu-shares actually change behavior?
3. Why set --log-opt max-size and max-file?
Flashcards
Question
--cpus vs --cpu-shares
Click to reveal answer
Answer
--cpus is a quota; shares only weight distribution when the host is contended.
Question
--pids-limit
Click to reveal answer
Answer
Caps processes in the namespace — stops fork bombs from exhausting host PIDs.
Question
nofile ulimit
Click to reveal answer
Answer
Open-file ceiling; proxies and Node need 65536+ to avoid EMFILE under load.
Question
Memory limit behavior
Click to reveal answer
Answer
Hard cgroup ceiling → OOM kill inside the container; --memory-swap bounds RAM+swap together.
Question
max-size/max-file
Click to reveal answer
Answer
json-file rotation options bounding disk use per container.
Revision Notes
Key Takeaways
- 1. Set memory, pids, and log limits on every container by default
- 2. shares are weights, cpus is a quota — they are not interchangeable
- 3. host networking trades isolation for throughput
Interview Tips
- • Map each failure mode (OOM, EMFILE, fork bomb, disk full) to the exact flag that prevents it
- • Show how you would right-size limits from docker stats observations
Cheat Sheet
--cpus quota, --cpu-shares weight; --memory + --memory-swap → OOM kill; --pids-limit; ulimit nofile=65536; --log-opt max-size=10m,max-file=5.