# Add a Dockerfile to a Sequelize App

Yes. An agent writes a two-stage Dockerfile for your Express and Sequelize app, keeps the SQLite file on a volume, seeds it once, runs the image, and opens a PR.

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

## What the agent does

The agent reads how the app installs, starts and stores its data before writing an image, because the answers decide three lines of the Dockerfile. In an app like the `express-main-example` in sequelize/express-example, `package.json` has `start` as `node index.js` and `setup-example-db` as `node sqlite-example-database/setup.js`, an `engines` field of Node 10 or later, and `body-parser`, `express` ^4.17.1, `sequelize` ^6.3.3 and `sqlite3` ^5.0.0 as its only dependencies. `.npmrc` contains `package-lock=false`, so there is no lockfile and `npm ci` cannot be used. `index.js` listens on a hard-coded port `8080` after `sequelize.authenticate()`, and exits with status 1 if the database cannot be reached. `sequelize/index.js` opens SQLite at the relative path `sqlite-example-database/example-db.sqlite`, with `logQueryParameters` and `benchmark` on, and `setup.js` calls `sequelize.sync({ force: true })` and inserts four users, three orchestras and random instruments, which means running it on every start would wipe the data.

From that the agent writes a `Dockerfile` with two stages on `node:20-bookworm-slim`. The build stage installs `python3`, `make` and `g++` so `sqlite3`, a native module, compiles if npm finds no prebuilt binary for the image's Node and libc, then runs `npm install --omit=dev` because there is no lockfile to feed `npm ci`. The final stage copies `node_modules` and the sources into `/app`, sets `WORKDIR /app` so the relative SQLite path resolves, declares `VOLUME /app/sqlite-example-database`, switches to the `node` user, exposes `8080`, and starts through a small `docker-entrypoint.sh` that runs `npm run setup-example-db` only when `example-db.sqlite` is missing or `SEED=true` is set, then `exec node index.js`. A `.dockerignore` keeps `node_modules`, `.git` and any local `*.sqlite` file out of the build context.

Two things the sources do not handle get a line each. `index.js` has no signal handler, and Node as process 1 in a container does not exit on `SIGTERM` by default, so the entrypoint uses `exec` and the pull request recommends `docker run --init` or `init: true` in Compose, and adds a `SIGTERM` handler to `index.js` that closes the Express server and calls `sequelize.close()` so the SQLite file is released cleanly. The port stays `8080` but is read from `process.env.PORT` first, a one-line change that lets the container be remapped without a rebuild. The agent also adds `.github/workflows/docker.yml` that builds the image on pull requests and pushes nothing, so a broken Dockerfile fails a check instead of a deploy.

The sandbox has no Docker, so the agent installs `docker.io`, starts `dockerd`, builds the image and runs it with a host directory mounted at `/app/sqlite-example-database`. It shows the entrypoint seeding on the first start, `curl` on `/api/users` returning the four seeded users and `/api/orchestras?includeInstruments` returning the three orchestras with their instruments, a second start that skips the seed because the file exists, and `docker stop` ending the process on `SIGTERM` within the grace period. Then it opens the pull request with that output.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Write a Dockerfile for this Express and Sequelize app. Read package.json, .npmrc, index.js, express/app.js, express/routes/users.js, sequelize/index.js, sequelize/models/user.model.js, sqlite-example-database/setup.js and readme.md first. Use two stages on node:20-bookworm-slim: a build stage that installs python3, make and g++ and runs npm install --omit=dev, since .npmrc disables the lockfile and npm ci needs one, and a final stage that copies node_modules and the sources into /app, sets WORKDIR /app, declares VOLUME /app/sqlite-example-database, runs as the node user and exposes 8080. Add docker-entrypoint.sh that runs npm run setup-example-db only when example-db.sqlite is missing or SEED=true, then exec node index.js, and explain in the pull request that setup.js force-syncs and would erase data on every start. Add .dockerignore. In index.js read PORT from the environment with 8080 as the default and add a SIGTERM handler that closes the server and calls sequelize.close(). Add .github/workflows/docker.yml that builds the image on pull requests and pushes nothing. Docker is not installed here: install docker.io, start dockerd, build the image, run it with a host directory mounted at the volume path, curl /api/users and /api/orchestras?includeInstruments, start it a second time to show the seed is skipped, docker stop it and show the server ending on SIGTERM, then open a pull request that pastes that output and the final docker image ls line.",
  "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 `Dockerfile`, `.dockerignore`, `docker-entrypoint.sh` and `.github/workflows/docker.yml`, and changes `index.js` and `readme.md`. The description pastes the build output for both stages, the entrypoint's seed line on the first start and its skip line on the second, the `/api/users` response with the four usernames from `setup.js`, the shutdown line after `docker stop`, and the `docker image ls` row for the final image.

The run's record holds the Docker install, the build, both container starts, the requests and the stop. The models, routes and `setup.js` are untouched; the only source change is the port and the signal handler in `index.js`.

Before merging, a reviewer decides where the SQLite file should live in production, a named volume or a bind mount, whether `logQueryParameters` should stay on inside a container whose logs are shipped elsewhere, since it prints every bound value, and whether to commit a lockfile after all by removing `package-lock=false`, which would let the build stage use `npm ci`.

## Run it on every new Node service

A team with several Express services starts one run per repository from its own server: one `POST /o/{org}/sessions` each, with that 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 already started rather than a second pull request. Each run reads that service's own `package.json` and database setup, so a service on Postgres gets no volume and a `DATABASE_URL`, and a service with a lockfile gets `npm ci`.

Afterwards, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` and the prompt "if this pull request changes package.json dependencies, the start script or the SQLite storage path, say whether the Dockerfile and docker-entrypoint.sh still match and comment with the line to change" keeps the image from drifting from the app. The trigger adds the pull request after the prompt; only work by an owner, member or collaborator fires it, never a fork's.

## Why run it on Rush?

A Dockerfile for this app is short, and every line that matters comes from reading a file: `.npmrc` rules out `npm ci`, `setup.js` rules out seeding on every start, the relative storage path fixes the working directory, and the missing signal handler decides how the container stops. The agent reads those files first, then builds and runs the image rather than reasoning about whether the native module compiles.

In its own sandbox the agent installs Docker, runs the container twice and stops it, and the run keeps the output for the pull request. For a team with many Node services, each gets its own run and its own pull request with an image that matches how that service installs and stores data.

## Frequently asked questions

### Why npm install and not npm ci?

Because `.npmrc` sets `package-lock=false`, so the repository has no lockfile and `npm ci` refuses to run without one. The build stage uses `npm install --omit=dev`, and the pull request notes that removing that line and committing `package-lock.json` would make builds reproducible and let the Dockerfile switch to `npm ci`.

### Why not seed the database in the image?

`setup.js` calls `sync({ force: true })`, which drops and recreates every table before inserting the example rows, so baking it into the image or running it on every start would erase whatever the running app wrote. The entrypoint runs it once, when the SQLite file is absent, and the data lives on the volume, not in the image layer.

### Why does the build stage install a compiler?

`sqlite3` is a native module. npm fetches a prebuilt binary when one is published for the image's Node version and libc, and compiles from source otherwise, which needs `python3`, `make` and `g++`. Installing them in the build stage means the image builds either way, and the final stage carries none of them.

### What happens on docker stop?

The new handler in `index.js` catches `SIGTERM`, closes the Express server and calls `sequelize.close()`, then the process exits before Docker's grace period ends. Because Node as process 1 does not get default signal handling, the entrypoint uses `exec` and the pull request recommends running with `--init`, which also reaps any child processes.

## More Sequelize jobs

- [Add GitHub Actions CI to a Sequelize App](https://getrush.ai/run/add-github-actions-ci/sequelize/)
- [Add Graceful Shutdown to an Express and Sequelize App](https://getrush.ai/run/add-graceful-shutdown/sequelize/)
- [Add Health Checks to an Express and Sequelize API](https://getrush.ai/run/add-health-checks/sequelize/)

## The same job on other stacks

- [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/)
- [Add a Dockerfile to a Socket.IO Chat Server](https://getrush.ai/run/add-dockerfile/socket-io/)

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