Course/Chapter 2/3. Production Deployment with Docker Compose
    beginner
    Chapter 2: Hosting n8n — Self-Hosted vs Cloud

    3. Production Deployment with Docker Compose

    Deploy n8n to a production server with Docker Compose.

    15m Lesson 3 of 5

    For production use, Docker Compose provides a reproducible, multi-service deployment with PostgreSQL for reliable data storage and Nginx for HTTPS termination.

    Architecture

    • - n8n: The automation engine
    • - PostgreSQL: Reliable database backend (replaces default SQLite)
    • - Nginx/Traefik: Reverse proxy with automatic SSL

    Code Examples

    docker-compose.yml
    yaml
    version: '3.8'
    
    services:
      n8n:
        image: n8nio/n8n
        restart: always
        ports:
          - "5678:5678"
        environment:
          - DB_TYPE=postgresdb
          - DB_POSTGRESDB_HOST=postgres
          - DB_POSTGRESDB_DATABASE=n8n
          - DB_POSTGRESDB_USER=n8n
          - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
          - N8N_HOST=${N8N_HOST}
          - N8N_PROTOCOL=https
          - WEBHOOK_URL=https://${N8N_HOST}/
        volumes:
          - n8n_data:/home/node/.n8n
        depends_on:
          - postgres
    
      postgres:
        image: postgres:15
        restart: always
        environment:
          - POSTGRES_DB=n8n
          - POSTGRES_USER=n8n
          - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
        volumes:
          - postgres_data:/var/lib/postgresql/data
    
    volumes:
      n8n_data:
      postgres_data:
    .env file
    bash
    N8N_HOST=n8n.yourdomain.com
    POSTGRES_PASSWORD=your_secure_password_here
    N8N_ENCRYPTION_KEY=your_random_encryption_key

    Pro Tips

    • 💡Always set N8N_ENCRYPTION_KEY — without it, credentials are encrypted with a default key
    • 💡Use PostgreSQL instead of SQLite for any production deployment
    • 💡Set WEBHOOK_URL to your public domain so webhook triggers work correctly

    Comprehension Quiz

    Answer 2 of 2 correctly to unlock lesson completion.

    1. Which database should replace SQLite for production n8n?

    2. What does the WEBHOOK_URL environment variable control?

    Pass the quiz above to unlock lesson completion