0
0
Dockerdevops~5 mins

CPU limits and reservations in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your computer runs many programs at once, and some use too much power. CPU limits and reservations help control how much computer power each program can use so everything runs smoothly without slowing down.
When you want to make sure a critical app always has enough CPU power to work well.
When running multiple containers on one machine and you want to prevent one from using all CPU.
When testing how your app behaves with limited CPU resources.
When you want to avoid your server becoming slow because one container uses too much CPU.
When you want to plan resource use before adding more containers to your system.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  my-app:
    image: nginx:1.23
    deploy:
      resources:
        limits:
          cpus: '0.5'
        reservations:
          cpus: '0.25'

This file tells Docker to run an Nginx app with CPU limits and reservations.

limits.cpus means the app can use up to half a CPU core.

reservations.cpus means Docker tries to keep at least a quarter of a CPU core available for this app.

Commands
This command starts the app using the docker-compose file and applies the CPU limits and reservations defined there.
Terminal
docker stack deploy -c docker-compose.yml my-stack
Expected OutputExpected
Creating network my-stack_default Creating service my-stack_my-app
-c - Specifies the compose file to use
This command lists all running services to check if our app started successfully.
Terminal
docker service ls
Expected OutputExpected
ID NAME MODE REPLICAS IMAGE PORTS abc123def456 my-stack_my-app replicated 1/1 nginx:1.23
This command shows the CPU limits and reservations set for the running service in JSON format.
Terminal
docker service inspect my-stack_my-app --format '{{json .Spec.TaskTemplate.Resources}}'
Expected OutputExpected
{"Limits":{"NanoCPUs":500000000},"Reservations":{"NanoCPUs":250000000}}
--format - Formats output to show only resource info in JSON
Key Concept

If you remember nothing else from this pattern, remember: CPU limits stop a container from using too much CPU, and reservations guarantee it some CPU to run smoothly.

Common Mistakes
Setting CPU limits without reservations
The container might get less CPU than it needs and slow down because no CPU is guaranteed.
Always set reservations along with limits to reserve CPU for your container.
Using CPU values greater than the host's CPU count
Docker cannot allocate more CPU than physically available, causing errors or ignored settings.
Set CPU limits and reservations within the total CPU cores your machine has.
Using CPU limits in docker run without swarm mode
CPU limits in the deploy section only work with Docker Swarm stacks, not with simple docker run commands.
Use --cpus flag with docker run for limits outside swarm mode.
Summary
Create a docker-compose.yml file with CPU limits and reservations under deploy.resources.
Deploy the stack with docker stack deploy to apply CPU controls.
Verify the service is running and check CPU settings with docker service inspect.