docker.recipes

NestJS Production Stack

advanced

Production NestJS with Bull queues, PostgreSQL, and Redis.

Overview

NestJS is a progressive Node.js framework built with TypeScript that uses decorators and modules to create scalable server-side applications. Inspired by Angular's architecture, NestJS brings enterprise-grade structure to Node.js development with built-in support for dependency injection, guards, interceptors, and pipes. It has gained significant traction since 2017 for building maintainable APIs and microservices that can scale with growing applications. This production stack combines NestJS with Bull queues for background job processing, PostgreSQL for robust data persistence, and Redis for caching and session management. Bull leverages Redis to manage job queues with features like job retries, delayed processing, and priority queuing, while PostgreSQL provides ACID-compliant data storage with advanced querying capabilities. The NGINX reverse proxy handles load balancing, SSL termination, and static file serving to optimize performance under production loads. Development teams building SaaS platforms, e-commerce applications, or content management systems will benefit from this stack's enterprise-ready architecture. The combination addresses common production challenges like background processing for email notifications, image resizing, or data exports, while maintaining data integrity through PostgreSQL's transaction support. TypeScript throughout the stack ensures type safety and reduces runtime errors in production environments.

Key Features

  • Bull queue management with Redis backend for reliable background job processing and retry mechanisms
  • TypeORM integration with PostgreSQL for type-safe database operations and migration management
  • NestJS modular architecture with dependency injection and decorator-based routing
  • Redis-based session storage and caching layer for improved application performance
  • Bull Board dashboard for monitoring queue status, failed jobs, and processing metrics
  • NGINX reverse proxy with load balancing and SSL termination capabilities
  • JWT authentication infrastructure built into the NestJS application
  • PostgreSQL JSONB support for flexible document storage alongside relational data

Common Use Cases

  • 1SaaS applications requiring background processing for user-generated content, email campaigns, or data exports
  • 2E-commerce platforms handling order processing, inventory updates, and payment notifications through queues
  • 3Content management systems with image resizing, video transcoding, and search indexing workflows
  • 4Financial applications needing ACID-compliant transactions and audit logging with PostgreSQL
  • 5Multi-tenant applications leveraging PostgreSQL's row-level security and NestJS guards
  • 6API-first applications serving mobile apps and SPAs with JWT authentication and Redis session management
  • 7Data analytics platforms processing large datasets through Bull queues with PostgreSQL aggregation

Prerequisites

  • Docker Engine 20.10+ and Docker Compose V2 for container orchestration
  • Minimum 2GB RAM recommended (PostgreSQL: 1GB+, Redis: 512MB+, NestJS: 256MB+, NGINX: 256MB+)
  • Available ports 80, 443 (NGINX), and 3001 (Bull Board) on the host system
  • Environment variables: POSTGRES_PASSWORD and JWT_SECRET must be configured
  • Basic understanding of TypeScript, decorators, and NestJS module system
  • SSL certificates for production NGINX configuration with HTTPS termination

For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

docker-compose.yml

docker-compose.yml
1services:
2 postgres:
3 image: postgres:15-alpine
4 environment:
5 - POSTGRES_USER=nestjs
6 - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
7 - POSTGRES_DB=nestjs_app
8 volumes:
9 - postgres_data:/var/lib/postgresql/data
10 networks:
11 - nestjs_net
12
13 redis:
14 image: redis:7-alpine
15 volumes:
16 - redis_data:/data
17 networks:
18 - nestjs_net
19
20 nestjs:
21 build:
22 context: .
23 dockerfile: Dockerfile
24 command: node dist/main.js
25 environment:
26 - NODE_ENV=production
27 - DATABASE_URL=postgres://nestjs:${POSTGRES_PASSWORD}@postgres:5432/nestjs_app
28 - REDIS_HOST=redis
29 - REDIS_PORT=6379
30 - JWT_SECRET=${JWT_SECRET}
31 depends_on:
32 - postgres
33 - redis
34 networks:
35 - nestjs_net
36
37 bull-board:
38 image: deadly0/bull-board:latest
39 ports:
40 - "3001:3000"
41 environment:
42 - REDIS_HOST=redis
43 - REDIS_PORT=6379
44 depends_on:
45 - redis
46 networks:
47 - nestjs_net
48
49 nginx:
50 image: nginx:alpine
51 ports:
52 - "80:80"
53 - "443:443"
54 volumes:
55 - ./nginx.conf:/etc/nginx/nginx.conf:ro
56 depends_on:
57 - nestjs
58 networks:
59 - nestjs_net
60
61volumes:
62 postgres_data:
63 redis_data:
64
65networks:
66 nestjs_net:

.env Template

.env
1# NestJS Production
2POSTGRES_PASSWORD=secure_postgres_password
3JWT_SECRET=your_jwt_secret_key_here
4
5# NestJS at http://localhost
6# Bull Board at http://localhost:3001

Usage Notes

  1. 1NestJS at http://localhost
  2. 2Bull Board at http://localhost:3001
  3. 3TypeORM for PostgreSQL
  4. 4Bull queues with Redis
  5. 5JWT authentication ready

Individual Services(5 services)

Copy individual services to mix and match with your existing compose files.

postgres
postgres:
  image: postgres:15-alpine
  environment:
    - POSTGRES_USER=nestjs
    - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    - POSTGRES_DB=nestjs_app
  volumes:
    - postgres_data:/var/lib/postgresql/data
  networks:
    - nestjs_net
redis
redis:
  image: redis:7-alpine
  volumes:
    - redis_data:/data
  networks:
    - nestjs_net
nestjs
nestjs:
  build:
    context: .
    dockerfile: Dockerfile
  command: node dist/main.js
  environment:
    - NODE_ENV=production
    - DATABASE_URL=postgres://nestjs:${POSTGRES_PASSWORD}@postgres:5432/nestjs_app
    - REDIS_HOST=redis
    - REDIS_PORT=6379
    - JWT_SECRET=${JWT_SECRET}
  depends_on:
    - postgres
    - redis
  networks:
    - nestjs_net
bull-board
bull-board:
  image: deadly0/bull-board:latest
  ports:
    - "3001:3000"
  environment:
    - REDIS_HOST=redis
    - REDIS_PORT=6379
  depends_on:
    - redis
  networks:
    - nestjs_net
nginx
nginx:
  image: nginx:alpine
  ports:
    - "80:80"
    - "443:443"
  volumes:
    - ./nginx.conf:/etc/nginx/nginx.conf:ro
  depends_on:
    - nestjs
  networks:
    - nestjs_net

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 postgres:
5 image: postgres:15-alpine
6 environment:
7 - POSTGRES_USER=nestjs
8 - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
9 - POSTGRES_DB=nestjs_app
10 volumes:
11 - postgres_data:/var/lib/postgresql/data
12 networks:
13 - nestjs_net
14
15 redis:
16 image: redis:7-alpine
17 volumes:
18 - redis_data:/data
19 networks:
20 - nestjs_net
21
22 nestjs:
23 build:
24 context: .
25 dockerfile: Dockerfile
26 command: node dist/main.js
27 environment:
28 - NODE_ENV=production
29 - DATABASE_URL=postgres://nestjs:${POSTGRES_PASSWORD}@postgres:5432/nestjs_app
30 - REDIS_HOST=redis
31 - REDIS_PORT=6379
32 - JWT_SECRET=${JWT_SECRET}
33 depends_on:
34 - postgres
35 - redis
36 networks:
37 - nestjs_net
38
39 bull-board:
40 image: deadly0/bull-board:latest
41 ports:
42 - "3001:3000"
43 environment:
44 - REDIS_HOST=redis
45 - REDIS_PORT=6379
46 depends_on:
47 - redis
48 networks:
49 - nestjs_net
50
51 nginx:
52 image: nginx:alpine
53 ports:
54 - "80:80"
55 - "443:443"
56 volumes:
57 - ./nginx.conf:/etc/nginx/nginx.conf:ro
58 depends_on:
59 - nestjs
60 networks:
61 - nestjs_net
62
63volumes:
64 postgres_data:
65 redis_data:
66
67networks:
68 nestjs_net:
69EOF
70
71# 2. Create the .env file
72cat > .env << 'EOF'
73# NestJS Production
74POSTGRES_PASSWORD=secure_postgres_password
75JWT_SECRET=your_jwt_secret_key_here
76
77# NestJS at http://localhost
78# Bull Board at http://localhost:3001
79EOF
80
81# 3. Start the services
82docker compose up -d
83
84# 4. View logs
85docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/nestjs-production/run | bash

Troubleshooting

  • NestJS container fails to connect to PostgreSQL: Ensure POSTGRES_PASSWORD environment variable matches in both services and verify the DATABASE_URL format
  • Bull queues not processing jobs: Check Redis connectivity and ensure REDIS_HOST points to the redis service name, not localhost
  • TypeORM migration errors: Run migrations manually with 'docker-compose exec nestjs npm run migration:run' if auto-migration fails
  • Bull Board shows empty queues: Verify Redis connection parameters match between NestJS Bull configuration and Bull Board environment variables
  • NGINX returns 502 Bad Gateway: Check that NestJS service is healthy and listening on the correct internal port, usually 3000
  • PostgreSQL container exits with permission errors: Ensure the postgres_data volume has correct ownership or recreate the volume

Community Notes

Loading...
Loading notes...

Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license

Ad Space