# Write a Dockerfile for a discord.js Bot

Yes. An agent writes a Dockerfile and .dockerignore for a discord.js bot, builds and starts it in its sandbox without baking in the token, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-dockerfile/discord-js/). 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 bot starts before it writes a line of Dockerfile. In a bot laid out like lawalbolaji/Discord-bot-QR-code-generator, `index.js` loads `.env` with `dotenv`, reads `QR_BOT_TOKEN`, creates a `Client` with the `GUILDS` intent on discord.js 13.3.1, and loads every command with `fs.readdirSync('./commands')`, a path relative to the working directory rather than to the file, so the container's `WORKDIR` decides whether the commands are found. `deployCommands.js` registers the slash commands for one guild through `@discordjs/rest` using `CLIENT_ID` and `GUILD_ID`; it is a one-time step, not the process that stays up. `package.json` has `start`, a `devStart` on nodemon, a `test` that runs one mocha file against `externalAPIs/googleQR.js`, and an `engines` field pinning Node 17.0.1. A `Procfile` says `worker: node index.js`, `.gitignore` lists `.env` and `/node_modules`, and there is no Dockerfile, no CI and no lockfile-aware install anywhere.

The Dockerfile it writes is short because the bot is: a `node` base image at the major version `engines` names, `WORKDIR /app`, `package.json` and `package-lock.json` copied first so `npm ci --omit=dev` is cached until the dependencies change, then `index.js`, `deployCommands.js`, `commands/` and `externalAPIs/` copied in, `USER node`, and `CMD ["node", "index.js"]`. No `EXPOSE`: the bot opens a connection out to Discord's gateway and listens on nothing. No `COPY .env`: the token is passed at run time with `--env-file` or the host's secret store, and `.dockerignore` lists `.env`, `node_modules`, `tests`, `.git` and the `Procfile` so a stray copy of the token can never land in a layer. `dotenv` does nothing when there is no `.env`, so the same `index.js` works in both places. The one-time registration becomes `docker run --rm --env-file .env <image> node deployCommands.js`, documented in the README beside `npm start`.

Node 17 is an odd-numbered release line that left maintenance long ago, so an image built on it no longer receives security fixes. The agent builds on the version `engines` names, because that is what the code was run on, and says plainly in the pull request that the next change should be to move `engines` and the base image to a current LTS, which discord.js 13 supports; it does not make that change unasked.

The sandbox has no Docker, so the agent installs it with `sudo apt-get install -y docker.io`, starts `dockerd`, and builds with `docker build -t qr-bot .`. It then starts the image with a placeholder token: the process loads both commands, reaches `client.login`, and discord.js rejects the token, which is the proof that module resolution, the working directory and the `USER` switch are right without any real credential in the sandbox. It also runs `npm test` to show the mocha test still passes on the image's Node version, and checks the image's history for the `.env` file to confirm the ignore rules held.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Write a Dockerfile for this discord.js bot. Read package.json, package-lock.json, index.js, deployCommands.js, every file under commands/ and externalAPIs/, the tests, the Procfile, .gitignore and the README first. Use a node base image at the major version the engines field names, install with npm ci omitting dev dependencies in a cached layer, copy only the files the bot needs, run as the node user, and start with node index.js. Do not expose a port and do not copy .env; write a .dockerignore that keeps .env, node_modules, tests and .git out of the build context. Document how to pass the token at run time and how to run deployCommands.js once from the image. In the sandbox, install Docker, build the image, start it with a placeholder token to confirm the commands load and the process reaches login, run the test suite, and confirm no .env file is in any layer. State in the pull request that the Node line in engines is out of support and what moving to a current LTS would involve, without making that change. Open a pull request whose description quotes the build log and the container's startup output.",
  "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` and `.dockerignore` and changes the README. `index.js`, the commands, `package.json` and the `Procfile` are untouched. The description quotes the `docker build` log with its cached layers, the container's startup output up to the rejected placeholder token, the `npm test` result, and the layer listing that shows no `.env`, and it names the Node line the image is built on and why that line should change next.

The run's record keeps the full build and the container logs, so a reviewer sees the exact base image digest that was pulled and what the bot printed on start rather than a summary.

Before merging, a reviewer chooses where the token will come from on the host that runs the container, decides whether the `Procfile` should stay for a platform that still reads it, and whether to take the Node upgrade now or in the next pull request.

## Run it on every bot repository without a Dockerfile

A team that runs several bots sends this body once per repository from a script, with 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 already started. `GET /o/{org}/github` lists the repositories the Rush GitHub App reaches; the script keeps the ones with `discord.js` in `package.json` and no `Dockerfile`.

Each run reads its own bot. One that already has a lockfile for pnpm gets a `corepack`-enabled image and `pnpm install --frozen-lockfile --prod`; one written in TypeScript gets a build stage and a runtime stage; one that serves an HTTP health endpoint gets an `EXPOSE` and a `HEALTHCHECK`. Give the key a webhook URL and Rush POSTs `run.completed` with each `session_url` as the images build.

## Why run it on Rush?

A Dockerfile for a bot is easy to get wrong in ways that only show at run time: the commands directory is read relative to the wrong working directory, the token ends up in a layer because `.env` was copied, the process runs as root, or the base image is a Node line that no longer gets fixes. In a Rush sandbox the agent builds the image, starts it and reads what the bot prints, so the mistakes are found before the pull request exists rather than on the host at midnight.

The build log, the container output and the layer listing stay with the run, so the reviewer sees the image that was built rather than the file that describes it. Several bots run in parallel, each in its own sandbox with its own Docker daemon and its own pull request.

## Frequently asked questions

### Why no EXPOSE or HEALTHCHECK?

The bot connects out to Discord's gateway and serves nothing, so there is no port to publish and no HTTP endpoint to probe. A health check that only tests that `node` is alive would pass while the gateway connection is down, which is worse than none. A bot that adds an HTTP endpoint gets both in the same change.

### How does the token get into the container?

At run time, never at build time: `docker run --env-file .env` on a developer machine, or the secret store of whatever runs the container in production. `.dockerignore` keeps `.env` out of the build context, and the agent lists the image's layers in the pull request to show it is not there.

### What about deployCommands.js?

It registers the slash commands with Discord once per guild and exits, so it is not the container's command. The README gains the one-line `docker run --rm` invocation that runs it from the same image, with the same environment file.

### Why build on an old Node line instead of upgrading?

Because the request is a Dockerfile, and the code was pinned to that line by its `engines` field. The agent builds what the repository says it runs on, states in the pull request that the line is out of support, and describes the upgrade so it can be its own reviewed change.

### Does the Procfile still matter?

Only to a platform that reads it. It is left in place and listed in `.dockerignore`; removing it is a decision for whoever still deploys the bot that way.

## More discord.js jobs

- [Add Modals to a discord.js Bot](https://getrush.ai/run/add-modals/discord-js/)
- [Add a Slash Command to a discord.js Bot](https://getrush.ai/run/add-slash-command/discord-js/)
- [Enable TypeScript Strict Mode in a discord.js Bot](https://getrush.ai/run/enable-strict-mode/discord-js/)

## The same job on other stacks

- [Add a Dockerfile to a .NET Web App](https://getrush.ai/run/add-dockerfile/dotnet/)
- [Add a Dockerfile to a Gradle Spring Boot App](https://getrush.ai/run/add-dockerfile/gradle/)
- [Add a Dockerfile to a Leptos Axum App](https://getrush.ai/run/add-dockerfile/leptos/)

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