0
0
Dockerdevops~10 mins

CPU limits and reservations in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - CPU limits and reservations
Start Container
Check CPU Reservation
Guarantee CPU Shares
Check CPU Limit
Restrict CPU Usage
Run Container with Constraints
This flow shows how Docker starts a container by first reserving CPU shares, then applying CPU usage limits, and finally running the container with these CPU constraints.
Execution Sample
Docker
docker run --cpus=1.5 --cpu-shares=512 alpine top
Runs an Alpine container limiting CPU usage to 1.5 CPUs and reserving CPU shares of 512.
Process Table
StepActionParameter CheckedValueEffect on Container
1Start container creation--Prepare container environment
2Check CPU reservationcpu-shares512Container guaranteed relative CPU priority
3Check CPU limit--cpus1.5Container limited to max 1.5 CPUs usage
4Apply CPU constraintscpu-shares & --cpus512 & 1.5CPU scheduler enforces limits
5Run container--Container runs with CPU limits and reservations
6Exit--Container runs until stopped or exited
💡 Container runs with CPU limits and reservations applied as specified
Status Tracker
VariableStartAfter Step 2After Step 3Final
cpu-sharesdefault 1024512512512
cpu-limit (cpus)unlimitedunlimited1.51.5
Key Moments - 3 Insights
Why does setting --cpu-shares not limit CPU usage strictly?
Because cpu-shares only set relative priority for CPU time when CPU is contended, not a hard limit. See execution_table step 2 vs step 3.
What happens if --cpus is not set but cpu-shares is?
The container can use unlimited CPU but with relative priority defined by cpu-shares. This is shown by cpu-limit variable staying 'unlimited' in variable_tracker after step 2.
Can CPU usage exceed the --cpus limit?
No, --cpus sets a hard limit enforced by the scheduler, so usage cannot exceed this. See execution_table step 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what CPU limit value is applied?
AUnlimited CPU
B512 CPU shares
C1.5 CPUs
D2 CPUs
💡 Hint
Check the 'Value' column at step 3 in execution_table
According to variable_tracker, what is the cpu-shares value after step 2?
A512
B1024
C2048
D256
💡 Hint
Look at the 'cpu-shares' row under 'After Step 2' in variable_tracker
If --cpus was removed from the command, how would the cpu-limit variable change in variable_tracker?
AIt would become 512
BIt would remain 'unlimited'
CIt would become 1.5
DIt would become 0
💡 Hint
Refer to variable_tracker 'cpu-limit' value before and after step 3
Concept Snapshot
Docker CPU limits and reservations:
- --cpu-shares sets relative CPU priority (default 1024)
- --cpus sets hard CPU usage limit (e.g., 1.5 CPUs)
- cpu-shares affect scheduling when CPU is busy
- --cpus restricts max CPU usage strictly
- Combine both for guaranteed and limited CPU usage
Full Transcript
This visual execution shows how Docker applies CPU limits and reservations when running a container. First, the container is prepared. Then Docker checks and applies CPU shares, which set the relative priority for CPU time when the CPU is busy. Next, Docker applies the CPU limit using the --cpus option, which restricts the container's maximum CPU usage strictly. Both settings are enforced by the CPU scheduler before the container runs. The variable tracker shows cpu-shares changing from the default 1024 to 512, and cpu-limit changing from unlimited to 1.5 CPUs. Key moments clarify that cpu-shares do not limit CPU usage strictly but prioritize it, and --cpus sets a hard limit. The quizzes test understanding of these values at different steps.