0
0
Dockerdevops~30 mins

CPU limits and reservations in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
CPU Limits and Reservations with Docker
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use docker inspect --format='{{.Spec.TaskTemplate.Resources.Reservations.NanoCPUs}} {{.Spec.TaskTemplate.Resources.Limits.NanoCPUs}}' webserver to see CPU reservation and limit in nanoseconds.