NewCode CLOUD — 20% off your first month on all cloud VPSClaim

Self-Host n8n with Docker: Workflow Automation and AI Agents on Your Own Server

n8n is a workflow automation tool — an open, self-hostable alternative to Zapier and Make. You connect apps and APIs with visual nodes: receive a webhook, query a database, call an AI model, post to Telegram or Slack, update a spreadsheet. Self-hosting gives you unlimited workflows and executions, keeps your data on your server, and lets workflows reach internal systems.

This guide installs n8n with Docker Compose, PostgreSQL and Caddy for automatic HTTPS.

n8n is distributed under its own “fair-code” licence. Self-hosting for your own business is permitted; check the licence terms if you plan to offer n8n to others as a service.

Choosing a server

Usage Suggested resources
Personal / a few workflows 1–2 vCPU, 2 GB RAM
Team, hundreds of executions per hour 2–4 vCPU, 4–8 GB RAM
Heavy AI or data processing 4+ vCPU, 8 GB+ RAM, queue mode

Location matters for AI workflows. If your workflows call OpenAI, Anthropic (Claude) or Google (Gemini) APIs, the server must be in a region those APIs serve. Servers in Hong Kong cannot access these APIs directly; servers in Japan, Singapore or the US can. A Japan or US server is a common choice for teams in Asia — see AI hosting.

Prerequisites

  • Docker and Compose installed — install Docker.
  • A subdomain such as n8n.example.com with an A record to the server.
  • Ports 80 and 443 open.

Step 1: Project directory

sudo mkdir -p /srv/n8n && cd /srv/n8n
sudo chown $USER:$USER /srv/n8n

Create .env:

cat > .env <<EOF
DOMAIN=n8n.example.com
GENERIC_TIMEZONE=Asia/Hong_Kong
POSTGRES_PASSWORD=$(openssl rand -base64 24 | tr -d '/+=')
N8N_ENCRYPTION_KEY=$(openssl rand -hex 32)
EOF
chmod 600 .env

Back up N8N_ENCRYPTION_KEY somewhere safe. n8n encrypts all stored credentials (API keys, passwords) with it; without the key, a restored database is useless.

Step 2: compose.yaml

services:
  postgres:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: n8n
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: n8n
    volumes:
      - db-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U n8n"]
      interval: 10s
      retries: 5

  n8n:
    image: docker.n8n.io/n8nio/n8n:latest
    restart: unless-stopped
    environment:
      DB_TYPE: postgresdb
      DB_POSTGRESDB_HOST: postgres
      DB_POSTGRESDB_DATABASE: n8n
      DB_POSTGRESDB_USER: n8n
      DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
      N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
      N8N_HOST: ${DOMAIN}
      N8N_PROTOCOL: https
      N8N_PORT: 5678
      WEBHOOK_URL: https://${DOMAIN}/
      GENERIC_TIMEZONE: ${GENERIC_TIMEZONE}
      TZ: ${GENERIC_TIMEZONE}
      N8N_PROXY_HOPS: 1
      EXECUTIONS_DATA_PRUNE: "true"
      EXECUTIONS_DATA_MAX_AGE: 336
    volumes:
      - n8n-data:/home/node/.n8n
      - ./files:/files
    depends_on:
      postgres:
        condition: service_healthy

  caddy:
    image: caddy:2
    restart: unless-stopped
    ports: ["80:80", "443:443"]
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
    depends_on: [n8n]

volumes:
  db-data:
  n8n-data:
  caddy-data:

Pin the n8n image to a specific version (for example n8nio/n8n:1.x.y) once you have a working setup, so updates happen when you choose.

Caddyfile:

{$DOMAIN} {
    reverse_proxy n8n:5678
}

Pass the domain to Caddy by adding environment: [DOMAIN=${DOMAIN}] to the caddy service, or write the domain directly in the Caddyfile.

Step 3: Start

docker compose up -d
docker compose logs -f n8n

Open https://n8n.example.com. On first visit, n8n asks you to create the owner account. Do this immediately — until then, anyone who finds the URL could claim it.

Important settings explained

Variable Why it matters
WEBHOOK_URL Webhook URLs shown in the editor use this. Wrong value = webhooks that point to localhost
N8N_PROXY_HOPS=1 Trust one reverse proxy for client IPs and HTTPS detection
GENERIC_TIMEZONE Schedule triggers (Cron node) run in this time zone
EXECUTIONS_DATA_PRUNE / MAX_AGE Delete execution logs older than 336 hours (14 days) so the database does not grow forever
N8N_ENCRYPTION_KEY Encrypts credentials; must stay the same forever

Security

  • Strong owner password and 2FA (available in the user settings).
  • Consider restricting the editor to your IPs while leaving webhooks public. With Caddy:
{$DOMAIN} {
    @editor not path /webhook/* /webhook-test/* /form/*
    handle @editor {
        @blocked not remote_ip 198.51.100.7
        respond @blocked 403
        reverse_proxy n8n:5678
    }
    handle {
        reverse_proxy n8n:5678
    }
}
  • Keep n8n updated — it holds credentials for all your connected services.
  • Workflows can run code (Code node) and shell commands (Execute Command node). Treat editor access like server access.

Connecting AI models

n8n has built-in nodes for OpenAI, Anthropic, Google Gemini, Ollama and others, plus AI Agent nodes that combine a model, memory and tools. Add your API key under Credentials. For a fully private setup, run models locally with Ollama on the same server or another one on your private network and point n8n’s Ollama node at it.

Backups

  1. Database: docker compose exec -T postgres pg_dump -U n8n n8n | gzip > /var/backups/n8n-$(date +%F).sql.gz
  2. .env (contains the encryption key) and compose.yaml.
  3. Workflows as JSON (optional extra): docker compose exec n8n n8n export:workflow --all --output=/files/workflows.json

Schedule them with cron and copy off-site with restic.

Updating

cd /srv/n8n
docker compose pull
docker compose up -d

Read n8n’s release notes before jumping across major versions; database migrations run automatically on start, so take a backup first.

Scaling: queue mode

For many executions, n8n’s queue mode separates the main instance (editor, triggers) from worker containers that execute workflows, coordinated through Redis. Add a Redis service, set EXECUTIONS_MODE=queue and run additional n8n worker containers. Start simple; move to queue mode when executions start to queue up.

Troubleshooting

Problem Fix
Webhook URLs show localhost:5678 Set WEBHOOK_URL and recreate the container
“Credentials could not be decrypted” N8N_ENCRYPTION_KEY changed or missing
Schedules run at the wrong hour GENERIC_TIMEZONE
AI node: region not supported / 403 Server in a region the API does not serve — use Japan, Singapore or US
Database growing fast Enable execution pruning