# Add a Dockerfile to a Rocket App

Yes. An agent writes a two-stage Dockerfile for your Rocket app on nightly Rust, a compose file for Postgres, builds the image in its sandbox, and opens a PR.

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

## What the agent does

The agent reads what the binary needs before it writes a line of Dockerfile. In an API laid out like TatriX/realworld-rust-rocket, `Cargo.toml` names the package `realworld` at version 0.6.0 with `rocket` 0.5.0-rc.1 (`default-features = false`, only `json`), `rocket_sync_db_pools` with the `diesel_postgres_pool` feature, `diesel` 1.4.8 with `postgres`, `scrypt`, `jsonwebtoken` 7.2.0 and `dotenv`. The `rust-toolchain` file holds one word, `nightly`, and `src/lib.rs` opens with `#![feature(proc_macro_hygiene, decl_macro)]`, so a stable image cannot build it. `src/config.rs` is where the runtime contract lives: `from_env()` reads `PORT` (default 8000) and `DATABASE_URL` (a panic without it) and merges both into `Config::figment()`, and `AppState::manage()` reads `SECRET_KEY`, falling back to a debug constant only in debug builds and panicking in release. `.env` is committed with a local `DATABASE_URL` and a `SECRET_KEY`, `init.sql` creates the `realworld` role and database, `migrations/` holds six Diesel migrations, and `.travis.yml` applies them with `diesel migration run` after `psql -U postgres -f init.sql`. There is no Dockerfile, no `.dockerignore` and no compose file.

The Dockerfile has two stages. The builder is `rustlang/rust:nightly-slim`: its default toolchain is already nightly, so the `rust-toolchain` file asks rustup for nothing it does not have. It installs `libpq-dev` and `pkg-config` with `apt-get`, because Diesel's `postgres` feature links libpq through `pq-sys`, copies `Cargo.toml` and `Cargo.lock` first and builds a stub `src/main.rs` and `src/lib.rs` so the dependency layer is cached, then copies the real `src/` and `migrations/` and runs `cargo build --release`. The runtime stage is `debian:bookworm-slim` with `libpq5` and `ca-certificates`, a non-root user, and `target/release/realworld` copied into `/usr/local/bin`. It sets `ENV ROCKET_ADDRESS=0.0.0.0`, exposes 8000 and ends with `CMD ["realworld"]`. The address line matters: Rocket binds 127.0.0.1 unless told otherwise, `Config::figment()` reads `ROCKET_ADDRESS` from the environment, and without it the container starts, logs its routes and answers nothing from outside. A `.dockerignore` lists `target`, `.git`, `tests` and `.env`, so the committed secret is never copied into an image layer.

The database comes from a `docker-compose.yml` beside it: a `db` service on `postgres:16` with `POSTGRES_USER`, `POSTGRES_PASSWORD` and `POSTGRES_DB` all set to `realworld`, the same names `init.sql` creates, a `pg_isready` healthcheck, and an `app` service built from the Dockerfile with `DATABASE_URL=postgres://realworld:realworld@db/realworld`, a `SECRET_KEY` read from the host environment, port 8000 published, and `depends_on` waiting for the healthy database. The image carries no Diesel CLI, so migrations keep running the way `.travis.yml` runs them, `diesel migration run` from a checkout against the compose database on its published port; the pull request says so and names the `diesel_migrations` crate's `embed_migrations!` macro as the follow-up that would move them into the binary.

The check is a build and a request. The sandbox has no Docker, so the agent installs it first (`sudo apt-get install -y docker.io`, then starts `dockerd`); `docker compose` v2 is not in that package, so it uses `docker-compose` v1. It runs `docker build`, brings the pair up, applies the migrations, and requests `GET /api/tags`, a route that touches the database and needs no token. One thing it cannot promise in advance: `Cargo.lock` pins 2022 crates, and today's nightly may reject one of them. If `cargo build --release` fails inside the builder, the agent pins `rust-toolchain` to a dated nightly that builds the lockfile and explains the pin in the pull request, rather than upgrading Rocket as a side effect of a packaging change.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add a Dockerfile to this Rocket app. Read Cargo.toml, Cargo.lock, rust-toolchain, src/lib.rs, src/config.rs, .env, init.sql, migrations/ and .travis.yml first. Write a two-stage Dockerfile: a rustlang/rust nightly-slim builder with libpq-dev and pkg-config, a cached dependency layer built from a stub main before the real sources, cargo build --release, then a debian bookworm-slim runtime with libpq5 and ca-certificates, a non-root user, ROCKET_ADDRESS=0.0.0.0, port 8000 exposed and the realworld binary as the command. Add a .dockerignore that leaves out target, .git, tests and .env. Add a docker-compose.yml with a postgres:16 service whose user, password and database match init.sql, a pg_isready healthcheck, and the app service with DATABASE_URL pointing at it and SECRET_KEY taken from the host environment. Install Docker in the sandbox, build the image, start both services, run diesel migration run against the published database port, and request GET /api/tags. If the pinned nightly cannot build Cargo.lock, pin rust-toolchain to a dated nightly that can and say why. Add a Docker section to README.md. Open a pull request that quotes the build output and the curl response.",
  "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

The pull request adds `Dockerfile`, `.dockerignore` and `docker-compose.yml`, adds a Docker section to `README.md` under the existing Getting started steps, and changes nothing under `src/`. If the toolchain had to be pinned, `rust-toolchain` changes too, with the compiler error that forced it quoted in the description. The description quotes the tail of the `docker build` log, the `docker-compose up` lines where the database turns healthy and Rocket lists its mounted routes, the `diesel migration run` output, and the `/api/tags` response.

A reviewer checks four things before merging: that the builder and runtime tags are the ones they want to track (nightly-slim moves daily, and the pull request says so), that `.env` is in `.dockerignore` and no `SECRET_KEY` appears in any layer, that `ROCKET_ADDRESS` is set in the image rather than left to the compose file, and that the runtime stage carries `libpq5`, without which the binary fails to load. The compose file is for local and staging use; whoever deploys it elsewhere still supplies `DATABASE_URL` and `SECRET_KEY`.

Nothing about the API changes. The same `cargo test` suite and the Postman collection under `tests/` run against the container exactly as they run against `cargo run`, with `apiUrl` pointing at port 8000.

## Run it on every service repository without a Dockerfile

A handler on the reader's own server lists the organisation's repositories, skips the ones that already have a `Dockerfile`, and sends this body once per remaining repository to `POST /o/{org}/sessions`, naming the repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run it already started rather than a second one. Each run reads its own crate's manifest and config, so a Rocket service that reads `ROCKET_PORT` instead of `PORT`, or links SQLite instead of Postgres, gets a different Dockerfile from the same prompt.

After the file exists, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` reads each pull request that changes `Cargo.toml`. An automation's run is read-only, so when a new crate needs a system library the image lacks, it leaves a review comment naming the package to add to the builder and runtime stages instead of pushing the change.

## Why run it on Rush?

A Dockerfile is only right once it has been built, and building this one means a nightly toolchain, libpq headers, a release compile of a Rocket app and a Postgres container to point it at. The agent does all of that in its own sandbox, installs Docker there, and keeps the build log, the compose log and the curl response with the pull request, so the reviewer reads evidence rather than reproducing the build on a laptop that may have a different nightly.

The same request runs across many services at once, each in its own sandbox, each reading its own manifest. A team with a dozen Rust services gets a dozen pull requests in parallel, each with the base image, the system packages and the environment variables that one service needs, and each with proof that its image starts and answers a request.

## Frequently asked questions

### Why nightly, and why rustlang/rust rather than the official rust image?

The `rust-toolchain` file pins nightly and `src/lib.rs` uses two unstable features, so the crate does not compile on stable. The official `rust` image ships stable toolchains; `rustlang/rust` is the nightly build of the same image, with `nightly-slim` and `nightly-bookworm-slim` tags. The pull request names the tag it built with and notes that nightly tags move every day.

### Does the image run the database migrations?

No. The binary does not embed them and the image carries no Diesel CLI, so `diesel migration run` keeps running from a checkout against the compose database, the way `.travis.yml` already does it. The pull request names the `diesel_migrations` crate as the way to move them into the binary if the team wants a self-migrating container.

### Is the committed .env a problem?

It is excluded by `.dockerignore`, so it never enters an image layer, and the compose file passes `DATABASE_URL` and `SECRET_KEY` explicitly instead. `dotenv().ok()` in `src/lib.rs` still loads a `.env` when one is present on disk, which is unchanged for local `cargo run`.

### Why does the container need ROCKET_ADDRESS?

Rocket's default address is 127.0.0.1, which inside a container is reachable only from the container itself. `src/config.rs` builds its config from `Config::figment()`, which reads `ROCKET_*` environment variables, so `ROCKET_ADDRESS=0.0.0.0` in the Dockerfile makes the published port work without a code change.

### What if today's nightly cannot build the 2022 lockfile?

The agent finds out in the builder stage, not after merging. It pins `rust-toolchain` to a dated nightly that builds `Cargo.lock` unchanged, quotes the compiler error in the pull request, and leaves upgrading Rocket to a separate change.

## More Rocket jobs

- [Add GitHub Actions CI to a Rocket App](https://getrush.ai/run/add-github-actions-ci/rocket/)
- [Add Health Check Endpoints to a Rocket App](https://getrush.ai/run/add-health-checks/rocket/)
- [Add OpenAPI Docs to a Rocket API](https://getrush.ai/run/add-openapi-docs/rocket/)

## The same job on other stacks

- [Add a Dockerfile to a Sequelize App](https://getrush.ai/run/add-dockerfile/sequelize/)
- [Write a Dockerfile for a Bolt for JavaScript Slack App](https://getrush.ai/run/add-dockerfile/slack-bolt/)
- [Add a Dockerfile to a Slim 4 App](https://getrush.ai/run/add-dockerfile/slim/)

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