StackBlueprint

EP01 ARCHITECTURE · DOCKER COMPOSE + POSTGRES 17 + CADDY

Production n8n Docker Compose Guide: Postgres 17, Redis & Caddy

Default n8n Compose tutorials run SQLite, leave :latest unpinned, and expose ports on 0.0.0.0. Use this pinned n8n:3.4.4 + postgres:17-alpine + caddy:2-alpine architecture with 127.0.0.1 isolation on an $8.79–$14.99/mo VPS.

Public Attack Surface 80 / 443 only Postgres & Redis internal-only · 127.0.0.1 loopback
Monthly VPS Cost (KVM 2) $8.79–$14.99/mo 8 GB RAM · 0 execution cap vs $144/mo n8n Cloud
Kit v1 ($49) Payback 12 days vs n8n Cloud Pro ($144/mo) · 3d vs Make 500k

1. Five Failure Modes in Generic n8n Docker Compose Files

Before copying any community YAML onto Ubuntu 24.04, check five production invariants that separate a fragile weekend setup from an unattended B2B automation host:

Failure Mode Common DIY Mistake StackBlueprint EP01 Production Setting
Firewall Bypass ports: ["5432:5432"] exposes Postgres on 0.0.0.0 despite UFW Zero public DB/Redis ports; internal Compose network + 127.0.0.1:5678 fallback
Disk Exhaustion Unbounded Docker logs + infinite execution history in Postgres json-file rotation (10m × 3) + EXECUTIONS_DATA_MAX_AGE=168 & 10000 prune cap
OOM Crash Loop No container mem_limit or Node heap cap during large webhooks NODE_OPTIONS=--max-old-space-size=896 + explicit mem_limit & cpus per service
Abrupt Shutdowns Default 10s Docker SIGKILL drops mid-flight webhook jobs N8N_GRACEFUL_SHUTDOWN_TIMEOUT=65 paired with stop_grace_period: 75s
Silent Upgrade Drift n8nio/n8n:latest & postgres:latest break on recreate Pinned n8n:3.4.4, postgres:17-alpine, redis:7-alpine, caddy:2-alpine

2. Production docker-compose.yml & Caddyfile Excerpt (EP01 Kit)

Below is the core architecture excerpt from n8n Production Kit v1. Notice the YAML anchor for log rotation, readiness healthchecks on every service, zero published ports on PostgreSQL or Redis, and the opt-in profiles: [queue] activation for Redis + workers:

x-logging: &logging
  driver: json-file
  options:
    max-size: "10m"
    max-file: "3"

x-n8n: &n8n
  image: "${N8N_IMAGE:-docker.n8n.io/n8nio/n8n:3.4.4}"
  restart: unless-stopped
  stop_grace_period: 75s
  logging: *logging
  environment:
    DB_TYPE: postgresdb
    DB_POSTGRESDB_HOST: postgres
    DB_POSTGRESDB_PORT: "5432"
    DB_POSTGRESDB_POOL_SIZE: "5"
    N8N_ENCRYPTION_KEY: "${N8N_ENCRYPTION_KEY:?Set an encryption key}"
    N8N_HOST: "${N8N_DOMAIN:?Set your hostname}"
    N8N_PROTOCOL: https
    N8N_PROXY_HOPS: "1"
    N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS: "true"
    N8N_DIAGNOSTICS_ENABLED: "false"
    EXECUTIONS_MODE: "${EXECUTIONS_MODE:-regular}"
    EXECUTIONS_DATA_PRUNE: "true"
    EXECUTIONS_DATA_MAX_AGE: "168"
    EXECUTIONS_DATA_PRUNE_MAX_COUNT: "10000"
    N8N_DEFAULT_BINARY_DATA_MODE: database
    N8N_GRACEFUL_SHUTDOWN_TIMEOUT: "65"
    NODE_OPTIONS: --max-old-space-size=896
  healthcheck:
    test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:5678/healthz/readiness').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
    interval: 15s
    timeout: 5s
    retries: 5
    start_period: 90s

services:
  postgres:
    image: "${POSTGRES_IMAGE:-postgres:17-alpine}"
    restart: unless-stopped
    logging: *logging
    mem_limit: 768m
    cpus: 1.0
    # Zero public ports: internal Docker network only (use 127.0.0.1:5432:5432 only if local psql is required)
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""]
      interval: 10s
      timeout: 5s
      retries: 6

  redis:
    image: "${REDIS_IMAGE:-redis:7-alpine}"
    profiles: [queue]
    restart: unless-stopped
    mem_limit: 384m
    cpus: 0.5
    command: ["redis-server", "--appendonly", "yes", "--maxmemory", "192mb", "--maxmemory-policy", "noeviction"]
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5

  n8n:
    <<: *n8n
    mem_limit: 1280m
    cpus: 1.5
    # If running behind an external Nginx/Traefik proxy instead of Caddy, add:
    # ports: ["127.0.0.1:5678:5678"]
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - n8n_data:/home/node/.n8n

  caddy:
    image: "${CADDY_IMAGE:-caddy:2-alpine}"
    restart: unless-stopped
    mem_limit: 128m
    cpus: 0.5
    ports: ["80:80", "443:443"]
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config

Hardened Caddyfile Reverse Proxy (Auto-TLS + Security Headers)

Caddy terminates Let’s Encrypt TLS automatically, strips the Server banner, enforces HSTS, and raises response_header_timeout to 300s so long-running webhook responses do not time out prematurely:

{
  email {$ACME_EMAIL}
  acme_ca https://acme-v02.api.letsencrypt.org/directory
}

{$N8N_DOMAIN} {
  encode zstd gzip
  header {
    Strict-Transport-Security "max-age=31536000"
    X-Content-Type-Options nosniff
    Referrer-Policy strict-origin-when-cross-origin
    -Server
  }
  reverse_proxy n8n:5678 {
    transport http {
      dial_timeout 5s
      response_header_timeout 300s
    }
  }
}

3. Pre-Deploy Security Audit & Complete Ops Bundle

Before pushing any custom Compose file or n8n workflow JSON to production, run a 100% local static security scan using StackGuard n8n Workflow & Docker Security Audit (56 AST rules, zero telemetry) to catch 0.0.0.0 bindings, hardcoded credentials, and unsafe expression sinks.

Need the complete Ubuntu 24.04 hardening script (01-harden-vps.sh), automated age-encrypted R2 backups, isolated restore drill, and uptime/error workflows ready to deploy in under 15 minutes?

Affiliate disclosure: Hostinger links are affiliate links; we may earn a commission at no extra cost to you. See Affiliate Disclosure. Published by Oriancy.

Frequently Asked Questions

Why should PostgreSQL 17 and Redis never expose public ports in n8n Docker Compose?
Docker inserts iptables rules ahead of UFW when a port is published on 0.0.0.0. Keeping Postgres 17 and Redis internal to the Compose network—or binding strictly to 127.0.0.1—ensures only Caddy ports 80 and 443 are exposed publicly.
How much VPS RAM is required for n8n + PostgreSQL 17 + Redis in 2026?
Single mode (n8n + postgres:17-alpine + caddy) fits comfortably in 4 GB RAM. Enabling Queue mode with redis:7-alpine and dedicated workers requires 8 GB RAM (such as Hostinger KVM 2 at $8.79/mo promo or $14.99/mo renewal) to prevent OOM kills under burst concurrency.
Why pin n8n, Postgres, Redis, and Caddy tags instead of using :latest?
Pinning explicit versions (n8n:3.4.4, postgres:17-alpine, redis:7-alpine, caddy:2-alpine) prevents unintended major database upgrades or breaking workflow migrations during routine restarts and guarantees clean restore drills.