NestJS Production Stack
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-alpine4 environment: 5 - POSTGRES_USER=nestjs6 - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}7 - POSTGRES_DB=nestjs_app8 volumes: 9 - postgres_data:/var/lib/postgresql/data10 networks: 11 - nestjs_net1213 redis: 14 image: redis:7-alpine15 volumes: 16 - redis_data:/data17 networks: 18 - nestjs_net1920 nestjs: 21 build: 22 context: .23 dockerfile: Dockerfile24 command: node dist/main.js25 environment: 26 - NODE_ENV=production27 - DATABASE_URL=postgres://nestjs:${POSTGRES_PASSWORD}@postgres:5432/nestjs_app28 - REDIS_HOST=redis29 - REDIS_PORT=637930 - JWT_SECRET=${JWT_SECRET}31 depends_on: 32 - postgres33 - redis34 networks: 35 - nestjs_net3637 bull-board: 38 image: deadly0/bull-board:latest39 ports: 40 - "3001:3000"41 environment: 42 - REDIS_HOST=redis43 - REDIS_PORT=637944 depends_on: 45 - redis46 networks: 47 - nestjs_net4849 nginx: 50 image: nginx:alpine51 ports: 52 - "80:80"53 - "443:443"54 volumes: 55 - ./nginx.conf:/etc/nginx/nginx.conf:ro56 depends_on: 57 - nestjs58 networks: 59 - nestjs_net6061volumes: 62 postgres_data: 63 redis_data: 6465networks: 66 nestjs_net: .env Template
.env
1# NestJS Production2POSTGRES_PASSWORD=secure_postgres_password3JWT_SECRET=your_jwt_secret_key_here45# NestJS at http://localhost6# Bull Board at http://localhost:3001Usage Notes
- 1NestJS at http://localhost
- 2Bull Board at http://localhost:3001
- 3TypeORM for PostgreSQL
- 4Bull queues with Redis
- 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 file2cat > docker-compose.yml << 'EOF'3services:4 postgres:5 image: postgres:15-alpine6 environment:7 - POSTGRES_USER=nestjs8 - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}9 - POSTGRES_DB=nestjs_app10 volumes:11 - postgres_data:/var/lib/postgresql/data12 networks:13 - nestjs_net1415 redis:16 image: redis:7-alpine17 volumes:18 - redis_data:/data19 networks:20 - nestjs_net2122 nestjs:23 build:24 context: .25 dockerfile: Dockerfile26 command: node dist/main.js27 environment:28 - NODE_ENV=production29 - DATABASE_URL=postgres://nestjs:${POSTGRES_PASSWORD}@postgres:5432/nestjs_app30 - REDIS_HOST=redis31 - REDIS_PORT=637932 - JWT_SECRET=${JWT_SECRET}33 depends_on:34 - postgres35 - redis36 networks:37 - nestjs_net3839 bull-board:40 image: deadly0/bull-board:latest41 ports:42 - "3001:3000"43 environment:44 - REDIS_HOST=redis45 - REDIS_PORT=637946 depends_on:47 - redis48 networks:49 - nestjs_net5051 nginx:52 image: nginx:alpine53 ports:54 - "80:80"55 - "443:443"56 volumes:57 - ./nginx.conf:/etc/nginx/nginx.conf:ro58 depends_on:59 - nestjs60 networks:61 - nestjs_net6263volumes:64 postgres_data:65 redis_data:6667networks:68 nestjs_net:69EOF7071# 2. Create the .env file72cat > .env << 'EOF'73# NestJS Production74POSTGRES_PASSWORD=secure_postgres_password75JWT_SECRET=your_jwt_secret_key_here7677# NestJS at http://localhost78# Bull Board at http://localhost:300179EOF8081# 3. Start the services82docker compose up -d8384# 4. View logs85docker compose logs -fOne-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 | bashTroubleshooting
- 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
Components
nestjsbullpostgresqlredisnginx
Tags
#nestjs#nodejs#typescript#bull#production
Category
Full Web StacksAd Space
Shortcuts: C CopyF FavoriteD Download