Sub topic 12
Changing Limits on a Running Container
This chapter assumes you can run containers with --memory and --cpus, and that you know what swap is. Sometimes a container's limits turn out wrong after it starts. Recreating it means downtime and lost in-memory state. docker update changes the limits of a running container in place.
What docker update does
docker update rewrites the cgroup settings of an existing container. The container's process keeps running and its PID does not change. Docker also stores the new values in the container's configuration, so docker inspect shows them afterward.
The command accepts the same resource flags you use with docker run, including --memory, --memory-swap, --memory-reservation, --cpus, --cpu-shares and --cpuset-cpus. It does not change non-resource settings such as ports, environment variables or volumes. Those need a new container.
Example: raising limits
Start a container with modest limits, then raise them:
docker run -d --name web --memory 256m --cpus 0.5 nginx:alpine
docker update --memory 512m --memory-swap 512m --cpus 1 webSet --memory and --memory-swap together. The kernel rejects a swap value that is smaller than the memory value. Passing both keeps the pair consistent, and setting them equal disables swap for the container.
Confirm the change took effect:
docker inspect --format 'mem={{.HostConfig.Memory}} nanocpus={{.HostConfig.NanoCpus}}' webMemory is reported in bytes, so 512 MiB appears as 536870912. CPU is reported in billionths of a core, so --cpus 1 appears as 1000000000.
Lowering limits
Raising a limit is safe. Lowering a memory limit below what the container currently uses is riskier. The kernel first tries to reclaim memory by dropping caches and swapping pages out. If it cannot get usage under the new limit, it may invoke the OOM killer. Before you lower a limit, check current usage:
docker stats --no-stream webLeave headroom above the usage you see, since usage fluctuates.
Update flow
The last step matters because docker update changes only the live container.
Limits of docker update
- Changes do not survive recreation. If you remove the container and run it again from your original command or Compose file, the old limits return. Update the source of truth too.
- Compose does not see the change. The next
docker compose upmay detect a configuration difference and recreate the container with the file's values. - Restart policy still applies.
docker update --restartis also supported. This is useful for adjustingunless-stoppedwithout recreating the container. - You can update several containers at once. List multiple names or IDs after the flags.
docker update --cpus 0.5 web api workerCommon errors
| Symptom | Likely cause |
|---|---|
| Error that memory limit should be smaller than swap | You changed `--memory` without a compatible `--memory-swap` |
| Container exits with code 137 after lowering memory | New limit was below real usage, so the OOM killer ran |
| Old limits return after redeploy | Only the live container was updated, not the Compose file |
Exercise
A container named api was started with --memory 256m and you want 1 GiB with no swap. Which command does this, and why does the swap flag appear?
Show answer
docker update --memory 1g --memory-swap 1g api. The swap flag is set equal to the memory flag so the pair stays consistent and swap is disabled. Setting only --memory could conflict with the existing swap value.