Setting up N8N in Docker

installing Docker

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt install python3-setuptools

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-compose

creating docker compose for n8n and its database setup

mkdir n8n
cd n8n

nano docker-compose.yml

and add following content

version: '3'

services:
  n8n:
    image: n8nio/n8n
    restart: always
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      - postgres

  postgres:
    image: postgres:13
    restart: always
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  n8n_data:
  postgres_data:

creating .env file

nano .env

add following content replace pasword etc

POSTGRES_DB=n8n
POSTGRES_USER=n8nuser
POSTGRES_PASSWORD=your_strong_password

Start the n8n Container

docker-compose up -d

verify if containers are running

docker ps

seeting up caddy

Of course. Here are the commands and steps to install Caddy and configure it as a reverse proxy for your self-hosted n8n instance.

This guide assumes you are using a Debian-based Linux distribution like Ubuntu, which is very common for this type of setup.

Step 1: Install Caddy

The recommended way to install Caddy is to use the official Caddy repository. This ensures you get automatic updates and a proper systemd service for running Caddy in the background.

  1. Add the Caddy repository’s GPG key:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg

  1. Add the repository itself:
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
  1. Update your package list and install Caddy:
sudo apt update
sudo apt install caddy

Caddy is now installed and should be running automatically as a service. You can check its status with:

systemctl status caddy

Step 2: Configure Caddy as a Reverse Proxy for n8n

Caddy’s configuration is managed through a simple file called the Caddyfile. The default location for this file is /etc/caddy/Caddyfile.

  1. Open the Caddyfile for editing: Use a text editor like nano to edit the file. You will need sudo to modify it.
sudo nano /etc/caddy/Caddyfile
  1. Add your n8n configuration: Delete any existing content in the file and replace it with the following block. Make sure to replace your-domain.com with your actual domain name.
Code snippetyour-domain.com {
    ### Caddy will automatically provision and renew an SSL certificate for your domain.
    #tls internal # enable for self signed ssl
    ### Reverse proxy requests to the n8n container
    ### This assumes you are running n8n on the same machine and it's listening on port 5678.
    reverse_proxy 127.0.0.1:5678
}

`What this configuration does:

  • your-domain.com: This tells Caddy which domain to listen for.
  • reverse_proxy 127.0.0.1:5678: This is the core instruction. It tells Caddy to forward all incoming traffic for your-domain.com to your n8n application, which is running locally on port 5678.
  • Automatic HTTPS: Caddy will automatically obtain a free SSL certificate from Let’s Encrypt for your domain, so your connection will be secure (https://).
  1. Save and close the file: If you’re using nano, press Ctrl+X, then Y, and then Enter.

Step 3: Reload Caddy to Apply the Configuration

After saving your Caddyfile, you need to tell Caddy to load the new configuration.

  1. Reload the Caddy service:
sudo systemctl reload caddy

This command gracefully reloads Caddy without any downtime.
3. Check for errors (optional but recommended): If you want to check your configuration for syntax errors before reloading, you can run:

sudo caddy fmt --overwrite /etc/caddy/Caddyfile && sudo caddy validate --config /etc/caddy/Caddyfile

Step 4: Final n8n Configuration

For webhooks and other features to work correctly, you must tell your n8n instance its public-facing URL.

  1. Set the WEBHOOK_URL environment variable:
    Go back to the directory where you have your docker-compose.yml file and edit your .env file (nano .env). Add the following line, again replacing your-domain.com with your domain:WEBHOOK_URL=https://your-domain.com/Your full .env file might look like this now:POSTGRES_DB=n8n POSTGRES_USER=n8nuser POSTGRES_PASSWORD=your_strong_password WEBHOOK_URL=https://your-domain.com/
  2. Restart your n8n container to apply the new environment variable:
docker-compose down && docker-compose up -d

That’s it! You can now access your secure n8n instance by navigating to https://your-domain.com in your web browser. Caddy will handle the SSL certificate and securely proxy all requests to your n8n application.