> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gatojs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Docker

> How the GatoJS Dockerfile builds a small production image from Next.js standalone output, how to run it, and how to apply Prisma migrations on deploy.

Every GatoJS project ships a multi-stage `Dockerfile` that produces a production image from Next.js [standalone output](https://nextjs.org/docs/app/api-reference/config/next-config-js/output). The image runs `node server.js` as a non-root user and includes the Prisma CLI so you can apply migrations from the same image. Railway builds from this file (see [Railway](https://docs.gatojs.com/deployment/railway.md)), and you can run it on any container host.

## Standalone output

`next.config.ts` sets `output: "standalone"`. With it, `next build` writes a `server.js` to `.next/standalone` together with only the files from `node_modules` the server actually uses. The image needs neither the full `node_modules` nor your source code.

## Build stages

| Stage | What it does | Why |
| - | - | - |
| `base` | `FROM node:22-alpine` | Shared base. Node 22, matching `.nvmrc`. |
| `deps` | Copies `package.json`, `package-lock.json` and every workspace package's `package.json`, then runs `npm ci --ignore-scripts`. | Installs exactly what the lockfile says. `--ignore-scripts` skips `postinstall` (`prisma generate`) and `prepare` (Husky). |
| `builder` | Copies `node_modules` and the build context, runs `npx prisma generate`, `mkdir -p public` and `npm run build` with `NEXT_TELEMETRY_DISABLED=1`. | The Prisma client is regenerated because `src/generated` is excluded from the context. `public/` is created because the runner stage copies it. |
| `runner` | Sets runtime env, creates user `nextjs` (UID 1001), copies `public/`, `.next/standalone`, `.next/static`, `prisma/` and `prisma.config.ts`, installs the Prisma CLI, creates `/app/volumes/uploads`, switches to `USER nextjs` and runs `CMD ["node", "server.js"]`. | The final image: server, static assets, schema with migrations, and the migration tool. |

The workspace `COPY` lines in the `deps` stage sit in a generated region:

```dockerfile Dockerfile theme={null}
COPY package.json package-lock.json ./
# wa:workspaces:start
COPY packages/env/package.json ./packages/env/package.json
COPY packages/observability/package.json ./packages/observability/package.json
# wa:workspaces:end
RUN npm ci --ignore-scripts
```

`gato generate` rewrites this region from the directories under `packages/`. If you add a workspace package, regenerate so `npm ci` sees it.

## Runtime environment

```dockerfile Dockerfile theme={null}
FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
```

* **`NODE_ENV=production`** turns on the production rules of env validation: `RESEND_API_KEY` becomes required, sample secrets from `.env.example` are rejected, and modules add their own rules. It also stops the logger from writing to the console (see [Logging](https://docs.gatojs.com/configuration/logging-and-observability.md)).
* **`PORT=3000`** is a default. Platforms such as Railway inject their own `PORT`, which overrides it.
* **`HOSTNAME="0.0.0.0"`** makes the server listen on all interfaces.

> [!WARNING]
>
> **Don't set `HOSTNAME` to `127.0.0.1`.** With that value, unprefixed (default-locale) URLs answer with a 307 redirect to themselves, an endless loop, and the healthcheck won't catch it because `/api` bypasses the proxy. Safe values are `0.0.0.0` and `::`. `localhost` works but listens on loopback only, which is useless inside a container.

## Prisma CLI in the image

Migrations run with `npx prisma migrate deploy`, which reads `prisma.config.ts`. That file imports `prisma` and `dotenv`, but the standalone output contains only `@prisma/client`. The runner stage installs both packages separately:

```dockerfile Dockerfile theme={null}
RUN cd /tmp && npm init -y > /dev/null 2>&1 \
    && npm install prisma@7.10.1 dotenv@17.4.2 > /dev/null 2>&1 \
    && cp -rP /tmp/node_modules/* /app/node_modules/ \
    && mkdir -p /app/node_modules/.bin \
    && ln -sf ../prisma/build/index.js /app/node_modules/.bin/prisma \
    && rm -rf /tmp/node_modules /tmp/package.json /tmp/package-lock.json \
    && chown -R nextjs:nodejs /app/node_modules
```

> [!WARNING]
>
> These versions are pinned to match `package-lock.json`. When you upgrade `prisma` or `dotenv` in `package.json`, update them here too; nothing checks that the two stay in sync.

The image has no `scripts/`, `src/`, `tsx` or dev dependencies, so `npm run admin:create` **can't run inside the container**. Run it from your machine against the production database instead (see [Railway](https://docs.gatojs.com/deployment/railway.md#create-the-first-accounts)).

## What goes into the build context

`.dockerignore` keeps local and kit-only files out of the image:

```text .dockerignore theme={null}
node_modules
.next
.git
.env
.env.*
!.env.example
*.md
.claude
.superpowers
.turbo
volumes
e2e
playwright.config.ts
test-results
playwright-report
src/generated
kit
docs
```

* `.env` and `.env.*` never enter the image, so local secrets can't leak into it. Production variables come from the runtime.
* `node_modules`, `.next` and `src/generated` are rebuilt inside the image.
* `prisma/migrations/` is **not** excluded, so committed migrations ship with the image. Make sure they are committed: platforms that build from git only see tracked files.

## Build and run locally

**1. Build the image**

```bash theme={null}
docker build -t my-app .
```

The build needs no environment variables; `next build` skips the env check.

**2. Prepare an env file**

Create a production env file outside the repository with at least the required variables:

```bash prod.env theme={null}
DATABASE_URL=postgresql://user:pass@db.example.com:5432/my_app
BETTER_AUTH_SECRET=<openssl rand -base64 32>
APP_URL=https://app.example.com
EMAIL_FROM=My App <noreply@example.com>
RESEND_API_KEY=re_...
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.example.com
```

Add module variables too, for example `API_KEY_PEPPER` for `api-keys` and storage settings for `uploads`. See [Environment variables](https://docs.gatojs.com/configuration/environment-variables.md).

**3. Apply migrations**

Run the migration command in a one-off container from the same image:

```bash theme={null}
docker run --rm --env-file prod.env my-app npx prisma migrate deploy
```

**4. Start the server**

```bash theme={null}
docker run -d --name my-app --env-file prod.env -p 3000:3000 my-app
```

Then check `http://localhost:3000/api/healthcheck`. It should return `{"status":"ok","db":"ok"}`.

If a variable is missing or invalid, the container exits with code 1 right after start and prints a report starting with `Nieprawidłowe zmienne środowiskowe` ("invalid environment variables"). Read it with `docker logs my-app`. A restart policy won't help, because every start fails the same way.

## Migrations on deploy

Run `npx prisma migrate deploy` **before** the new version starts serving traffic, in a separate container or pre-deploy step from the same image. That way the migrations in the image always match the code.

| Output | Meaning | Action |
| - | - | - |
| `N migrations found in prisma/migrations`, then `All migrations have been successfully applied.` | Migrations applied. | None. |
| `No pending migrations to apply.` after a migration count | The database is up to date. | None. |
| `No migration found in prisma/migrations` | The image has no migrations. The command still exits with `0`, and the healthcheck passes on an empty database. | Commit `prisma/migrations/` and redeploy. |
| `Error: P3005`, `The database schema is not empty.` | Tables exist without migration history, for example after `prisma db push`. | Baseline the database following Prisma's instructions from the error link. |
| `Error: P3018`, `A migration failed to apply.` | A migration failed. Later deploys fail with `P3009` until it is resolved. | Fix the cause, then mark it with `prisma migrate resolve`. |

> [!NOTE]
>
> The kit doesn't ship migrations, because the schema depends on the modules you install. Create your first one with `npx prisma migrate dev --name init` and commit the whole `prisma/migrations/` folder. See [Database and migrations](https://docs.gatojs.com/guides/database-and-migrations.md).

## Persistent files

The runner creates `/app/volumes/uploads`, owned by `nextjs`, in every image. The `uploads` module's `local` provider writes there when you mount a volume at that path and set `STORAGE_LOCAL_PATH=/app/volumes/uploads`:

```bash theme={null}
docker run -d --env-file prod.env \
  -v uploads:/app/volumes/uploads \
  -e STORAGE_LOCAL_PATH=/app/volumes/uploads \
  -p 3000:3000 my-app
```

Production with `local` storage outside Railway requires an **absolute** `STORAGE_LOCAL_PATH`. See [File storage](https://docs.gatojs.com/deployment/file-storage.md) for permissions, S3 and multi-instance setups.

## Related

> [card] [Deploy on Railway](https://docs.gatojs.com/deployment/railway.md)
>
> The recommended host, step by step.

> [card] [File storage](https://docs.gatojs.com/deployment/file-storage.md)
>
> Where production uploads live.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.