brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: deploy.resources.limits caps memory and CPU in plain Docker Compose too

Docker
~/tips/tip-docker-compose-deploy-resource-limits

services:

Set memory and CPU limits per service in the Compose file, no Swarm needed.

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

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link