# n8n in production with Docker Compose (lesson AUT-204-L01): PostgreSQL, external task runners and Caddy (TLS).
# Based on the official n8n-hosting examples (MIT), changed so that ONLY Caddy publishes ports and all secrets
# come from files (_FILE). Nothing here is a real secret; you create the files yourself.
#
#   cp env.example .env                      # set the domain, versions, timezone
#   mkdir -p secrets && chmod 700 secrets    # never commit this folder
#   openssl rand -hex 32 > secrets/n8n_encryption_key.txt     # BACK THIS UP SEPARATELY, see the runbook
#   openssl rand -hex 24 > secrets/postgres_password.txt
#   chmod 644 secrets/*.txt                 # the n8n container user (uid 1000) must be able to READ the files;
#                                            # the 700 folder keeps other host users out
#   docker compose up -d
services:
  postgres:
    image: postgres:${POSTGRES_VERSION:?set POSTGRES_VERSION in .env}
    restart: unless-stopped
    environment:
      POSTGRES_USER: n8n
      POSTGRES_DB: n8n
      POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
      PGDATA: /var/lib/postgresql/data          # keep the data path stable across major versions
    secrets: [postgres_password]
    volumes:
      - db_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n -d n8n"]
      interval: 10s
      timeout: 5s
      retries: 10

  n8n:
    image: docker.n8n.io/n8nio/n8n:${N8N_VERSION:?pin N8N_VERSION in .env}
    restart: unless-stopped
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres
      DB_POSTGRESDB_DATABASE: n8n
      DB_POSTGRESDB_USER: n8n
      DB_POSTGRESDB_PASSWORD_FILE: /run/secrets/postgres_password
      N8N_ENCRYPTION_KEY_FILE: /run/secrets/n8n_encryption_key
      N8N_HOST: ${N8N_DOMAIN}
      N8N_PROTOCOL: https
      N8N_PORT: "5678"
      N8N_WEBHOOK_URL: https://${N8N_DOMAIN}/
      N8N_PROXY_HOPS: "1"
      GENERIC_TIMEZONE: ${TIMEZONE}
      TZ: ${TIMEZONE}
      N8N_RUNNERS_MODE: external
      N8N_RUNNERS_AUTH_TOKEN: ${RUNNERS_AUTH_TOKEN:?set RUNNERS_AUTH_TOKEN in .env}   # shared only between n8n and its runners
      N8N_RUNNERS_BROKER_LISTEN_ADDRESS: 0.0.0.0
      N8N_DIAGNOSTICS_ENABLED: "false"
      N8N_PUBLIC_API_DISABLED: "true"
      N8N_COMMUNITY_PACKAGES_ENABLED: "false"
      EXECUTIONS_DATA_PRUNE: "true"
      EXECUTIONS_DATA_MAX_AGE: "336"
    secrets: [postgres_password, n8n_encryption_key]
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      postgres:
        condition: service_healthy
    # no "ports": n8n is reachable only through Caddy

  n8n-runners:
    image: n8nio/runners:${N8N_VERSION}          # must match the n8n version
    restart: unless-stopped
    environment:
      N8N_RUNNERS_AUTH_TOKEN: ${RUNNERS_AUTH_TOKEN}
      N8N_RUNNERS_TASK_BROKER_URI: http://n8n:5679
    depends_on: [n8n]

  caddy:
    image: caddy:${CADDY_VERSION:?set CADDY_VERSION in .env}
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    environment:
      N8N_DOMAIN: ${N8N_DOMAIN}
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    depends_on: [n8n]

secrets:
  postgres_password:
    file: ./secrets/postgres_password.txt
  n8n_encryption_key:
    file: ./secrets/n8n_encryption_key.txt

volumes:
  db_data:
  n8n_data:
  caddy_data:
  caddy_config:
