# Harden Docker Compose Services with Read-Only Filesystems and Dropped Capabilities

Yes. An agent adds read-only filesystems, dropped capabilities, no-new-privileges and non-root users to your Compose services and proves each still starts.

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

## What the agent does

The agent starts by reading every service definition, because the right hardening differs per container and a blanket change breaks half of them. In a repository like docker/awesome-compose there are 37 `compose.yaml` files, one per sample. Across them, one service sets `user:`, eight define a `healthcheck`, and the only `read_only: true` anywhere is on bind-mounted `nginx.conf` files in the three `nginx-golang` samples, not on a container's root filesystem; no file sets `cap_drop`, `security_opt` or `tmpfs`. Taking `nginx-flask-mysql/compose.yaml` as the worked example: `db` runs `mariadb:10-focal` with a `healthcheck`, a `db-password` secret and a `db-data` volume; `backend` builds `backend/Dockerfile`, whose `builder` stage runs `flask run` as root on `python:3.10-alpine`; `proxy` builds `proxy/Dockerfile`, `nginx:1.13-alpine` with one config file copied in; and the three sit on `backnet` and `frontnet` networks. `react-express-mongodb/compose.yaml` is the other shape: `frontend` and `backend` bind-mount their source directories for development and `mongo:4.2.0` keeps `mongo_data`.

For each service the agent decides what it needs to write and where, then hardens around that. The `proxy` gets `read_only: true` with `tmpfs` for `/var/cache/nginx` and `/var/run`, `cap_drop: [ALL]` with `cap_add: [NET_BIND_SERVICE]` because it binds port 80, and `security_opt: [no-new-privileges:true]`. The `backend` gets `read_only: true` with a `tmpfs` at `/tmp`, `cap_drop: [ALL]`, `no-new-privileges`, and a non-root `user`, which means a one-line `adduser` in `backend/Dockerfile`'s builder stage and a check that `hello.py` opens only `/run/secrets/db-password`, which it does. The `db` keeps a writable root because MariaDB writes under `/var/lib/mysql`, `/run/mysqld` and `/tmp`, so it gets `cap_drop: [ALL]` plus the capabilities the image's entrypoint needs to switch user, and `no-new-privileges`; the agent finds the needed set by starting the container and reading the entrypoint's errors rather than guessing. In the React sample, the development bind mounts mean `read_only` would block hot reload, so those two services get `cap_drop`, `no-new-privileges` and `user` only, and the description says why.

The sandbox has no Docker, so the agent installs it first, `sudo apt-get install -y docker.io`, starts `dockerd`, and uses the `docker-compose` v1 binary from the distribution package, since Compose v2 is not in that package; the files use the `compose.yaml` name and the version-less format, which v1 1.29 reads. For each sample it brings the stack up before any change, records which services pass their health check or answer on their port, applies the hardening to one service, runs `docker-compose up` again, and reads `docker-compose logs` for the permission errors a too-strict setting produces, such as nginx failing to open its PID file, then adds the specific `tmpfs` path and repeats. Each service's settings are therefore the smallest set that leaves it running, and the agent writes the error that justified each exception as a YAML comment beside it.

The proof is the stacks running hardened. For `nginx-flask-mysql` the agent curls `http://localhost:80` through the proxy and shows the Flask response with the four seeded blog rows, then runs `docker inspect` on each container to print `ReadonlyRootfs`, `CapDrop`, `SecurityOpt` and `User`, and quotes that output in the pull request as the evidence that the settings took effect, not only that they were written. It also runs `docker-compose config` on every edited file so a YAML mistake is caught before the pull request opens, and leaves the images, networks, volumes and secrets exactly as they were.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Harden the services in this repository's Compose files. Read every compose.yaml and the Dockerfiles they build first and list which services already set user, read_only, cap_drop, security_opt or a healthcheck. Install Docker in the sandbox, bring each stack up unchanged to record a baseline, then add read_only with the tmpfs paths each process needs, cap_drop ALL with only the capabilities the service requires, security_opt no-new-privileges and a non-root user where the image allows it, one service at a time, re-running the stack after each change and reading the logs to find the smallest setting that works. Leave bind-mounted development services writable and say why. Prove each hardened stack with a request through its entry point and docker inspect output for the security fields, validate every file with docker-compose config, and open a pull request with a per-service table.",
  "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 on its own branch that edits the `compose.yaml` files the prompt covers and the Dockerfiles that needed a non-root user, with a YAML comment beside every exception naming the error that required it. Images, tags, networks, volumes, secrets and the samples' READMEs are unchanged, so the diff is the hardening and nothing else.

The description has a table with one row per service: `read_only` yes or no with its `tmpfs` paths, the capabilities kept, `no-new-privileges`, the user, and the reason for any field left open. Below it are the `curl` output through each stack's entry point and the `docker inspect` fields for each container, taken from the transcript at `GET /o/{org}/sessions/{id}/messages`, plus the `docker-compose config` result for every edited file.

Before merging, a reviewer reads the exceptions column, since each is a judgment about what a process needs to write, and tries one stack locally with `docker compose up` to confirm v2 reads the files the same way. Services that could not be made read-only are listed with the error that stopped it, not silently left out.

## Run it on every terminal hand-off

A developer who just read a container-escape advisory starts this from the terminal with this body and the repository in `resources`, then goes back to the sprint. The run reads the real Compose files and the real Dockerfiles, so the settings fit what each process writes rather than a hardening checklist pasted onto every service.

Send a `request_id`, a UUID, in the body so a retried start returns the run it already began. When the turn ends, `GET /o/{org}/sessions/{id}` names the pull request, and a follow-up through `POST /o/{org}/sessions/{id}/messages`, for example to add `pids_limit` or memory limits next, wakes the parked sandbox with Docker and the built images still in place.

## Why run it on Rush?

Hardening is a loop of break and fix: set `read_only`, watch nginx fail on its PID file, add a `tmpfs`, watch MariaDB fail on a capability, add it back, and so on for every service. Done by hand it takes an afternoon and ends with a `cap_add` nobody remembers the reason for. The agent runs the loop itself, keeps the smallest working set, and writes the error that justified each exception next to it, so the review is of reasons rather than of flags.

On Rush the Docker daemon, the images and the running stacks live in the agent's own sandbox, where restarting a database container twenty times costs nobody else anything, and the `docker inspect` output that proves the settings took effect stays in the transcript. The same body on a repository with one `compose.yaml` or forty does the same per-service work.

## Frequently asked questions

### Will read-only break my database or my dev setup?

The agent does not force it. A database that writes under its data directory, its socket directory and `/tmp` keeps a writable root and gets `cap_drop`, `no-new-privileges` and a volume for its data instead, and a development service that bind-mounts source for hot reload is left writable with the reason in a comment. Every such exception appears in the pull request's table.

### How does it know which capabilities to keep?

By dropping all of them and reading the logs. A proxy binding port 80 fails without `NET_BIND_SERVICE`, a database entrypoint that switches to its own user fails without the capabilities to do so, and each failure names what is missing. The agent adds exactly those back, one at a time, and notes the error beside each `cap_add` entry.

### The sandbox has no Docker. How does it test?

It installs Docker first with `sudo apt-get install -y docker.io`, starts `dockerd`, and uses the `docker-compose` v1 binary from the same package, because Compose v2 is not in it. The files are then also checked with `docker-compose config`. A reviewer's local `docker compose` v2 reads the same version-less `compose.yaml` files, which is the final check the description asks for.

### Does it change the images or add new ones?

No. Image names and tags stay as they are. Where a non-root user is needed and the image has none, the agent adds an `adduser` line and a `USER` instruction to the sample's own Dockerfile rather than swapping the base image, and leaves networks, volumes and secrets untouched.

## More Docker Compose jobs

- [Pin and Upgrade Container Images in Docker Compose](https://getrush.ai/run/pin-image-versions/docker-compose/)
- [Add Smoke Tests to a Docker Compose Stack](https://getrush.ai/run/write-compose-smoke-tests/docker-compose/)
- [Add Compose Profiles for Optional Services](https://getrush.ai/run/add-compose-profiles/docker-compose/)

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

- [Turn a Jira Issue into a Pull Request for an ASP.NET Core App](https://getrush.ai/run/jira-issue-to-pr/aspnet-core/)
- [Turn a Jira Issue into a Pull Request for a Spring Boot App](https://getrush.ai/run/jira-issue-to-pr/spring-boot/)
- [Run a Large Refactor on an Elixir Codebase](https://getrush.ai/run/large-refactor/elixir/)

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