Sub topic 13
Resource limits and cgroup v2 delegation
This chapter assumes you have a working rootless daemon, a basic idea of what cgroups are, and that your host uses cgroup v2. Control groups (cgroups) are the kernel feature that limits how much CPU, memory, and process count a group of processes can use.
Why limits behave differently in rootless mode
In rootful Docker, the daemon runs as root and can create cgroups anywhere in the hierarchy. A rootless daemon is an ordinary user process, so it can only manage the part of the cgroup tree that systemd hands over to it. This hand-over is called delegation. Each controller (cpu, memory, io, pids) is delegated separately. If a controller is not delegated to your user, Docker cannot enforce limits for it.
Rootless mode requires cgroup v2. On cgroup v1 hosts, flags such as --memory and --cpus are not supported in rootless mode.
Checking what is delegated
Read the list of controllers available to your user service:
cat "/sys/fs/cgroup/user.slice/user-$(id -u).slice/user@$(id -u).service/cgroup.controllers"On many systems, the default output is only memory pids. The cpu, cpuset, and io controllers are missing, so CPU limits are ignored until you delegate them.
Delegating more controllers
Create a systemd drop-in that tells every user service manager to receive the extra controllers:
sudo mkdir -p /etc/systemd/system/[email protected]
sudo tee /etc/systemd/system/[email protected]/delegate.conf > /dev/null <<'EOF'
[Service]
Delegate=cpu cpuset io memory pids
EOF
sudo systemctl daemon-reloadDelegation is read when the user service manager starts. Log out and back in, or reboot, and then restart the daemon:
systemctl --user restart dockerRun the cat command again. The file should now list cpu, cpuset, io, memory, and pids.
Applying limits
Once the controllers are delegated, the normal flags work:
docker run --rm -d --name capped --cpus 0.5 --memory 256m --pids-limit 100 alpine sleep 300
docker stats --no-stream capped
docker rm -f cappedThe docker stats output should show a memory limit of 256 MiB.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Warning that no CPU limit support is available in `docker info` | `cpu` controller not delegated | Add the drop-in and log in again |
| Limit flags are accepted but have no effect | Controller missing from `cgroup.controllers` | Compare the file with the controllers you need |
| Limits work after reboot but not after `su` or `sudo -u` | User service manager not started for that session | Log in with a real session, or enable lingering |
Exercise
After running docker run --cpus 0.5 in rootless mode, you find the container uses a full CPU core. cgroup.controllers for your user service lists only memory pids. What do you change, and what must you do before the change takes effect?
Show answer
Add a drop-in for [email protected] with Delegate=cpu cpuset io memory pids (at least cpu), run sudo systemctl daemon-reload, then log out and back in or reboot so the user service manager restarts with the new delegation. Restart the rootless daemon with systemctl --user restart docker, and confirm cpu now appears in cgroup.controllers.