# Add Resource Limits to a Docker Compose Stack

Yes. An agent measures each Compose service with docker stats, sets memory and CPU limits and reservations per service, checks the stack still starts and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-resource-limits/docker-compose/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the stack before choosing a number. In a sample shaped like `react-express-mongodb` in docker/awesome-compose, `compose.yaml` defines three services: `frontend`, built from `frontend/Dockerfile` at its `development` target with `3000:3000` published, a bind mount of the source and `restart: always`; `backend`, built from `backend/Dockerfile` at the same target, exposing 3000 on the `react-express` and `express-mongo` networks and depending on `mongo`; and `mongo`, the `mongo:4.2.0` image with a `mongo_data` volume. Both Dockerfiles start from a `node:lts-buster` image, run `npm ci` and end in `npm run dev` or `npm start`. No service has a `deploy` block, so a runaway build or a leaking process can take the whole host down. The only sample in the repository with a limit is `minecraft/compose.yaml`, which sets `deploy.resources.limits.memory: 1.5G`.

Numbers come from measurement, not guesses. The agent runs `docker compose up -d --build` in the sample's directory, waits for the frontend to answer on port 3000, exercises the API with `curl` a few times, and reads `docker stats --no-stream` for each container's memory and CPU. It repeats the reading after a minute so the Node dev servers have finished their first compile, then takes each service's peak, adds headroom, and rounds to a value a person would write.

Each service gets a `deploy.resources` block with `limits` for `memory` and `cpus` and `reservations` for `memory`, in the form `minecraft/compose.yaml` already uses, which `docker compose up` honours. Because a process does not know its cgroup limit, the agent also sets `NODE_OPTIONS=--max-old-space-size` on the two Node services to a value under their memory limit, so V8 collects garbage before the kernel kills the container, and passes `--wiredTigerCacheSizeGB` to `mongod` so its cache is pinned under the `mongo` limit rather than sized from the host's RAM. The `README.md` for the sample gains a short section listing the limits and the commands to re-measure them.

Then it proves the stack still works inside the limits: `docker compose config` to render the file and catch a typo, `docker compose down -v` and `docker compose up -d --build --wait` from clean, `docker inspect` on each container to read `HostConfig.Memory` and `NanoCpus`, a `curl` against the frontend and the API, and a final `docker stats --no-stream` showing each container under its limit. Any service that is killed under the chosen limit gets a higher one and a note in the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "codex",
  "input": "Add resource limits to the Compose stack in react-express-mongodb and open a pull request. Read compose.yaml, backend/Dockerfile, frontend/Dockerfile, backend/package.json, frontend/package.json and README.md in that directory, and minecraft/compose.yaml for the form the repository already uses. Bring the stack up with docker compose up -d --build, wait for port 3000 to answer, call the API with curl a few times, and read docker stats --no-stream twice a minute apart. From the peaks set deploy.resources.limits for memory and cpus and reservations for memory on frontend, backend and mongo, with headroom, rounded to values a person would write. Set NODE_OPTIONS with --max-old-space-size under the memory limit on the two Node services and pass --wiredTigerCacheSizeGB to mongod under the mongo limit. Add a README section with the limits and the commands to re-measure them. Validate with docker compose config, restart from clean with docker compose down -v and up -d --build --wait, read HostConfig.Memory and NanoCpus with docker inspect, call the frontend and API with curl, and take a final docker stats reading. Put the measurements, the inspect output and the final stats table in the pull request. Do not change images, targets, ports or volumes.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that changes `react-express-mongodb/compose.yaml` and `README.md` and nothing else. The description holds two `docker stats` tables, before and after, the `docker inspect` values for each container, a `curl` response from the frontend and the API after the clean restart, and one line per service explaining how its limit was derived from the measured peak.

The repository's only workflow is CodeQL, which does not run the samples, so the pull request's evidence is what the agent recorded. The description lists the exact commands so a reviewer can repeat them on their own machine.

A reviewer checks that the limits leave room for the dev server's first compile, which is the heaviest moment for a Node service with a bind mount, that the Mongo cache value is under its limit, and that the reservations add up to less than the host they run on. Then they decide whether the same limits belong in the sample's production notes, which the pull request does not write.

## Run it on every Compose sample without limits

The repository holds dozens of samples and one of them has limits. Send one `POST /o/{org}/sessions` per sample directory with this prompt and the directory named in `input`, and each run measures that stack on its own, so a Java sample with a JVM heap gets `JAVA_TOOL_OPTIONS` instead of `NODE_OPTIONS`, and a sample with a database gets a cache setting for that database. Each run opens its own pull request with its own measurements.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn finishes, with the `session_url` to read the pull request link from, so a handler on your server can post it where that sample's maintainer reviews.

## Why run it on Rush?

A limit chosen without a measurement is either so high it protects nothing or so low it kills the container on the first compile, and the measurement means running the stack, loading it, and reading `docker stats` with nothing else on the machine. A Rush sandbox starts clean with Docker available, so the numbers are the stack's own, and the commands and their output stay in the run's record for the reviewer to read.

Bringing a three-service stack up from clean twice, with a rebuild each time, ties up a laptop's Docker for the whole job and leaves images and volumes behind. In Rush that happens in the run's own sandbox, and the same request body applies to every Compose file a team maintains, each measured in its own run.

## Frequently asked questions

### Why deploy.resources and not mem_limit?

The repository already uses `deploy.resources.limits` in `minecraft/compose.yaml`, and `docker compose up` applies that block without Swarm. `mem_limit` and `cpus` at the service level also work in Compose, but one form across the repository is easier to read, so the agent follows the existing one.

### Will the limits kill the dev server during its first build?

That is the moment the agent measures: it takes a reading after the stack answers and another a minute later, and sets the limit above the higher of the two with headroom. The clean restart with `--wait` is the check that the containers survive it.

### Why set NODE_OPTIONS and a Mongo cache size as well?

A cgroup limit is enforced by the kernel, which kills the process when it is crossed. V8 and WiredTiger size themselves from what they can see, so telling them their budget makes them collect or evict before that happens. The values are set under the container limit so the two never disagree.

### Are these numbers right for production?

They are right for this sample under the load the agent applied, which the pull request describes. A production service gets its own measurement under its own traffic; the README section the pull request adds gives the commands to repeat the process there.

## More Docker Compose jobs

- [Add Restart Policies to Docker Compose Services](https://getrush.ai/run/add-restart-policies/docker-compose/)
- [Add a Reverse Proxy to a Docker Compose Stack](https://getrush.ai/run/add-reverse-proxy/docker-compose/)
- [Fix "the attribute `version` is obsolete" in a Docker Compose File](https://getrush.ai/run/fix-compose-version-obsolete-warning/docker-compose/)

## The same job on other stacks

- [Add Resource Requests and Limits to a Helm Chart](https://getrush.ai/run/add-resource-limits/helm/)
- [Add Resource Requests and Limits to Kubernetes Manifests from a Linear Issue](https://getrush.ai/run/add-resource-limits/kubernetes/)
- [Add Resource Requests and Limits to a Kustomize App from a Linear Issue](https://getrush.ai/run/add-resource-limits/kustomize/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
