Prepare your NestJS application for production with CI/CD, logging, and monitoring.
Welcome to Dockerizing your NestJS Application! In this chapter, you'll learn how to containerize your NestJS app using Docker, write optimized multi-stage Dockerfiles, and orchestrate services with Docker Compose. Let's dive into the world of containerization!
Let's start by creating a basic Dockerfile for your NestJS application:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
This is a good starting point, but we can optimize it further!
FROM node:14 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node:14
WORKDIR /app
COPY --from=builder /app/dist ./dist
CMD ["npm", "run", "start:prod"]
The multi-stage approach helps keep your final image lean and secure by separating the build environment from the production environment.
Docker Compose allows you to define and run multi-container applications. Here's an example setup:
version: '3'
services:
app:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:12
environment:
POSTGRES_USER: nest
POSTGRES_PASSWORD: nest
POSTGRES_DB: nest
This setup will start your NestJS app and a PostgreSQL database, linked together seamlessly.
FROM node:14-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
RUN npm run build
FROM node:14-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package.json .
RUN npm install --production
CMD ["npm", "run", "start:prod"]
This example demonstrates an optimized production build using Alpine Linux, resulting in a smaller and more secure image.
You've now learned how to Dockerize your NestJS application effectively. By following best practices and avoiding common pitfalls, you can ensure smooth deployment and scalability of your applications.
Welcome to the world of logging, metrics, and health checks in NestJS! These tools are essential for maintaining a healthy, observable application. In this chapter, we'll explore how to implement structured logging with Pino
or Winston
, expose health endpoints using @nestjs/terminus, integrate metrics with Prometheus and Grafana, and apply best practices for production diagnostics.
Structured logging is a modern approach to capturing application logs in a machine-readable format. Unlike traditional plain-text logs, structured logs allow for easier searching, filtering, and analysis.
Pino
for fast, flexible logging with JSON outputWinston
for a widely adopted logging solutionimport { Logger } from 'pino';
const logger = Logger({
name: 'my-app',
level: 'info'
});
logger.info('Application started', {
version: '1.0.0',
environment: 'production'
});
Health checks are critical for monitoring application uptime and performance. NestJS provides a simple way to implement health checks using the @nestjs/terminus package.
import { HealthCheck, HealthCheckService } from '@nestjs/terminus';
@HealthCheck()
public async checkStatus(): Promise<string> {
try {
await this.databaseService.ping();
return 'All systems operational';
} catch (error) {
throw new Error('Database connection failed');
}
}
Monitoring your application's performance with metrics is essential for proactive maintenance. Prometheus and Grafana provide a powerful combination for collecting, visualizing, and alerting on metrics.
import { Module } from '@nestjs/common';
import { MonitoringModule } from '@nestjs/microservices';
@Module({
imports: [
MonitoringModule.forRoot({
name: 'my-app',
path: '/metrics'
})
]
})
export class AppModule {}
Question 1 of 10