📖 Scenario: You are managing a Docker container that runs a small web server. You want to control how much CPU the container can use to keep your system stable and responsive.
🎯 Goal: Learn how to set CPU limits and CPU reservations for a Docker container using the docker service create command.
📋 What You'll Learn
Create a Docker container running the nginx image
Set a CPU reservation to guarantee minimum CPU share
Set a CPU limit to cap maximum CPU usage
Verify the container runs with the specified CPU settings
💡 Why This Matters
🌍 Real World
Controlling CPU usage of containers helps keep your system responsive and prevents one container from using too much CPU.
💼 Career
Understanding CPU limits and reservations is important for managing containerized applications in production environments.
Progress0 / 4 steps
1
Create a Docker container running nginx
Write a docker service create command to start a service named webserver using the nginx image.
Docker
Hint
Use docker service create --name webserver nginx to start the service.
2
Add a CPU reservation to guarantee minimum CPU share
Modify the docker service create command to add a CPU reservation of 0.5 CPUs using the --reserve-cpu option.
Docker
Hint
Add --reserve-cpu 0.5 to reserve half a CPU for the container.
3
Add a CPU limit to cap maximum CPU usage
Modify the docker service create command to add a CPU limit of 1.0 CPUs using the --limit-cpu option, keeping the CPU reservation from Step 2.
Docker
Hint
Add --limit-cpu 1.0 to limit the container to one CPU maximum.
4
Verify the container runs with the CPU settings
Write a docker inspect command to display the CPU reservation and CPU limit settings of the webserver service.
Docker
Hint
Use docker inspect --format='{{.Spec.TaskTemplate.Resources.Reservations.NanoCPUs}} {{.Spec.TaskTemplate.Resources.Limits.NanoCPUs}}' webserver to see CPU reservation and limit in nanoseconds.
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
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 B
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
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 A
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
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 A
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
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 C
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
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.