⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Docker & Containers Interview Questions Scenario 7 of 159 in Docker & Containers
Senior DevOps / SRE [L2] Docker Production Scenario [L2]
🎯 Target Role / Context: Docker Scenario #7 (L2) · Docker

Q: A container is consuming 100% CPU. How do you limit it without restarting?

To limit container CPU without restarting it, run docker update --cpus 0.5 [container]. The daemon rewrites the container's cgroup CPU quota live, so the process keeps running with the same PID and state.

#Docker #L2 #Containers #Linux
🎙️ Candidate Opening & Architectural Context
"To <strong>limit container CPU without restarting</strong> it, run <code>docker update --cpus 0.5 &lt;container&gt;</code>. The daemon rewrites the container's cgroup CPU quota live, so the process keeps running with the same PID and state. Interviewers ask this because it separates people who know that resource limits are cgroup settings (adjustable at runtime) from those who only know <code>docker run</code> flags. A strong answer also covers containing the blast radius first, finding why the CPU is pegged, and making the limit permanent in Compose or your orchestrator so it survives the next redeploy."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

Direct Answer: To <strong>limit container CPU without restarting</strong> it, run <code>docker update --cpus 0.5 &lt;container&gt;</code>. The daemon rewrites the container's cgroup CPU quota live, so the process keeps running with the same PID and state. Interviewers ask this because it separates people who know that resource limits are cgroup settings (adjustable at runtime) from those who only know <code>docker run</code> flags. A strong answer also covers containing the blast radius first, finding why the CPU is pegged, and making the limit permanent in Compose or your orchestrator so it survives the next redeploy.

1️⃣

Mental model: CPU limits are live cgroup settings

Docker translates CPU flags into cgroup files on the host, and the kernel scheduler enforces them, which is why they can change while the process runs.

  • Hard cap: --cpus sets a CFS quota (cgroup v2 cpu.max). The container is throttled once it uses its quota in a scheduling period.
  • Relative weight: --cpu-shares only matters under contention. It does not cap anything on an idle host, so it will not stop a container from using 100% of a free CPU.
  • Pinning: --cpuset-cpus restricts which cores the container may run on, useful for noisy-neighbour isolation.
  • Why no restart: these values live in the cgroup, not inside the image or process, so the daemon can rewrite them at runtime.
  • Cgroup versions: modern distributions default to cgroup v2 where the quota is cpu.max, while older hosts use v1 files such as cpu.cfs_quota_us. The Docker command is identical, but the paths you inspect differ.
2️⃣

Hands-on: limit container CPU without restarting

Identify the offender, apply the cap, and check that it took effect.

docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}'
# NAME     CPU %     MEM USAGE / LIMIT
# my-app   398.12%   612MiB / 15.6GiB

docker update --cpus 1.5 my-app      # prints: my-app
docker update --cpu-shares 512 my-app  # lower weight under contention

# Pin to cores 0 and 1 only
docker update --cpuset-cpus 0,1 my-app
  • Find it: docker stats --no-stream shows CPU% per container. Note that 100% means one full core, so a busy multi-threaded process can show several hundred percent.
  • Apply the cap: docker update --cpus 1.5 my-app takes effect immediately for the running container.
  • Combine with memory carefully: if you lower --memory in the same call, also pass --memory-swap, otherwise the update can be rejected.
  • Restart policy: docker update --restart unless-stopped my-app uses the same mechanism if you also want crash recovery.
  • Safe first response: in an incident, cap the container first to protect neighbouring workloads on the same host, then investigate. A limit that is slightly too low is cheaper than a node where every service is starved.
Pro Tip: <code>--cpus</code> and the older <code>--cpu-period</code>/<code>--cpu-quota</code> flags express the same limit. Prefer <code>--cpus</code> and do not mix them.
Advertisement
3️⃣

Verify the limit is actually enforced

Never trust the command output alone. Confirm the setting and watch throttling.

  • Inspect the config: docker inspect --format '{{.HostConfig.NanoCpus}}' my-app should show 1500000000 for 1.5 CPUs.
  • Watch live usage: docker stats my-app should settle near 150% instead of the earlier spike.
  • Read the cgroup (v2): on the host, cat /sys/fs/cgroup/system.slice/docker-<full-id>.scope/cpu.max shows quota and period. With the cgroupfs driver the path differs, so check docker info for the cgroup driver and version.
  • Check throttling: cpu.stat in the same directory reports nr_throttled and throttled_usec. Rising values prove the cap is biting.
  • What to capture: before changing anything, save docker logs, docker top and a quick docker stats sample so the evidence exists for the post-incident review even if the problem disappears after throttling.
4️⃣

Failure modes: a cap treats the symptom, not the cause

Limiting CPU buys time. You still need to learn why the process is spinning.

  • Inspect processes: docker top my-app maps container processes to host PIDs, which you can profile with top -H -p <pid> or perf top -p <pid>.
  • Typical causes: an infinite retry loop, a runaway regex or GC thrash, a traffic spike, or a crypto-miner in a compromised image. Check docker logs --since 10m my-app and the image provenance.
  • Throttling hurts latency: a too-tight quota makes request-serving apps stall in bursts even though average CPU looks fine. Raise the cap if p99 latency worsens.
  • Kubernetes caveat: pod CPU limits are not resizable in place on 1.28-1.31 without enabling an alpha feature gate, so the equivalent there is editing the Deployment and letting it roll.
  • Decide whether to scale: if the load is legitimate traffic rather than a bug, a cap only moves the pain to users. Add replicas behind the load balancer or raise capacity instead of throttling real demand.
5️⃣

Production practice: make the limit permanent

docker update changes are lost when the container is recreated from its original definition.

services:
  my-app:
    image: registry.example.com/my-app:1.4.2
    deploy:
      resources:
        limits:
          cpus: "1.5"
          memory: 1G
  • Compose: set deploy.resources.limits.cpus (or service-level cpus) and run docker compose up -d to recreate with the new value.
  • Alert before it pages: export cAdvisor or Docker metrics to Prometheus and alert on sustained CPU near the limit plus throttling ratio.
  • Defaults: make limits mandatory in templates so no container ships without a cap.
  • Review cadence: revisit limits after each release, because a new code path can change the CPU profile, and stale limits are a common source of mysterious latency regressions.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: <code>docker update</code> is a stopgap. Record the new value in Compose or your manifest the same day, or the next recreate silently brings the 100% CPU problem back."
⚡ 60-Second Elevator Pitch Talking Points
  • A container at 100% CPU: I contain it with <code>docker update --cpus</code>, which edits the live cgroup quota with no restart.
  • I verify with <code>docker inspect</code> NanoCpus, <code>docker stats</code>, and the cgroup <code>cpu.stat</code> throttling counters, then profile the process to find the real cause.
  • I then codify the limit in Compose or the orchestrator and alert on CPU throttling so it never needs a manual fix again.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Using only --cpu-shares to stop a hog Shares are relative weights and do nothing on an otherwise idle host, so the container still takes every free core.
  2. Assuming docker update persists It changes the running container only. Recreating from the Compose file or run command restores the old, unlimited settings.
  3. Reading 100% in docker stats as the whole machine 100% equals one core, so a multi-threaded process can legitimately show several hundred percent. Compare against the number of CPUs.
  4. Capping so tightly that latency suffers CFS throttling pauses all threads for the rest of a period, causing latency spikes in request-driven services.
Advertisement
Want more Docker & Containers scenarios?
Explore our complete collection of scenario-based Docker & Containers interview runbooks.
Browse All Docker & Containers Questions →