Difficulty: Advanced
Can you change the CPU or memory limits of a container while it is running, and what are the limits of doing so?
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.
docker update only covers settings that map to cgroups plus the restart policy:
--cpus, --cpu-shares, --cpu-period, --cpu-quota, --cpuset-cpus, --cpuset-mems--memory, --memory-swap, --memory-reservation--pids-limit, --blkio-weight, --restartEverything 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.
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.
| Change | What happens |
|---|---|
Raise CPU (--cpus 1 to --cpus 2) | Throttling eases immediately; latency often drops within seconds |
| Raise memory | Process can allocate more before hitting the OOM killer |
| Lower memory below current usage | Kernel tries to reclaim memory; the update can fail, or the app can be OOM-killed |
Set --memory alone when swap was set | May error because --memory-swap must be greater than or equal to --memory; set both together |
--cpu-shares | Only 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.
docker stats, adjust, and observe before writing the final numbers into Compose.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.InPlacePodVerticalScaling feature), which graduated to beta in v1.33, so on recent clusters you can resize without recreating the pod.# 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.
docker update, cgroups, --memory and --memory-swap, --cpus, --restart policy, Live resource limits