Docker Compose production setup for Node.js + Redis + PostgreSQL: multi-stage builds, health checks, volume management, secrets, networking, and deploy configs.
Docker Compose is excellent for production when you use it correctly. The common failure modes — services starting before their dependencies are ready, secrets in environment variables visible in docker inspect, single-layer images that are 1.2GB, no memory limits causing OOM kills, volumes with no backup strategy — are all avoidable with the right configuration.
This guide walks through a complete production-grade Node.js + Redis + PostgreSQL stack. Every configuration choice here addresses a real failure mode.
Try it yourself: Free .env File Converter — free, no signup, runs in your browser.
Multi-Stage Dockerfile: Node.js
A naive Node.js Dockerfile copies everything and installs all dependencies in one layer. The result is a 1GB+ image with dev dependencies, source maps, and unnecessary files. Multi-stage builds produce images under 200MB with only what production needs.
# Dockerfile
# Stage 1: Install all dependencies (including dev)
FROM node:22-alpine AS deps
WORKDIR /app
# Copy package files first for layer caching
COPY package*.json ./
COPY prisma ./prisma/
# Install all deps including devDeps for build
RUN npm ci --frozen-lockfile
# Stage 2: Build the application
FROM node:22-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Generate Prisma client
RUN npx prisma generate
# Build TypeScript
RUN npm run build
# Stage 3: Production image — only runtime artifacts
FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
# Security: run as non-root
RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 --ingroup nodejs nodeuser
# Copy only what production needs
COPY --from=builder --chown=nodeuser:nodejs /app/dist ./dist
COPY --from=builder --chown=nodeuser:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nodeuser:nodejs /app/prisma ./prisma
COPY --chown=nodeuser:nodejs package.json ./
USER nodeuser
EXPOSE 3000
# Use exec form to handle signals properly (not shell form)
CMD ["node", "dist/server.js"]
HEALTHCHECK --interval=15s --timeout=5s --start-period=30s --retries=3 CMD wget -qO /dev/null http://localhost:3000/health || exit 1
Complete docker-compose.yml for Production
# docker-compose.yml
name: myapp
services:
# --- PostgreSQL ---
postgres:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_DB: ${POSTGRES_DB:-myapp}
POSTGRES_USER: ${POSTGRES_USER:-myapp}
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets:
- postgres_password
volumes:
- postgres_data:/var/lib/postgresql/data
- ./db/init:/docker-entrypoint-initdb.d:ro
networks:
- backend
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-myapp} -d ${POSTGRES_DB:-myapp}"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
reservations:
memory: 256M
# --- Redis ---
redis:
image: redis:7-alpine
restart: unless-stopped
command: >
redis-server
--requirepass-file /run/secrets/redis_password
--maxmemory 256mb
--maxmemory-policy allkeys-lru
--save 60 1000
--appendonly yes
secrets:
- redis_password
volumes:
- redis_data:/data
networks:
- backend
healthcheck:
test: ["CMD", "redis-cli", "-a", "$(cat /run/secrets/redis_password)", "ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 15s
deploy:
resources:
limits:
cpus: "0.5"
memory: 384M
# --- Database migrations (run-once job) ---
migrator:
build:
context: .
dockerfile: Dockerfile
target: builder
command: npx prisma migrate deploy
environment:
DATABASE_URL: "postgresql://${POSTGRES_USER:-myapp}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-myapp}"
secrets:
- postgres_password
depends_on:
postgres:
condition: service_healthy
networks:
- backend
restart: "no"
# --- Application ---
app:
build:
context: .
dockerfile: Dockerfile
target: runner
cache_from:
- type=registry,ref=myregistry.io/myapp:buildcache
cache_to:
- type=registry,ref=myregistry.io/myapp:buildcache,mode=max
image: myregistry.io/myapp:latest
restart: unless-stopped
environment:
NODE_ENV: production
PORT: "3000"
DATABASE_URL_FILE: /run/secrets/database_url
REDIS_URL_FILE: /run/secrets/redis_url
JWT_SECRET_FILE: /run/secrets/jwt_secret
secrets:
- database_url
- redis_url
- jwt_secret
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
migrator:
condition: service_completed_successfully
networks:
- backend
- frontend
healthcheck:
test: ["CMD", "wget", "-qO", "/dev/null", "http://localhost:3000/health"]
interval: 15s
timeout: 5s
retries: 3
start_period: 30s
deploy:
replicas: 2
update_config:
parallelism: 1
delay: 15s
failure_action: rollback
order: start-first
rollback_config:
parallelism: 1
delay: 10s
resources:
limits:
cpus: "1.0"
memory: 512M
reservations:
cpus: "0.25"
memory: 128M
logging:
driver: "json-file"
options:
max-size: "50m"
max-file: "5"
# --- Nginx reverse proxy ---
nginx:
image: nginx:1.25-alpine
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- certbot_www:/var/www/certbot:ro
- certbot_conf:/etc/letsencrypt:ro
depends_on:
app:
condition: service_healthy
networks:
- frontend
healthcheck:
test: ["CMD", "nginx", "-t"]
interval: 30s
timeout: 10s
volumes:
postgres_data:
driver: local
redis_data:
driver: local
certbot_www:
certbot_conf:
networks:
backend:
driver: bridge
internal: true # No direct internet access
frontend:
driver: bridge
secrets:
postgres_password:
file: ./secrets/postgres_password.txt
redis_password:
file: ./secrets/redis_password.txt
database_url:
file: ./secrets/database_url.txt
redis_url:
file: ./secrets/redis_url.txt
jwt_secret:
file: ./secrets/jwt_secret.txt
Comments · 0
Beta: comments are stored locally on your device and not visible to other readers.
No comments yet. Be the first to share your thoughts.