# Run Docker Compose Services as a Non-Root User

Yes. An agent changes your Dockerfiles and compose.yaml so each service runs as an unprivileged user, starts the stack in its sandbox to prove it, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-non-root-user/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 it changes a user. In a project like the `react-express-mongodb` sample in docker/awesome-compose, `compose.yaml` defines three services: `frontend`, built from `frontend/` at the `development` target with port `3000:3000` and a bind mount of `./frontend` to `/usr/src/app` plus an anonymous volume over `/usr/src/app/node_modules`; `backend`, built the same way from `backend/` with `expose: 3000` and the same pair of volumes; and `mongo`, the `mongo:4.2.0` image with a named `mongo_data` volume. Two networks, `react-express` and `express-mongo`, keep the browser-facing service away from the database.

Both Dockerfiles start `FROM` an official Node image, `node:lts-buster-slim` for the backend and `node:lts-buster` for the frontend, set `WORKDIR /usr/src/app`, copy `package.json`, `package-lock.json`, `.npmrc` and `.yarnrc.yml`, run `npm ci`, copy the rest, and end in `CMD ["npm", "run", "dev"]` or `CMD ["npm", "start"]` with no `USER` instruction, so `nodemon` and `react-scripts` run as root inside the container. Each file also has a second stage, `dev-envs`, that creates a `vscode` user with a heredoc `RUN` and copies Docker tooling in; it is not the target compose builds. `backend/package.json` runs `nodemon server.js` in development, and the Express app in `server.js` listens on `3000`, a port that needs no privilege.

The official Node images ship a `node` user with uid and gid `1000`, so the agent uses it rather than adding one. In each Dockerfile's `development` stage it creates the working directory owned by that user before `WORKDIR`, copies with `COPY --chown=node:node`, moves `USER node` above `npm ci` so the installed `node_modules` are owned by the user that will run them, and leaves `EXPOSE 3000` and the `CMD` alone. In `compose.yaml` it sets `user: node` on `frontend` and `backend` so the choice is visible without opening the Dockerfile, and documents the one dev-time wrinkle: the bind mount hands the container the host's file ownership, so on a Linux host whose developer is not uid `1000` the agent shows how to pass `UID` and `GID` as build arguments. For `mongo` it changes nothing and says why: the image's entrypoint drops to its own `mongodb` user after fixing the data directory's ownership, and setting `user:` on that service would skip the fix on a fresh `mongo_data` volume.

To prove it, the agent installs Docker in its sandbox with `sudo apt-get install -y docker.io`, starts `dockerd`, and builds both images with `DOCKER_BUILDKIT=1`, which the `# syntax=docker/dockerfile:1.4` line and the heredoc in the `dev-envs` stage require even though that stage is not built. It brings the stack up with `docker-compose up -d`, the v1 binary that package provides, runs `docker-compose exec backend id` and the same for `frontend` to show `uid=1000(node)`, requests `http://localhost:3000` from the frontend and the backend's route through the proxy `frontend/package.json` sets to `http://backend:3000`, and checks that `nodemon` still restarts when a file under the bind mount changes. It then opens the pull request with those outputs and a note on the `UID` and `GID` build arguments.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Make every service in this Docker Compose project run as a non-root user and open a pull request. Read compose.yaml, every Dockerfile, each service's package.json and the README first. For the Node services use the node user the official images already have: create and chown the working directory before WORKDIR, copy with --chown, put USER node before npm ci, and set user: node on those services in compose.yaml. Explain the bind-mount ownership issue for developers whose uid is not 1000 and add UID and GID build arguments for it. Leave the database image's own privilege drop in place and say why. The sandbox has no Docker: install docker.io, start dockerd, build with DOCKER_BUILDKIT=1, bring the stack up with docker-compose, show the uid each service runs as with docker-compose exec, and request the frontend and backend over HTTP. Quote those outputs in the pull request.",
  "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 changes `backend/Dockerfile` and `frontend/Dockerfile` to own the working directory, copy with `--chown=node:node` and switch to `USER node` before `npm ci`, changes `compose.yaml` to set `user: node` on the two Node services and pass `UID` and `GID` build arguments with defaults of `1000`, and adds a short section to the README on when to override them. The `mongo` service and the `dev-envs` stages are unchanged, and the description says why.

The evidence is the sandbox session quoted in the description and kept in the run's record: the two `docker build` outputs, `docker-compose up -d`, `docker-compose exec backend id` and `docker-compose exec frontend id` each showing `uid=1000(node) gid=1000(node)`, the HTTP responses from port `3000`, and the `nodemon` restart line after a file change. Any workflow the repository runs on pull requests, such as the repository's own CodeQL analysis, runs here too.

Before merging, a reviewer checks that the anonymous `node_modules` volume is created owned by `node` on a fresh `docker-compose up`, that developers on Linux with a different uid know to pass `UID` and `GID`, that nothing in `backend/logs` or another writable path still expects root, and that production images built from these files carry the same `USER` line. Then they rebuild locally once with `--no-cache` and watch the stack start.

## Run it on every compose project that still runs as root

A team with several Compose projects sends one `POST /o/{org}/sessions` per repository from the terminal or a script, with this prompt and the repository in `resources`. `GET /o/{org}/github` lists every repository the Rush GitHub App installation can reach, so a script can clone each, grep its Dockerfiles for a `USER` line and start a run only where none is found. Each run installs its own Docker, reads its own base images and opens its own pull request.

Set a webhook URL on the API key and Rush POSTs `run.completed` or `run.failed` when each run's turn finishes, with `session_url` so your server can collect the pull request links. Send a fresh UUID as `request_id` with each request so a retry returns the run already started.

## Why run it on Rush?

Dropping root in a Compose stack is a five-line change that breaks in ways you only see at runtime: `npm ci` writing to a directory the new user cannot read, a bind mount whose files belong to someone else, a database entrypoint that needs to start as root to fix its own volume. On Rush the agent installs Docker in its own sandbox, builds the images, brings the stack up and shows the uid each process runs as, so the pull request shows the stack running rather than asserting it would. Your own Docker daemon and your containers stay as they are.

The record keeps every build and every `exec`, the pull request names the one case developers must handle by hand, and nothing merges without a person. The same request can go to every Compose project the team keeps, each in its own sandbox with its own daemon, one pull request each.

## Frequently asked questions

### Why reuse the node user instead of creating one?

Every official Node image already has `node` at uid and gid `1000`, which is also the first user on most Linux desktops and matches the bind mount in development. Creating another user adds a `RUN` layer and a second uid to think about. The agent only adds a user when the base image has none, as it would for a `python` or `golang` image.

### Will the bind mounts still work?

Yes when the developer's uid is `1000`, which is the common case on Linux and irrelevant on Docker Desktop for macOS and Windows, where file sharing maps ownership. For a Linux developer with another uid, the `UID` and `GID` build arguments the agent adds rebuild the image with a matching user, and the README section shows the one-line `docker-compose build` invocation.

### What about the MongoDB service?

The agent leaves it alone and explains why in the pull request. The `mongo` image starts as root only to set the data directory's ownership on a fresh volume and then runs `mongod` as its own `mongodb` user. Forcing `user:` on the service would skip that step and fail on the first start with an empty `mongo_data` volume.

### How does the agent run Docker in the sandbox?

The sandbox has no Docker to begin with. The agent installs the `docker.io` package, starts `dockerd`, and uses the `docker-compose` v1 binary that package provides, with `DOCKER_BUILDKIT=1` so the `# syntax` directive and heredoc in the Dockerfiles parse. Every command and its output are in the run's record.

## More Docker Compose jobs

- [Add a Production Override to a Docker Compose Project](https://getrush.ai/run/add-production-compose-override/docker-compose/)
- [Add Renovate to Keep Docker Compose Image Tags Current](https://getrush.ai/run/add-renovate/docker-compose/)
- [Add Resource Limits to a Docker Compose Stack](https://getrush.ai/run/add-resource-limits/docker-compose/)

## The same job on other stacks

- [Run an Echo App as a Non-Root User in Docker](https://getrush.ai/run/add-non-root-user/echo/)
- [Run a Fiber Docker Image as a Non-Root User](https://getrush.ai/run/add-non-root-user/fiber/)
- [Run Your Docker Images as a Non-Root User](https://getrush.ai/run/add-non-root-user/docker/)

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