# Move Hard-Coded Values in a Compose File into a .env File

Yes. An agent moves the ports, image tags and database names hard-coded in your Compose file into a .env with defaults, checks the rendered config and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-env-file/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 and everything that depends on its literals. In a project shaped like the react-express-mongodb sample in docker/awesome-compose, compose.yaml defines `frontend` with `ports: 3000:3000`, `backend` with `expose: 3000`, and `mongo` pinned to `image: mongo:4.2.0` with a `mongo_data` volume, and no `.env`, `env_file` or `${VAR}` anywhere in the file. The backend's connection string is not in Compose at all: backend/config/config.json holds `MONGODB_URI` as `mongodb://mongo:27017/TodoApp` for development and a `TodoAppTest` database for test, loaded into `process.env` by backend/config/config.js, and the frontend's package.json carries `"proxy": "http://backend:3000"`, which is how the browser's requests reach the API. The repository's own convention for this is in the postgresql-pgadmin sample, whose compose.yaml reads `${POSTGRES_USER}`, `${POSTGRES_PW}` and `${PGADMIN_MAIL}` from a committed `.env` and whose README lists each variable under a Configuration heading.

It then decides what belongs in the file and what does not. Values a developer changes between machines move: the host port the frontend publishes, the Mongo image tag, the database name, and the backend port. Each becomes `${NAME:-default}` in compose.yaml so the file still works with no `.env` present, and the `.env` lists the same defaults with a one-line comment each. Values that are wiring between containers stay literal: the service names, the `expose` on the backend that is only reachable on the Compose network, and the `depends_on` order. The backend's `MONGODB_URI` becomes an `environment` entry on the service built from `${MONGO_DB}` so the database name lives in one place, and config.js is left to keep its JSON for the test environment, which does not run under Compose.

Secrets are kept out on purpose. The sample has none, but the agent writes a `.env.example` with the defaults and adds `.env` to .gitignore only when the file would ever carry a credential, which it notes it does not here, so the committed `.env` stays, matching postgresql-pgadmin. It then adds a Configuration section to the sample's README.md in the same shape as that sample's, listing each variable, its default and what changes when it is set.

The check is `docker compose config`, which renders the file with interpolation applied, run twice: once with the `.env` present and once with it moved aside, diffing the two to show the defaults produce the same rendered configuration, and a third time with `FRONTEND_PORT=3001` exported to show the port mapping follows. The agent then runs `docker compose up -d`, waits for the backend to log that Mongo connected, which backend/db/index.js prints as `MongoDB is connected` after its retry loop, requests the frontend on the published port, and runs `docker compose down -v`. The repository has no test for its samples beyond CodeQL, so this is the proof.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Move the hard-coded values in this project's compose.yaml into a .env file. Read compose.yaml, the backend's Dockerfile, package.json, server.js, db/index.js and config/config.js and config/config.json, the frontend's Dockerfile and package.json, README.md, and any other sample in the repository that already uses ${VAR} interpolation with a .env so you follow its conventions. Move the values a developer changes between machines into variables: the published frontend port, the Mongo image tag, the database name and the backend port, each written as ${NAME:-default} in compose.yaml so the file still works without a .env. Leave service names, expose entries and depends_on literal. Set the backend's MONGODB_URI as an environment entry built from the database variable. Write .env with the defaults and a comment per line, and a .env.example; only add .env to .gitignore if it would hold a credential, and say whether it would. Add a Configuration section to README.md listing each variable. Run docker compose config with and without the .env and diff the results, and once more with one variable overridden; then docker compose up -d, wait for the backend to log that Mongo connected, request the frontend, and docker compose down -v. Open a pull request with the diffs and the log lines.",
  "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 four or five lines in compose.yaml to `${NAME:-default}` form and adds one `environment` entry on the backend, adds `.env` and `.env.example` with the same defaults, and adds a Configuration section to the sample's README.md in the shape the postgresql-pgadmin README uses. The Dockerfiles, server.js and the frontend's `proxy` setting do not change, and the description says so, because a reviewer wants to know the containers themselves are untouched.

The description quotes the `docker compose config` diff between the `.env` present and absent, which is empty, and the diff with one variable overridden, which is one line; then the backend's `MongoDB is connected` log line and the HTTP status from the frontend, followed by the clean `down -v`. It also states in one line that no value in the file is a credential and why `.env` stays committed.

Before merging, a reviewer checks that the default values match what the README's run instructions promise, decides whether the backend port should be a variable at all given the frontend's `proxy` names it, and whether other samples in the repository should get the same treatment. A follow-up on the same session can apply the pattern to a second sample.

## Run it on every sample or service you maintain

This is a tidy-up you want done the same way across many Compose projects: a `POST /o/{org}/sessions` with this prompt and one repository in `resources` for each, sent from the terminal, one run per repository. Each run reads that project's own literals and conventions and opens its own pull request. The sandbox parks with its disk when the turn ends, so a follow-up such as `do the same for the react-express-mysql sample` wakes it with the images already built.

With a webhook URL on the API key, Rush posts `run.completed` or `run.failed` with the `session_url` for each run, so the links collect in one place as the runs finish instead of in a dozen tabs.

## Why run it on Rush?

Interpolation mistakes are silent: a misspelt variable renders as an empty string and the stack still starts, with the wrong port or no database name. The agent renders the file with `docker compose config` in its own sandbox, diffs the rendered output with and without the `.env`, and then starts the stack and reads the backend's own connection log, with every command and its output in the run's record.

The record also shows what was deliberately left alone. The transcript has the agent reading the frontend's `proxy` setting and the backend's config.js, deciding which literals are wiring and which are configuration, and following the repository's existing postgresql-pgadmin convention rather than inventing one. A reviewer who wants `.env` gitignored anyway can say so in a follow-up and the parked sandbox makes the change with the checks already run.

## Frequently asked questions

### Will the file still work without a .env?

Yes. Every variable is written as ${NAME:-default}, so Compose substitutes the default when the variable is unset and the .env is absent. The pull request shows docker compose config rendering identically with and without the file.

### Why keep .env committed?

Because in this project the file holds a port, an image tag and a database name, none of them secret, and the repository's own postgresql-pgadmin sample commits its .env the same way. The agent only adds .env to .gitignore when a value would be a credential, and says so either way.

### What stays hard-coded?

Service names, the backend's expose entry and depends_on. Those are wiring between containers on the Compose network and changing them per machine makes no sense, so turning them into variables would add indirection without a use.

### Does the backend's config.json change?

No. config.js only loads that file for the development and test environments, and the test environment does not run under Compose. The Compose service gets MONGODB_URI as an environment entry built from the database variable, so under Compose the database name has one source.

### How is the change verified?

With docker compose config to render the interpolated file three ways and diff them, then docker compose up -d, the backend's MongoDB is connected log line, a request to the frontend on the published port, and docker compose down -v to remove the volume. All of it is in the pull request and the run's record.

## More Docker Compose jobs

- [Add Hadolint to CI for a Docker Compose Repository](https://getrush.ai/run/add-hadolint-to-ci/docker-compose/)
- [Add Health Checks to a Docker Compose Setup](https://getrush.ai/run/add-health-checks/docker-compose/)
- [Add Log Rotation to a Docker Compose Stack](https://getrush.ai/run/add-log-rotation/docker-compose/)

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

- [Read Typer CLI Options From Environment Variables](https://getrush.ai/run/add-env-var-options/typer/)
- [Add Error Boundaries to a React Router App](https://getrush.ai/run/add-error-boundaries/react-router/)
- [Add Error Boundaries to a Vue App](https://getrush.ai/run/add-error-boundaries/vue/)

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