# Dockerize a Vite React App

Yes. An agent writes a two-stage Dockerfile and nginx config for your Vite React app, builds and runs the image, checks it in a browser, and opens a pull request.

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

## What the agent does

The agent reads the files that decide how the image must be built. In an app like the `apps/react-vite` application in alan2207/bulletproof-react, `package.json` declares `build` as `tsc && vite build --base=/`, `test` as `vitest`, `lint` as `eslint src`, `check-types` as `tsc --noEmit`, and `test-e2e` as a `pm2` start of the mock server followed by Playwright; the README asks for Node 20 or newer and Yarn 1.22, and a `yarn.lock` sits beside the manifest. `vite.config.ts` sets `base` to `./`, which the build script overrides to `/`, puts the dev and preview servers on port 3000, and uses `vite-tsconfig-paths` for the `@/` imports. `src/config/env.ts` reads every `VITE_APP_*` variable at build time through `import.meta.env` and throws if `API_URL` is missing, so the API address is baked into the bundle and must be a build argument, not a runtime one. `.env.example` sets `VITE_APP_API_URL` and `VITE_APP_ENABLE_API_MOCKING=true`; `.env.example-e2e` points at the mock server on port 8080.

The `Dockerfile` the agent writes in `apps/react-vite/` has two stages. The build stage is `node:20-alpine`: copy `package.json` and `yarn.lock` first so the install layer caches, `yarn install --frozen-lockfile`, copy the rest, declare `ARG VITE_APP_API_URL` and `ARG VITE_APP_ENABLE_API_MOCKING` and pass them as environment to `yarn build`, which runs the type check and `vite build` into `dist/`. The runtime stage is `nginx:alpine` with `dist/` copied to the web root and an `nginx.conf` that serves `index.html` for any path that is not a file, because `src/app/router.tsx` uses React Router 7 in browser history mode with routes like `/app/discussions/:discussionId` from `src/config/paths.ts`; `/assets/` gets a long immutable cache header, since Vite hashes those filenames, `index.html` gets `no-cache`, and gzip is on. A `.dockerignore` keeps `node_modules`, `dist`, `e2e`, `playwright-report` and `.storybook` out of the context.

Because the app is a monorepo member and the root `package.json` only chains `yarn` into each app, the agent writes a `compose.yaml` in `apps/react-vite/` with the build context there, the two build arguments, and port 3000 mapped to nginx's 80 so it matches the dev server. It adds a second service for the repository's own mock API, `vite-node mock-server.ts`, which `mock-server.ts` starts with Express, `@mswjs/http-middleware` and the handlers under `src/testing/mocks/`, so `docker compose up` gives a working demo with `VITE_APP_API_URL` pointing at that service and mocking off in the bundle.

Then it proves the image. It builds it, starts it with `docker compose up`, and checks with `curl` that `/` and a deep link like `/app/discussions/1` both return the app shell with status 200, that a hashed file under `/assets/` carries the cache header, and that `/index.html` does not. It opens the home page in the sandbox browser and takes a screenshot, records the image size from `docker images`, runs `yarn lint` and `yarn check-types` to be sure nothing it added upset them, and opens a pull request with the files, the commands to build and run, the screenshot and the note that `VITE_APP_API_URL` is fixed at build time.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Dockerize this Vite React app. Read package.json, yarn.lock, vite.config.ts, src/config/env.ts, src/config/paths.ts, src/app/router.tsx, .env.example, .env.example-e2e, mock-server.ts and the workflows under .github/workflows first. Write a two-stage Dockerfile: a build stage on node:20-alpine that copies package.json and yarn.lock before the rest, runs yarn install --frozen-lockfile, takes VITE_APP_API_URL and VITE_APP_ENABLE_API_MOCKING as build arguments and runs yarn build; and a runtime stage on nginx:alpine serving dist with an nginx.conf that uses try_files to index.html for the browser-history router, long immutable cache headers for /assets, no-cache for index.html and gzip on. Add a .dockerignore and a compose.yaml that passes the build arguments, maps port 3000 to nginx, and runs the repository's mock API server as a second service. Build the image, start it with docker compose, confirm with curl that / and a deep link return the shell with status 200 and that a hashed asset carries the cache header, open the home page in the browser and take a screenshot, and record the image size from docker images. Run yarn lint and yarn check-types. Open a pull request with the files, the build and run commands, the screenshot, and a note that VITE_APP_API_URL is fixed at build time.",
  "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 adding four files to the app directory: `Dockerfile`, `nginx.conf`, `.dockerignore` and `compose.yaml`. No application source changes. The description lists the build arguments and their defaults from `.env.example`, the `docker build` and `docker compose up` commands, the image size the agent read from `docker images`, the `curl` results for the root and a deep link, and the screenshot of the home page served by nginx.

The repository's `React Vite CI` workflow runs on the branch as on any pull request: `yarn build`, `yarn test`, `yarn lint` and `yarn check-types`, plus the Playwright job against the mock server. The session transcript at `GET /o/{org}/sessions/{id}/messages` keeps the Docker build output layer by layer, the `curl` responses with headers, and the browser screenshot as an artifact on the session.

Before merging, a reviewer checks that the production API address is passed at build time wherever images are built, since the bundle cannot read it later; that `try_files` does not also swallow requests for missing assets, which should stay 404; and that the mock API service in `compose.yaml` is understood as a local convenience rather than a deployment target. A follow-up to the same session changes any of it on the same branch.

## Run it on every terminal hand-off

This is a job you start and check back on. One `POST /o/{org}/sessions` with this body and your repository in `resources` starts the run in its own sandbox with Docker available; `GET /o/{org}/sessions/{id}/events` streams the build if you want to watch, and the run continues when you close the laptop. When the turn ends the sandbox parks with the built image and its layer cache, so a follow-up through `POST /o/{org}/sessions/{id}/messages`, say to add a health check or a non-root user, rebuilds only the changed layers.

Give the key a `webhook_url` and Rush POSTs `run.completed` to your server when the turn finishes, with the result and the `session_url`. Send a `request_id` with the request so a retry returns the run already started.

## Why run it on Rush?

Dockerizing a single-page app is quick to write and slow to get right: the first image is a gigabyte because `node_modules` went into the runtime stage, deep links return nginx's 404 until `try_files` is added, and the API address turns out to be compiled in. On a laptop each of those is a rebuild. In a Rush sandbox the agent does the rebuilds on its own machine, keeps every layer's output, and opens the result in a browser before it opens the pull request.

What arrives is a pull request with evidence attached: the `curl` headers that show the cache policy, the screenshot that shows the app rendering from nginx, the image size, and the lint and type checks. Several apps in a monorepo can be dockerized at once, each in its own sandbox, and the compose file gives anyone a one-command demo with the repository's own mock API.

## Frequently asked questions

### Why is the API URL a build argument instead of an environment variable?

Because `src/config/env.ts` reads `import.meta.env`, which Vite replaces at build time, and throws at startup if `API_URL` is missing. The value is in the bundle, so the image is specific to one API address. Making it runtime-configurable means a small change to serve a config file or inject a script, which the agent can do in a follow-up if you ask; the pull request names the trade-off.

### Will deep links like /app/discussions/1 work?

Yes. The `nginx.conf` uses `try_files $uri $uri/ /index.html`, so any path that is not a real file serves the shell and React Router resolves it client-side. Real files such as `/favicon.ico` and the hashed bundles under `/assets/` are still served directly, and a missing asset is still a 404 rather than the shell.

### What about the mock API server?

The repository runs it locally with `vite-node mock-server.ts`, an Express app with `@mswjs/http-middleware` over the handlers in `src/testing/mocks/`. The `compose.yaml` adds it as a second service so `docker compose up` shows the app working end to end with `VITE_APP_ENABLE_API_MOCKING=false` and the API URL pointing at it. It is for demos and review, not production, and the file says so.

### Why node:20 and Yarn 1?

The README asks for Node 20 or newer and Yarn 1.22, the workflows install with `yarn install`, and the lockfile is `yarn.lock` in the Yarn 1 format. Node images ship Yarn 1, so the build stage needs no extra install and `--frozen-lockfile` fails loudly if the lock is stale.

### How big is the image?

The agent reads the size from `docker images` after the build and puts the number in the pull request rather than guessing. An `nginx:alpine` runtime with only `dist/` copied in is small; if it comes out larger than expected, the transcript shows which layer added the weight.

## More Vite + React jobs

- [Find Dead Code in a Vite React App](https://getrush.ai/run/find-dead-code/vite-react/)
- [Migrate ESLint to Flat Config in a Vite React App](https://getrush.ai/run/migrate-eslint-flat-config/vite-react/)
- [Upgrade npm Dependencies in a Vite React App](https://getrush.ai/run/upgrade-npm-dependencies/vite-react/)

## The same job on other stacks

- [Dockerize a Vue App](https://getrush.ai/run/dockerize-app/vue/)
- [Dockerize an Actix Web App](https://getrush.ai/run/dockerize-app/actix/)
- [Dockerize an Angular App](https://getrush.ai/run/dockerize-app/angular/)

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