The deploy key looks like something that only matters for Swarm. Its resources.limits part is honoured by a normal docker compose up as well, and it is the tidy way to cap a hungry service.
services:
web:
image: nginx:alpine
deploy:
resources:
limits:
cpus: "1.5"
memory: 512M
reservations:
memory: 128M
cpus is the number of cores the service may use in total, so 1.5 means one and a half. memory is a hard cap; a container that goes over it gets killed by the kernel. The reservation is a soft floor that matters when the host is short on memory.
After docker compose up -d, I’d check that it landed with:
docker stats --no-stream
The MEM USAGE / LIMIT column should show 512MiB instead of the host’s total memory.
A memory limit that is too tight shows up as a container that restarts every so often with exit code 137, which is easy to mistake for a bug in the app. Watch it for a day after you add the limit. And if the file also uses the older mem_limit or cpus service attributes, pick one style; recent Compose versions refuse the file when the two give different values.
Further reading: Compose Deploy Specification: resources