# Dockerize an Actix Web App

Yes. An agent writes a multi-stage Dockerfile for an Actix Web crate that builds with SQLx offline metadata, ships its templates and static files, and opens a PR.

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

## What the agent does

The agent reads the crate and the workspace it sits in. In a service like `basics/todo` in actix/examples, the crate's `Cargo.toml` inherits `edition` 2024 and `rust-version` 1.98 from `[workspace.package]` and takes `actix-web` 4.11, `actix-files`, `actix-session` with `cookie-session`, `env_logger`, `dotenvor` and `serde` from `[workspace.dependencies]`, plus its own `sqlx` 0.9 with `runtime-tokio`, `tls-rustls` and `sqlite`, and `tera` 2. `src/main.rs` loads `.env`, reads `DATABASE_URL`, opens a `SqlitePool`, loads templates with `Tera::load_from_glob("templates/**/*")`, serves `/static` from `./static` with `Files::new`, and binds `127.0.0.1:8080` with two workers. `.env` sets `DATABASE_URL=sqlite://${CARGO_MANIFEST_DIR}/todo.db`, `SQLX_OFFLINE=true` and `RUST_LOG=info`; `src/model.rs` uses `sqlx::query_as!` and `query!`, so a build needs either a live database or the offline metadata, which lives at the workspace root in `.sqlx`. There is one migration under `migrations/`, nothing in the crate runs it, and the workspace `.github/workflows/ci.yml` creates `todo.db` with `sqlx database create` and `sqlx migrate run --source=./basics/todo/migrations` before `cargo test`. The sibling `docker/` crate has a `Dockerfile` that builds on `rust:1-slim-bookworm`, runs on `bitnami/minideb:bookworm` as `USER 1001` and `EXPOSE 8080`, and its `Cargo.toml` says in a comment that workspace dependencies do not work in a Docker context because the build copies only that directory. That sentence is why `basics/todo` cannot copy that Dockerfile.

So the agent writes `basics/todo/Dockerfile` with the build context set to the workspace root, which the pull request's README section and a `.dockerignore` at the root make explicit. Cargo reads every member listed in the workspace manifest, so the build stage copies the whole workspace rather than one crate: the root `Cargo.toml`, `Cargo.lock`, `.sqlx` and every member directory, with `.dockerignore` excluding `target` directories and `.git` so the context is source only. It uses the same `rust:1-slim-bookworm` family as the sibling, sets `SQLX_OFFLINE=true` so the query macros compile against the checked-in metadata and no SQLite file is needed at build time, and runs `cargo build --release -p todo`, which compiles the todo crate and its dependencies and nothing else in the workspace.

Three things in `main.rs` are wrong for a container and the agent changes exactly those. The bind address becomes `HOST` from the environment with `127.0.0.1` as the default, so local runs behave as before and the image sets `HOST=0.0.0.0`. The template glob and the static directory are resolved relative to the binary's working directory, so the runtime stage sets `WORKDIR /app` and copies `templates/` and `static/` beside the binary. The database is created on start: the agent adds `sqlx::migrate!("./migrations").run(&pool)` after the pool is built, with `create_if_missing` on the connect options, so a fresh container with `DATABASE_URL=sqlite:///data/todo.db` and a mounted volume at `/data` starts with the schema in place rather than a 500 on the first request. The runtime stage is `bitnami/minideb:bookworm`, runs as `USER 1001`, owns `/data`, and exposes 8080, matching the sibling.

It then builds and runs the image in its sandbox: `docker build -f basics/todo/Dockerfile -t todo .` from the root, `docker run --rm -p 8080:8080 -v todo-data:/data -e DATABASE_URL=sqlite:///data/todo.db todo`, a `curl` of `/` and a `POST /todo` with a form body, and a second `docker run` against the same volume to show the task survives a restart. It also runs `cargo test -p todo` and `cargo clippy -p todo --all-targets` after creating the database the way `ci.yml` does, because the `migrate!` call and the `HOST` change touch code the workspace tests compile. The pull request carries the image size from `docker images`, the two `curl` responses and the `cargo` output.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Dockerize the basics/todo crate in this Actix Web workspace and open a pull request. Read the workspace Cargo.toml and .sqlx directory, basics/todo/Cargo.toml, basics/todo/src/main.rs, src/db.rs, src/model.rs, basics/todo/.env, basics/todo/README.md, the migrations directory, docker/Dockerfile, docker/Cargo.toml, docker/.dockerignore and .github/workflows/ci.yml first. Write basics/todo/Dockerfile with the workspace root as build context: a rust slim bookworm build stage that copies the whole workspace (root Cargo.toml, Cargo.lock, .sqlx and every member directory, since Cargo reads every listed member), sets SQLX_OFFLINE=true and runs cargo build --release -p todo; and a minideb bookworm runtime stage that runs as USER 1001, has WORKDIR /app with templates and static copied beside the binary, owns /data, and exposes 8080. Add a root .dockerignore that excludes target directories and .git. In main.rs read HOST from the environment with 127.0.0.1 as the default, enable create_if_missing on the SQLite connect options and run sqlx::migrate! on startup. Document the build and run commands in basics/todo/README.md. Build the image from the root, run it with a volume at /data, curl / and POST /todo, restart it against the same volume and show the task is still there. Create the test database as ci.yml does, run cargo test -p todo and cargo clippy -p todo --all-targets, and paste the image size, the curl responses and the cargo output in the pull request. Do not change the routes, templates or session settings.",
  "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 that adds `basics/todo/Dockerfile` and a root `.dockerignore`, edits `basics/todo/src/main.rs` for the bind address, the connect options and the startup migration, and adds a Docker section to `basics/todo/README.md`. The description pastes the `docker build` summary and image size, the `curl` responses before and after the restart, and the `cargo test` and `clippy` output, and explains why the build context is the workspace root rather than the crate directory.

On the workspace's `CI` workflow the pull request runs `cargo check` and `cargo test` across every crate after creating `todo.db`, so the `migrate!` call compiles against the real migrations directory and the todo tests run with the new startup path. The run's record keeps both `docker run` sessions and every request made against them.

A reviewer checks that `.sqlx` is copied into the build context, since without it `SQLX_OFFLINE=true` fails the build; that the runtime user can write `/data`; and whether they want migrations at startup or as a separate `migrate` command in the image. Then they decide where the SQLite volume lives in their deployment.

## Run it on every Rust service without an image

A team with several Actix Web services sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own `main.rs` and manifests, so a crate on Postgres gets `DATABASE_URL` as a runtime variable with no volume, a crate with no templates gets a runtime stage with only the binary, and a single-crate repository gets its own directory as the context, and each run opens its own pull request. Send a `request_id` per request so a retried call returns the run it already started.

Give the key a `webhook_url` and every run it starts posts `run.completed` when its turn ends, with `data.session_url` to read the pull request link from. The key runs at most `max_concurrent` sessions at once, 4 by default, and a run started beyond that is answered `202` with `status: "queued"`, so a list of Actix services can be sent in one loop.

## Why run it on Rush?

A Dockerfile for a Rust crate inside a workspace fails for reasons that only show up when you build it: a workspace dependency that is not in the context, SQLx macros with no database and no metadata, a binary that binds loopback inside the container, and templates that are not where the glob looks. In a Rush sandbox the agent builds the image, runs it, makes real requests, restarts it against the volume and keeps every command and its output in the run's record, so the pull request shows a container that served a request rather than a file that looks right.

The result is a branch with a pull request, so nothing lands without review, and the three code changes are the smallest ones that make the crate run in a container. The same request works on any Actix Web crate, in a workspace or alone.

## Frequently asked questions

### Why is the build context the workspace root?

Because `basics/todo/Cargo.toml` takes its dependencies from `[workspace.dependencies]` and its edition from `[workspace.package]`, and the SQLx offline metadata lives in the root `.sqlx`. A context limited to the crate directory has none of that, which is what the sibling `docker/` crate's comment warns about, and Cargo also refuses a workspace whose listed members are missing, so the whole source tree goes in and `.dockerignore` keeps `target` out.

### Does the image need SQLite installed?

No. The `sqlx` SQLite driver bundles the library, so the runtime stage needs no SQLite package. The database file is created on first start at `DATABASE_URL` with `create_if_missing`, and the migration creates the `tasks` table.

### Why change main.rs at all?

Three lines: the bind address, because `127.0.0.1` inside a container is unreachable from outside; `create_if_missing` and `migrate!`, because the image has no `todo.db` and nothing in the crate created the schema before. Local `cargo run` behaves as before, since `HOST` defaults to loopback and the migration is a no-op on an existing database.

### Can it push the image to a registry?

Not in this request, which ends at a tested local image and a pull request. A follow-up can add a workflow that builds and pushes on a tag, with the registry credentials kept as a Project secret rather than in the prompt.

## More Actix Web jobs

- [Fix Actix Web Errors from Sentry Alerts](https://getrush.ai/run/fix-sentry-errors/actix/)
- [Review Pull Requests on an Actix Web API](https://getrush.ai/run/review-pull-requests/actix/)
- [Upgrade Cargo Dependencies Weekly for an Actix Web API](https://getrush.ai/run/upgrade-cargo-dependencies/actix/)

## The same job on other stacks

- [Dockerize an Angular App](https://getrush.ai/run/dockerize-app/angular/)
- [Dockerize an Axum API With SQLx and Postgres](https://getrush.ai/run/dockerize-app/axum/)
- [Dockerize an Eleventy Site](https://getrush.ai/run/dockerize-app/eleventy/)

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