Updated November 2026: Rewritten for Docker Engine 29 and cgroups v2: how --cpus maps to cpu.max, --cpu-shares to cpu.weight, Compose deploy.resources, and how to spot throttling. All commands verified on Docker Desktop 4.94.

By default, Docker doesn’t limit CPU at all. A single container with a busy loop or a runaway build can eat every core the Docker host has. This post shows the three knobs I use to control that: --cpus for a hard limit, --cpuset-cpus to pin a container to specific cores, and --cpu-shares for priority under contention. For each one, I show what Docker writes into the container’s cgroup and how you check that the limit actually holds.

CPU is only half of the story. Limit memory as well; I cover that in Limit Memory for Docker Containers .

The Test Setup

All commands below ran on Docker Engine 29.8 (Docker Desktop 4.94) with 10 CPUs assigned to the Docker VM. On Docker Desktop, the “host” is a Linux VM; its CPU count is set in Settings > Resources > Advanced and is the upper bound for all containers. Check what your engine sees:

BASH
docker info --format '{{.NCPU}} CPUs, cgroup v{{.CgroupVersion}}'
# 10 CPUs, cgroup v2

Every current Linux distribution and Docker Desktop use cgroups v2, so that’s what this post covers.

To generate load, I don’t need a special stress image. Four yes processes in plain Alpine keep four cores busy:

BASH
docker run -d --rm --name cpu-test alpine:3.22 \
  sh -c 'for i in 1 2 3 4; do yes > /dev/null & done; wait'

docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}' cpu-test
# NAME       CPU %
# cpu-test   395.92%

docker stats reports 100% per fully used core, so without a limit the container takes four cores. Stop it with docker rm -f cpu-test.

Set a Hard Limit With –cpus

--cpus caps how much CPU time a container may use, expressed in cores. --cpus 1 means one core’s worth of time, spread across as many cores as the container likes:

BASH
docker run -d --rm --name cpu-test --cpus 1 alpine:3.22 \
  sh -c 'for i in 1 2 3 4; do yes > /dev/null & done; wait'

docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}' cpu-test
# NAME       CPU %
# cpu-test   99.61%

Under the hood, Docker translates --cpus into a CFS quota and period and writes both into the cpu.max file of the container’s cgroup. You can read it from inside the container:

BASH
docker exec cpu-test cat /sys/fs/cgroup/cpu.max
# 100000 100000

The first number is the quota, the second the period, both in microseconds. The container may use 100 ms of CPU time every 100 ms. With --cpus 0.5, the quota drops to 50000: the container’s threads run until they’ve consumed 50 ms in the current period, then the kernel throttles them until the next period starts.

How --cpus 0.5 maps to quota and period

You can change the limit of a running container without restarting it:

BASH
docker update --cpus 0.5 cpu-test

docker exec cpu-test cat /sys/fs/cgroup/cpu.max
# 50000 100000

docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}' cpu-test
# NAME       CPU %
# cpu-test   50.18%

--cpus was added as the friendly version of --cpu-period and --cpu-quota. You can still set those two directly, but not together with --cpus; Docker refuses with Conflicting options: Nano CPUs and CPU Period cannot both be set.

Docker also refuses limits above what the host has:

BASH
docker run --rm --cpus 12 alpine:3.22 true
# docker: Error response from daemon: range of CPUs is from 0.01 to 10.00, as there are only 10 CPUs available

Spot Throttling With cpu.stat

A hard limit that’s too tight shows up as latency, not as an error. The kernel counts how often the container hit its quota in cpu.stat:

BASH
docker exec cpu-test cat /sys/fs/cgroup/cpu.stat
# usage_usec 6649866
# ...
# nr_periods 66
# nr_throttled 66
# throttled_usec 19648750

nr_throttled close to nr_periods means the container wants more than its limit in nearly every period. For a CPU burner that’s expected; for a web API, it’s the reason your p99 latency looks bad. Stop the test container with docker rm -f cpu-test.

Pin Containers to CPUs With –cpuset-cpus

--cpuset-cpus restricts which cores the container’s processes may run on. It accepts a comma-separated list (0,2,4) or a range (1-3); cores are zero-based:

BASH
docker run -d --rm --name pinned --cpuset-cpus 1 alpine:3.22 \
  sh -c 'for i in 1 2 3 4; do yes > /dev/null & done; wait'

docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}' pinned
# NAME     CPU %
# pinned   99.30%

docker exec pinned cat /sys/fs/cgroup/cpuset.cpus.effective
# 1
docker exec pinned nproc
# 1

Four busy processes, one core: the container never gets past 100%. Unlike --cpus, the cpuset also changes what the container sees. nproc reports 1, so runtimes that size their thread pools by the number of CPUs (Go, .NET, the JVM, Node.js worker pools) pick sensible defaults. With --cpus alone, nproc still reports all 10 cores.

Asking for a core that doesn’t exist fails at start:

BASH
docker run --rm --cpuset-cpus 12 alpine:3.22 true
# docker: Error response from daemon: Requested CPUs are not available - requested 12, available: 0-9

Prioritize With –cpu-shares

--cpu-shares is a relative weight, not a limit. It only matters when containers compete for the same cores; if there’s spare capacity, every container gets what it asks for. The default is 1024.

To force contention, I pin two containers to the same core and give one twice the shares:

BASH
docker run -d --rm --name high --cpuset-cpus 0 --cpu-shares 2048 alpine:3.22 sh -c 'yes > /dev/null'
docker run -d --rm --name low  --cpuset-cpus 0 --cpu-shares 1024 alpine:3.22 sh -c 'yes > /dev/null'

docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}' high low
# NAME   CPU %
# high   61.53%
# low    36.50%

docker rm -f high low

That’s not the 2:1 split you’d expect from cgroups v1. cgroups v2 has no shares; it has cpu.weight with a range of 1 to 10000 and a default of 100. The container runtime converts your shares into a weight, and recent runc versions use a non-linear mapping that keeps 1024 at 100 but doesn’t scale the rest proportionally. Here is what I got on Docker Engine 29.8:

--cpu-shares cpu.weight
2 1
256 35
512 59
1024 (default) 100
2048 174
4096 303

The split above matches the weights: 174 to 100 is roughly 63% to 37%. Check the weight of your own container with cat /sys/fs/cgroup/cpu.weight, and treat shares as “more” or “less” rather than as an exact ratio.

CPU Limits in Docker Compose

In Compose, the hard limit lives under deploy.resources. docker compose up applies it; you don’t need Swarm for that anymore. Cpuset and shares are service-level attributes:

YAML
services:
  burner:
    image: alpine:3.22
    command: sh -c 'for i in 1 2 3 4; do yes > /dev/null & done; wait'
    cpuset: "0,1"
    cpu_shares: 512
    deploy:
      resources:
        limits:
          cpus: "0.5"
BASH
docker compose up -d
docker compose exec burner cat /sys/fs/cgroup/cpu.max /sys/fs/cgroup/cpu.weight /sys/fs/cgroup/cpuset.cpus.effective
# 50000 100000
# 59
# 0-1
docker compose down

Which One Do I Use?

  • --cpus when a container must never take more than a fixed amount of CPU time. It’s the same quota and period mechanism Kubernetes uses for CPU limits.
  • --cpuset-cpus when I want to isolate noisy containers on dedicated cores, or when a runtime should see fewer CPUs.
  • --cpu-shares when everything may use idle CPU, but one container should win when it gets tight.

They combine fine. I set limits on my local machine the same way I do in a cluster; it surfaces throttling problems long before they hit production.