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), 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
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:RESEND_API_KEYbecomes required, sample secrets from.env.exampleare rejected, and modules add their own rules. It also stops the logger from writing to the console (see Logging).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 container. Run it from your machine against the production database instead (see Railway).
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"}.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
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.
Related
Deploy on Railway
The recommended host, step by step.
File storage
Where production uploads live.