Sub topic 13
Declaring Limits in Docker Compose
This chapter assumes you can read a Compose file, run docker compose up, and already know what --memory and --cpus do with docker run. Here you will put the same limits in a Compose file so they are applied every time the service starts.
Where limits live in the file
Compose reads limits from the deploy.resources section of a service. It has two subsections:
limitssets the maximum the container may use.reservationssets the amount that should be available to the container.
services:
web:
image: nginx:alpine
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
reservations:
memory: 128MStart it and check the result:
docker compose up -d
docker inspect --format 'mem={{.HostConfig.Memory}} nanocpus={{.HostConfig.NanoCpus}}' $(docker compose ps -q web)The output should show mem=268435456 (256 MiB in bytes) and nanocpus=500000000 (half a core).
How the keys map to docker run
| Compose key | docker run flag | Effect |
|---|---|---|
| `limits.cpus` | `--cpus` | CPU quota, enforced as a cap |
| `limits.memory` | `--memory` | Hard memory limit |
| `reservations.memory` | `--memory-reservation` | Soft limit, applied under host memory pressure |
Write cpus as a quoted string, such as "0.50". Memory values take a unit suffix such as b, k, m, or g.
Other keys for the same limits
Older Compose files put these settings directly on the service, as in mem_limit, cpus, mem_reservation, and cpu_shares. The current Compose Specification accepts these service-level keys too, and docker compose applies them. Pick one style per file. If you set the same limit in both places, Compose reports a conflict or one value wins, depending on the key and version, so avoid the overlap.
Settings with no deploy.resources equivalent still go at the service level. This includes cpuset for pinning to cores and memswap_limit for swap:
services:
worker:
image: alpine
command: sleep 3600
cpuset: "0,1"
memswap_limit: 256M
deploy:
resources:
limits:
cpus: "1.0"
memory: 256MHere memswap_limit equals memory, so the container cannot use swap, as with --memory-swap in docker run.
Common errors
- Limits seem ignored. You are probably running an old
docker-composev1 binary, which applieddeployonly in Swarm mode. Usedocker compose(v2), then check withdocker inspect. - Changes have no effect. Editing the file does not change a running container. Run
docker compose up -dagain so Compose recreates the service. Usedocker updateonly for a quick change that the next recreation will overwrite. - Container exits with code 137. The memory limit is too low for the workload. Check
docker inspect --format '{{.State.OOMKilled}}'and raise the limit. - A reservation larger than the limit. Keep
reservations.memorybelowlimits.memory, or the settings contradict each other.
Exercise
A service has deploy.resources.limits.memory: 256M in its Compose file. You run docker update --memory 512m --memory-swap 512m on the container, then later run docker compose up -d after changing nothing in the file. What limit is the container using afterward, and why?
Show answer
Compose compares the file's configuration with the running container and recreates it only if the configuration changed. Because the file did not change, the container is not recreated and keeps the 512 MiB limit set by docker update. If the container is recreated for any reason, such as docker compose up -d --force-recreate, it returns to 256 MiB, because the file is the source of truth.
Exercise
Write the Compose settings for a service that may use at most one CPU core and 512 MiB of memory, with no swap.
Show answer
Use deploy.resources.limits with cpus: "1.0" and memory: 512M, and add memswap_limit: 512M at the service level. Setting the swap limit equal to the memory limit disables swap.