name: deploying-nestjs-production
description: Prepares a NestJS service for production: graceful shutdown, health/readiness, structured logs, config, Dockerfile, CI gates, and rollout safety. Use when promoting a service to staging/production or auditing readiness.
license: MIT
Deploying NestJS Production
When to use
- Promoting a service to staging/production
- Auditing production readiness
- Adding graceful shutdown, health checks, Dockerfile
Core rules
- Graceful shutdown on
SIGTERM/SIGINT
- Health (
/health) and readiness (/ready) endpoints
- Structured JSON logs via global interceptor
- Config validated at boot (Zod), secrets not logged
- Dockerfile: multi-stage build, non-root user
- CI gates:
tsc --noEmit, ESLint, tests, npm audit
Reference shape (TypeScript)
Graceful Shutdown
const app = await NestFactory.create(AppModule);
app.enableShutdownHooks();
await app.listen(3000);
Health Controller
@Controller('health')
export class HealthController {
@Get() health() { return { status: 'ok' }; }
@Get('ready') ready() { return { status: 'ready' }; }
}
Dockerfile
FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
USER node
CMD ["node", "dist/main.js"]
Examples — Do
app.enableShutdownHooks();
Examples — Don't
const app = await NestFactory.create(AppModule);
await app.listen(3000);
Checklist
See reference/deployment-patterns.md for full patterns.