Updating Limits

Difficulty: Advanced

Question

Can you change the CPU or memory limits of a container while it is running, and what are the limits of doing so?

Answer

Yes. docker update changes resource limits on a running (or stopped) container without restarting it, for example docker update --cpus 2 --memory 1g --memory-swap 1g api. Docker rewrites the container's cgroup settings in place, so the new limits apply immediately to the running processes and are saved in the container's config for future restarts.

What you can and cannot change

docker update only covers settings that map to cgroups plus the restart policy:

  • CPU: --cpus, --cpu-shares, --cpu-period, --cpu-quota, --cpuset-cpus, --cpuset-mems
  • Memory: --memory, --memory-swap, --memory-reservation
  • Other: --pids-limit, --blkio-weight, --restart

Everything else is fixed at creation: published ports, environment variables, volumes, networks, image, --shm-size, user and capabilities. Changing those means removing and recreating the container.

Why it works without a restart

Container limits are just values in the kernel's cgroup files (for example cpu.max and memory.max on cgroup v2). The kernel reads them on every scheduling decision and memory charge, so writing a new value takes effect at once. The process is never told; it simply gets more or less CPU time, or a different memory ceiling.

Behaviour worth knowing

ChangeWhat happens
Raise CPU (--cpus 1 to --cpus 2)Throttling eases immediately; latency often drops within seconds
Raise memoryProcess can allocate more before hitting the OOM killer
Lower memory below current usageKernel tries to reclaim memory; the update can fail, or the app can be OOM-killed
Set --memory alone when swap was setMay error because --memory-swap must be greater than or equal to --memory; set both together
--cpu-sharesOnly a relative weight under contention, not a hard cap; --cpus is the hard cap

Runtimes that size themselves at startup do not notice the change. A JVM picks its default max heap from the memory limit when it boots, and Node's V8 heap limit is fixed at launch, so raising the container's memory does not give the heap more room until a restart.

When to use it, and when not to

  • Firefighting: a reporting container pegged at its CPU quota during month-end can be given more CPU without dropping in-flight requests.
  • Tuning experiments: watch docker stats, adjust, and observe before writing the final numbers into Compose.
  • Not as a deployment method: the next docker compose up that recreates the container, or a fresh docker run, uses the original config. Always copy the change into compose.yaml or your infrastructure code.
  • Kubernetes: the equivalent is editing pod resources. Kubernetes added in-place pod resize (the InPlacePodVerticalScaling feature), which graduated to beta in v1.33, so on recent clusters you can resize without recreating the pod.
Hot-updating a container's limits. docker update edits cgroup values that the kernel enforces immediately.
  1. Spot pressure with docker stats: CPU pinned at quota or memory near limit.
  2. Run docker update: For example --cpus 2 --memory 1g --memory-swap 1g.
  3. Engine writes cgroup files: cpu.max and memory.max change in place.
  4. Kernel enforces new limits: Running processes are not restarted.
  5. Persist in Compose or IaC: Otherwise a recreate reverts the change.

Code examples

Raise limits on a live container and verify

# Current limits (NanoCpus: 1e9 = 1 CPU, Memory in bytes)
docker inspect api --format 'cpus={{.HostConfig.NanoCpus}} mem={{.HostConfig.Memory}}'

# Give it 2 CPUs and 1 GiB RAM (no swap beyond RAM), no restart needed
docker update --cpus 2 --memory 1g --memory-swap 1g api

# Change the restart policy too
docker update --restart unless-stopped api

# Confirm the new values and watch usage
docker inspect api --format 'cpus={{.HostConfig.NanoCpus}} mem={{.HostConfig.Memory}}'
docker stats --no-stream api

docker update prints the container name on success. The inspect output shows the limits doubled while the container kept running.

Key points

  • docker update changes CPU, memory, pids, blkio and restart policy on running or stopped containers
  • It works by rewriting cgroup values, so changes apply instantly without a restart
  • Ports, env vars, volumes, image and shm-size cannot be updated; recreate instead
  • Set --memory and --memory-swap together, and be careful lowering memory below current usage
  • Recreating the container (for example docker compose up) reverts to the original config unless you persist the change

Common follow-up questions

  • What is the difference between --cpus and --cpu-shares?
  • Why does a Java app not benefit from a memory increase until it restarts?
  • How would you resize resources for a pod in Kubernetes?
  • What happens inside the kernel when a container exceeds its memory limit?

Interview tips

  • Say clearly which settings are updatable and which are not; that boundary is what the interviewer is testing
  • Explain the cgroup mechanism in one sentence to show you know why no restart is needed
  • Stress persisting the change in Compose or IaC so it survives the next deploy

Concepts covered

docker update, cgroups, --memory and --memory-swap, --cpus, --restart policy, Live resource limits

More Docker & Containers questions