What if one app could silently steal all your computer's power without you noticing?
Why CPU limits and reservations in Docker? - Purpose & Use Cases
Imagine running many apps on one computer without telling it how much CPU each app can use. Sometimes one app hogs all the CPU, making others slow or freeze.
Manually watching and stopping apps when they use too much CPU is tiring and slow. You might miss the moment an app slows everything down, causing crashes or unhappy users.
CPU limits and reservations let you tell your computer exactly how much CPU each app can use. This keeps apps running smoothly without fighting for resources.
docker run myapp
docker run --cpus=1.5 --cpu-shares=1000 myapp
You can run many apps together without one stealing all the CPU, keeping everything fast and fair.
A website with many services uses CPU limits so the payment service never slows down, even if the chat service gets busy.
Without CPU limits, apps can slow each other down.
Manual control is hard and error-prone.
CPU limits and reservations keep apps running smoothly and fairly.
Practice
Solution
Step 1: Understand CPU limit meaning
A CPU limit restricts the maximum CPU a container can use, preventing it from using more than set.Step 2: Differentiate from CPU reservation
CPU reservation guarantees minimum CPU, not a maximum limit.Final Answer:
It restricts the maximum CPU the container can use. -> Option BQuick Check:
CPU limit = max CPU allowed [OK]
- Confusing CPU limit with CPU reservation
- Thinking CPU limit guarantees CPU
- Assuming CPU limit pauses container
Solution
Step 1: Identify correct flag for CPU limit
The correct Docker flag to limit CPU usage is --cpus, which accepts a decimal value.Step 2: Check other options for correctness
--cpu-limit and --cpu-reservation are invalid flags; --cpu is not a valid flag either.Final Answer:
docker run --cpus=0.5 myimage -> Option AQuick Check:
Use --cpus to set CPU limit [OK]
- Using non-existent flags like --cpu-limit
- Confusing CPU limit with CPU reservation flags
- Using --cpu instead of --cpus
docker service create --cpus=1.5 --reserve-cpu=1 myimage, what does this configuration mean?Solution
Step 1: Interpret --cpus flag
--cpus=1.5 means the container can use up to 1.5 CPUs maximum.Step 2: Interpret --reserve-cpu flag
--reserve-cpu=1 means the container is guaranteed at least 1 CPU.Final Answer:
The container can use up to 1.5 CPUs and is guaranteed 1 CPU. -> Option AQuick Check:
--cpus = max CPU, --reserve-cpu = guaranteed CPU [OK]
- Swapping meanings of limit and reservation
- Assuming reservation is max CPU
- Ignoring one of the flags
docker service create --reserve-cpu=2 myimage on a host with only 1 CPU. What is the likely outcome?Solution
Step 1: Understand CPU reservation vs host capacity
CPU reservation requests minimum CPU but Docker cannot guarantee more CPUs than host has.Step 2: Behavior when reservation exceeds host CPUs
Docker tries to reserve as much as possible but cannot guarantee more than available CPUs; it does not error out.Final Answer:
Docker will reserve CPU as best as possible but cannot guarantee 2 CPUs. -> Option CQuick Check:
Reservation tries best effort, no error if host CPU insufficient [OK]
- Expecting error when reservation > host CPU
- Thinking Docker limits container automatically
- Confusing reservation with limit
Solution
Step 1: Check CPU reservations sum
Container A reserves 1 CPU, Container B reserves 1.5 CPUs; total 2.5 CPUs < 4 CPUs host, so no overcommit.Step 2: Check CPU limits sum
Container A max 2 CPUs, Container B max 2.5 CPUs; total 4.5 CPUs > 4 CPUs host, but limits can exceed host CPUs as they are max caps, not guaranteed.Step 3: Verify options
Container A:--reserve-cpu=1 --cpus=2, Container B:--reserve-cpu=1.5 --cpus=2.5matches requirements exactly. Others swap reservation and limit or exceed reservation beyond requirements.Final Answer:
Container A: --reserve-cpu=1 --cpus=2, Container B: --reserve-cpu=1.5 --cpus=2.5 -> Option DQuick Check:
Reservations sum < host CPUs, limits set max usage [OK]
- Swapping reservation and limit values
- Overcommitting CPU reservations beyond host capacity
- Confusing reservation with limit in commands
Ready to prove it?
Pass the test and this pattern moves unknown
