Part 5: DevOps, Deployment, and Security
Introduction
Containerization with Docker
Multi-Stage Dockerfile
# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
# Copy package files
COPY package*.json ./
COPY prisma ./prisma/
# Install dependencies (including dev dependencies)
RUN npm ci
# Copy source code
COPY . .
# Build TypeScript
RUN npm run build
# Generate Prisma client
RUN npx prisma generate
# Production stage
FROM node:20-alpine AS production
# Security: Run as non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001
WORKDIR /app
# Copy package files and install prod dependencies only
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force
# Copy built app from builder
COPY --from=builder --chown=nodejs:nodejs /app/dist ./dist
COPY --from=builder --chown=nodejs:nodejs /app/node_modules/.prisma ./node_modules/.prisma
COPY --from=builder --chown=nodejs:nodejs /app/prisma ./prisma
# Set user
USER nodejs
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/health', (r) => r.statusCode === 200 ? process.exit(0) : process.exit(1))"
EXPOSE 3000
CMD ["node", "dist/main.js"]Docker Compose for Local Development
Testing the Container
CI/CD Pipeline with GitHub Actions
Pipeline Architecture
GitHub Actions Workflow
Kubernetes Deployment
Deployment Manifest
Service and Ingress
ConfigMap and Secrets
Security Hardening
Network Policies
Pod Security Policy
Monitoring and Observability
Exposing Metrics
Metrics Middleware
Grafana Dashboard
Key Takeaways
What's Next
Last updated