Agent-skills-standard nestjs-deployment
Containerize NestJS apps with multi-stage Docker builds, tune Node.js memory, and implement graceful shutdown hooks. Use when writing Dockerfiles, configuring K8s deployments, or adding shutdown hooks for NestJS. (triggers: Dockerfile, k8s/**, helm/**, max-old-space-size, shutdown hooks)
install
source · Clone the upstream repo
git clone https://github.com/HoangNguyen0403/agent-skills-standard
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HoangNguyen0403/agent-skills-standard "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/nestjs/nestjs-deployment" ~/.claude/skills/hoangnguyen0403-agent-skills-standard-nestjs-deployment && rm -rf "$T"
manifest:
skills/nestjs/nestjs-deployment/SKILL.mdsource content
Deployment & Ops Standards
Priority: P1 (OPERATIONAL)
Workflow: Containerize NestJS App
- Write multi-stage Dockerfile — Build stage installs devDeps and runs
; run stage copies onlynest build
and pruneddist
.node_modules - Set non-root user — Add
to Dockerfile.USER node - Tune memory — Set
to ~75% of container memory limit.--max-old-space-size - Enable shutdown hooks — Call
inapp.enableShutdownHooks()
.main.ts - Add K8s pre-stop — Configure 5-10s sleep pre-stop hook for LB draining.
Dockerfile Example
Runtime Tuning (Node.js)
- Memory Config: Container memory != Node memory.
- Rule: Explicitly set Max Old Space.
- Command:
node --max-old-space-size=XXX dist/main - Calculation: Set to ~75-80% of Kubernetes Limit. (Limit: 1GB -> OldSpace: 800MB).
- Graceful Shutdown:
- Signal: Listen to
.SIGTERM - NestJS:
mandatory.app.enableShutdownHooks() - Sleep: Add "Pre-Stop" sleep in K8s (5-10s) to allow Load Balancer to drain connections before Node process stops accepting traffic.
Init Patterns
- Database Migrations:
- Anti-Pattern: Running migration in
on startup.main.ts - Pro Pattern: Use Init Container in Kubernetes that runs
before app container starts.npm run typeorm:migration:run
Environment Variables & CI/CD
- CI/CD Pipelines (GitHub, GitLab, Azure, etc.):
- If you modify
to add new environment variable, you MUST map it explicitly in your deployment pipeline/infrastructure-as-code.src/config/env.validation.ts - Platform Context:
- Cloud Run/ECS: Variables must explicitly passed in service definition.
- Kubernetes: New variables must added to
manifest orDeployment
/ConfigMap
.Secret - Lambda/Serverless: Must added to
or provider console.serverless.yml - Fundamental Rule: Application code configuration changes "breaking changes" for infrastructure layer. Never assume environment inheritance.
Anti-Patterns
- No migrations in main.ts: Use K8s Init Containers or pre-deploy CI steps for migration runs.
- No root user in Docker: Always add
to Dockerfile; running as root security risk.USER node - No unbounded Node heap: Set
to ~75% of container memory limit.--max-old-space-size