Bird
Raised Fist0
Dockerdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does setting a CPU limit for a Docker container do?
easy
A. It pauses the container when CPU usage is high.
B. It restricts the maximum CPU the container can use.
C. It sets the container to use all available CPUs.
D. It guarantees the container will always get that CPU amount.

Solution

  1. Step 1: Understand CPU limit meaning

    A CPU limit restricts the maximum CPU a container can use, preventing it from using more than set.
  2. Step 2: Differentiate from CPU reservation

    CPU reservation guarantees minimum CPU, not a maximum limit.
  3. Final Answer:

    It restricts the maximum CPU the container can use. -> Option B
  4. Quick Check:

    CPU limit = max CPU allowed [OK]
Hint: Limit means max CPU allowed, reservation means minimum guaranteed [OK]
Common Mistakes:
  • Confusing CPU limit with CPU reservation
  • Thinking CPU limit guarantees CPU
  • Assuming CPU limit pauses container
2. Which of the following is the correct Docker run command to limit a container to use at most 0.5 CPUs?
easy
A. docker run --cpus=0.5 myimage
B. docker run --cpu-limit=0.5 myimage
C. docker run --cpu-reservation=0.5 myimage
D. docker run --cpu=0.5 myimage

Solution

  1. Step 1: Identify correct flag for CPU limit

    The correct Docker flag to limit CPU usage is --cpus, which accepts a decimal value.
  2. Step 2: Check other options for correctness

    --cpu-limit and --cpu-reservation are invalid flags; --cpu is not a valid flag either.
  3. Final Answer:

    docker run --cpus=0.5 myimage -> Option A
  4. Quick Check:

    Use --cpus to set CPU limit [OK]
Hint: Use --cpus flag to limit CPU usage in docker run [OK]
Common Mistakes:
  • Using non-existent flags like --cpu-limit
  • Confusing CPU limit with CPU reservation flags
  • Using --cpu instead of --cpus
3. Given the command docker service create --cpus=1.5 --reserve-cpu=1 myimage, what does this configuration mean?
medium
A. The container can use up to 1.5 CPUs and is guaranteed 1 CPU.
B. The container is limited to 1 CPU and has no guaranteed CPU.
C. The container can use up to 1 CPU and is guaranteed 1.5 CPUs.
D. The container is guaranteed 1.5 CPUs and limited to 1 CPU.

Solution

  1. Step 1: Interpret --cpus flag

    --cpus=1.5 means the container can use up to 1.5 CPUs maximum.
  2. Step 2: Interpret --reserve-cpu flag

    --reserve-cpu=1 means the container is guaranteed at least 1 CPU.
  3. Final Answer:

    The container can use up to 1.5 CPUs and is guaranteed 1 CPU. -> Option A
  4. Quick Check:

    --cpus = max CPU, --reserve-cpu = guaranteed CPU [OK]
Hint: Max CPU = --cpus, guaranteed CPU = --reserve-cpu [OK]
Common Mistakes:
  • Swapping meanings of limit and reservation
  • Assuming reservation is max CPU
  • Ignoring one of the flags
4. You run docker service create --reserve-cpu=2 myimage on a host with only 1 CPU. What is the likely outcome?
medium
A. Docker will reserve 2 CPUs regardless of host capacity.
B. Docker will throw an error and refuse to start the container.
C. Docker will reserve CPU as best as possible but cannot guarantee 2 CPUs.
D. Docker will limit the container to 1 CPU automatically.

Solution

  1. Step 1: Understand CPU reservation vs host capacity

    CPU reservation requests minimum CPU but Docker cannot guarantee more CPUs than host has.
  2. 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.
  3. Final Answer:

    Docker will reserve CPU as best as possible but cannot guarantee 2 CPUs. -> Option C
  4. Quick Check:

    Reservation tries best effort, no error if host CPU insufficient [OK]
Hint: Reservation can't exceed host CPU, but no error thrown [OK]
Common Mistakes:
  • Expecting error when reservation > host CPU
  • Thinking Docker limits container automatically
  • Confusing reservation with limit
5. You want to run two containers on a 4-CPU host. Container A needs at least 1 CPU but can use up to 2 CPUs. Container B needs at least 1.5 CPUs but can use up to 2.5 CPUs. Which of the following flag sets correctly set CPU limits and reservations to meet these requirements without overcommitting the host?
hard
A. Container A: --reserve-cpu=1.5 --cpus=2, Container B: --reserve-cpu=1 --cpus=2.5
B. Container A: --reserve-cpu=2 --cpus=1, Container B: --reserve-cpu=2.5 --cpus=1.5
C. Container A: --reserve-cpu=1 --cpus=1.5, Container B: --reserve-cpu=1.5 --cpus=2
D. Container A: --reserve-cpu=1 --cpus=2, Container B: --reserve-cpu=1.5 --cpus=2.5

Solution

  1. 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.
  2. 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.
  3. Step 3: Verify options

    Container A: --reserve-cpu=1 --cpus=2, Container B: --reserve-cpu=1.5 --cpus=2.5 matches requirements exactly. Others swap reservation and limit or exceed reservation beyond requirements.
  4. Final Answer:

    Container A: --reserve-cpu=1 --cpus=2, Container B: --reserve-cpu=1.5 --cpus=2.5 -> Option D
  5. Quick Check:

    Reservations sum < host CPUs, limits set max usage [OK]
Hint: Sum reservations ≤ host CPUs; limits can be higher [OK]
Common Mistakes:
  • Swapping reservation and limit values
  • Overcommitting CPU reservations beyond host capacity
  • Confusing reservation with limit in commands
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard