# Add Log Rotation to a Docker Compose Stack

Yes. An agent adds a shared json-file logging block with size and file limits to every service in your Compose file, verifies it with docker compose, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-log-rotation/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 Compose file before it changes it, because log rotation has to reach every service that writes to stdout, including the ones that are only pulled, not built. In a stack like the `nginx-golang-postgres` sample in docker/awesome-compose, `compose.yaml` defines three services: `backend`, built from `backend/Dockerfile` at the `builder` target, which starts a Go server on port 8000 behind `gorilla/handlers.LoggingHandler` so every request is one line on stdout; `db`, the `postgres` image with `restart: always`, a `db-data` volume and a `pg_isready` healthcheck; and `proxy`, the `nginx` image with `proxy/nginx.conf` bind-mounted read-only and port 80 published. None of the three has a `logging` key, so each container's `json-file` log grows without bound on the host.

The change is one anchor and three references. The agent adds a top-level `x-logging` extension field with `driver: json-file` and `options` of `max-size` and `max-file`, and puts `logging: *default-logging` on each of the three services, so the limits live in one place and a fourth service added later copies one line. It chooses sizes from how much each service writes: the nginx proxy and the request-logging backend get the same cap, and Postgres, which logs little at its default level, shares it rather than getting a special case. The agent does not switch drivers, because the `docker compose logs` command the README relies on reads `json-file` and would go quiet under `syslog` or `none`.

The sandbox has no Docker, so the agent installs it first with `sudo apt-get install -y docker.io` and starts `dockerd`; the `docker.io` package carries `docker-compose` v1, and the agent uses it to run `docker-compose config` and check that the anchor expands into each service's `logging` block, then `docker-compose up -d` to build the backend and start all three. It confirms the limits took effect with `docker inspect --format '{{json .HostConfig.LogConfig}}'` on each container, drives a few hundred requests through `curl localhost:80` so the backend and proxy write lines, and checks with `ls -l` under `/var/lib/docker/containers/<id>/` that a new `-json.log.1` file appears once the first passes `max-size`. Then `docker-compose down`.

Finally the agent adds a short note to the sample's `README.md` under the compose excerpt saying what the caps are and how to raise them, and pushes one commit for the Compose change and one for the README. It touches no Dockerfile, no `nginx.conf` and no Go code.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add log rotation to this Docker Compose stack and open a pull request. Read compose.yaml, README.md, backend/Dockerfile, backend/main.go and proxy/nginx.conf first and say which services write to stdout. Add a top-level x-logging anchor with the json-file driver and max-size and max-file options, and reference it with logging on every service, keeping the driver as json-file so docker compose logs still works. Install Docker in the sandbox (sudo apt-get install -y docker.io, start dockerd), run docker-compose config to show the expanded logging blocks, bring the stack up, check each container's HostConfig.LogConfig with docker inspect, send enough requests through the proxy for the backend log to roll over to a .1 file, and paste the outputs in the pull request. Add a README note on the limits. Do not change any Dockerfile, nginx config or application code.",
  "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 with a diff of a few lines in `compose.yaml`: the `x-logging` anchor at the top and a `logging` reference on `backend`, `db` and `proxy`, plus a paragraph in `README.md`. The description shows the `docker-compose config` output with the expanded blocks, the `LogConfig` of each running container, and the directory listing where the backend's log rolled over into a numbered file after the request loop.

A reviewer checks that all three services reference the anchor, that the driver is still `json-file`, that `max-file` is large enough to keep the history they rely on when they debug, and that nothing else in the file moved. On a host that already sets `log-opts` in `/etc/docker/daemon.json`, the per-service block wins, which the README note says, so they decide whether to keep the daemon default or the Compose setting. Then they merge and the limits apply on the next `docker compose up`.

## Run it on every Compose stack without log limits

A team with many Compose-deployed services starts one run per repository with `POST /o/{org}/sessions`, this prompt and the repository in `resources`. The agent reads each stack's own services and picks the caps from what they write, so a stack with a chatty worker gets a different size from one with a quiet database, and each run opens its own pull request.

The key's `webhook_url` receives `run.completed` for each run it starts, with a `session_url` to read the pull request link. Send a `request_id`, a UUID, with each start so a retried call returns the run it already started instead of opening a second pull request.

## Why run it on Rush?

Unbounded container logs fail slowly: nothing is wrong until a host runs out of disk months later, and the fix is a few lines that are easy to put on two services and forget on the third. In a Rush sandbox the agent installs Docker, starts the real stack, reads back `LogConfig` from each container and watches the log file roll over, and all of that output stays in the transcript, so the pull request proves the rotation happens rather than asserting that the YAML looks right.

The job is also one to hand off from the terminal rather than do by hand across every repository: start it, close the laptop, and come back to a pull request whose description already holds the evidence. The same request runs across every Compose stack the team owns, each in its own sandbox, in parallel.

## Frequently asked questions

### Why keep the json-file driver instead of switching to journald or syslog?

Because `docker compose logs` and `docker logs` read the `json-file` and `local` drivers, and the sample's README tells people to use them. Switching drivers would make those commands return nothing. The `json-file` driver supports `max-size` and `max-file` directly, which is all rotation needs.

### Does this apply to containers that are already running?

No. The log driver and its options are fixed when a container is created, so the limits take effect the next time Compose recreates the services, for example on `docker compose up -d` after the merge. The agent's README note says so.

### What about a daemon-wide setting in daemon.json?

A `log-opts` block in `/etc/docker/daemon.json` sets the default for every container on that host, and a `logging` block in the Compose file overrides it per service. The pull request keeps the per-service form so the repository is correct on any host, and the reviewer can remove it later if they prefer one daemon-wide setting.

### Why does the agent install Docker in the sandbox?

A Rush sandbox starts without Docker. The agent installs the `docker.io` package and starts `dockerd`, which gives it `docker` and the v1 `docker-compose` command; `docker compose` v2 is not part of that package. It uses those to expand the file, start the stack and inspect the containers, so the check is against a real daemon rather than a YAML linter.

## More Docker Compose jobs

- [Convert a Compose Project's Dockerfiles to Multi-stage Builds](https://getrush.ai/run/add-multi-stage-builds/docker-compose/)
- [Run Docker Compose Services as a Non-Root User](https://getrush.ai/run/add-non-root-user/docker-compose/)
- [Add a Production Override to a Docker Compose Project](https://getrush.ai/run/add-production-compose-override/docker-compose/)

## The same job on other stacks

- [Add Log Rotation to a Winston Logger](https://getrush.ai/run/add-log-rotation/winston/)

## More jobs like this: Hand off a long task

- [Add Logging to a Tauri App with tauri-plugin-log](https://getrush.ai/run/add-logging/tauri/)
- [Add Magic Link Sign-In to a Supabase App](https://getrush.ai/run/add-magic-link-auth/supabase/)

## 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/)
