NestJS

Deployment

Preparing a NestJS application for production deployment using Docker.

Dockerfile: A multi-stage Docker build to keep the production image lightweight.
📄Dockerfile
Dockerfile
# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# Production stage
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --only=production
COPY --from=builder /app/dist ./dist

EXPOSE 3000
CMD ["node", "dist/main"]
Docker Compose: Defining the NestJS app service for local production testing or basic deployment.
📄docker-compose.yml
YAML
version: '3.8'
services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=production
      - PORT=3000
    restart: unless-stopped