Dockerfile that produces a production image from Next.js standalone 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), the self-hosted Compose stack builds from it, and you can run it on any container host. Every project also publishes the image to the GitHub Container Registry for amd64 and arm64 (Published images).
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
The workspace
COPY lines in the deps stage sit in a generated region:
Dockerfile
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
NODE_ENV=productionturns on the production rules of env validation: a mail provider (orMAIL_PROVIDER=console) becomes required (Email), sample secrets from.env.exampleare rejected, and modules add their own rules. It also stops the logger from writing to the console (see Logging).NEXT_TELEMETRY_DISABLED=1andCHECKPOINT_DISABLE=1turn off Next.js telemetry and the Prisma CLI’s update check.PORT=3000is a default. Platforms such as Railway inject their ownPORT, which overrides it.HOSTNAME="0.0.0.0"makes the server listen on all interfaces.
Prisma CLI in the image
Migrations run withnpx 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
scripts/, src/, tsx or dev dependencies, so npm run admin:create can’t run inside the app container. Run it from your machine against the production database (see Railway), or in a container built from the builder stage, as the self-hosted stack’s tools service does (Self-hosting).
What goes into the build context
.dockerignore keeps local and kit-only files out of the image:
.dockerignore
.envand.env.*never enter the image, so local secrets can’t leak into it. Production variables come from the runtime.node_modules,.nextandsrc/generatedare 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
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:Add module variables too, for example
prod.env
API_KEY_PEPPER for api-keys and storage settings for uploads. See Environment variables.3
Apply migrations
Run the migration command in a one-off container from the same image:
4
Start the server
http://localhost:3000/api/healthcheck. It should return {"status":"ok","db":"ok"}.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
Runnpx 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.
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.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:
local storage outside Railway requires an absolute STORAGE_LOCAL_PATH. See File storage for permissions, S3 and multi-instance setups.
Published images
Every project has.github/workflows/docker-publish.yml. It builds the Dockerfile natively on amd64 and arm64 runners and pushes one multi-arch image to the GitHub Container Registry (GHCR) of the repository. Staging, production and a rollback then run the same tested image instead of a build on each host.
- The image name is the repository name in lower case, because GHCR rejects upper case.
- Pull requests never publish.
- The workflow uses only
GITHUB_TOKEN; it needs no secrets. - Each platform image carries SBOM and provenance attestations. The multi-arch image also gets a signed GitHub attestation, except in a private repository: GitHub signs attestations for private repositories only on Enterprise Cloud, so the step is skipped there.
git tag v1.2.3 && git push origin v1.2.3) or from your own release flow.
Pull the image on a host
The first push tomain creates a private package, listed under the repository’s (or organization’s) Packages. Either make it public in its Package settings (Change visibility), or log the host in with a token that can read it, such as a personal access token (classic) with the read:packages scope:
1.2.3), not latest or edge, so a restart never changes the code. Run prisma migrate deploy from the new image before it serves traffic, as for any image (Migrations on deploy). The self-hosted stack, Railway and the Helm chart can all run the published image.
Check an image
linux/amd64 and linux/arm64. The second checks the signed provenance of an image from a public repository.
Related
Deploy on Railway
The recommended host, step by step.
File storage
Where production uploads live.
Self-hosting
The image next to its database on your own server.
Kubernetes
The image on a cluster, with the Helm chart.