Skip to content
codewithanjum
n8n

How to self-host n8n with Docker (the setup I actually run)

Self-host n8n with Docker Compose: a working compose file, timezone and webhook settings, Cloudflare Tunnel for safe webhooks, plus backups and updates.

By Md Anjum Kamali · · 5 min read

Painting of a small dark mini PC with a glowing orange light, sitting on the floor under a wooden desk with a plant and books
On this page
  1. What do you need before you start?
  2. The docker-compose file
  3. What do the environment variables do?
  4. Do you need a reverse proxy?
  5. How do you expose n8n webhooks safely?
  6. How do you back up self-hosted n8n?
  7. How do you update n8n in Docker?
  8. When should you use Postgres instead of SQLite?
  9. Where to go from here
  10. Questions people ask

To self-host n8n with Docker, run the official n8nio/n8n image with Docker Compose, mount a volume at /home/node/.n8n so your data survives restarts, and set the timezone and webhook URL environment variables. Then put it behind a reverse proxy or a Cloudflare Tunnel so webhooks can reach it over HTTPS. The whole thing takes about twenty minutes on any Linux box with Docker installed.

This is the setup I run on a used Lenovo ThinkCentre Tiny under my desk. The hardware side is in my mini PC home server post. This post is just n8n.

Self-hosted n8n is the free community edition of n8n running on your own machine instead of n8n's cloud, so you pay for the server and nothing per workflow run.

What do you need before you start?

  1. A Linux machine or VPS with Docker and the Docker Compose plugin installed. Ubuntu Server is what I use.
  2. A domain on Cloudflare, if you want webhooks reachable from the internet. Skip this if n8n only needs to work on your home network.
  3. Ten minutes to think about backups. Really.

The docker-compose file

Make a folder and put this in docker-compose.yml. It's based on the compose example in the n8n Docker Compose docs, trimmed for a home server and with a Cloudflare Tunnel container in place of Traefik.

services:
  n8n:
    image: n8nio/n8n:latest
    restart: always
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      - N8N_HOST=${SUBDOMAIN}.${DOMAIN_NAME}
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - N8N_WEBHOOK_URL=https://${SUBDOMAIN}.${DOMAIN_NAME}/
      - NODE_ENV=production
      - GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
      - TZ=${GENERIC_TIMEZONE}
      - N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
      - N8N_RESTRICT_FILE_ACCESS_TO=/files
    volumes:
      - n8n_data:/home/node/.n8n
      - ./local-files:/files

  cloudflared:
    image: cloudflare/cloudflared:latest
    restart: always
    command: tunnel --no-autoupdate run --token ${CF_TUNNEL_TOKEN}
    depends_on:
      - n8n

volumes:
  n8n_data:

And a .env file next to it:

DOMAIN_NAME=example.com
SUBDOMAIN=n8n
GENERIC_TIMEZONE=Europe/Berlin
CF_TUNNEL_TOKEN=paste-your-tunnel-token-here

Change the timezone to yours. It uses the standard tz database names like Europe/London or America/New_York.

Create the local-files folder yourself before the first start (mkdir local-files), so it gets the right owner. Then run:

docker compose up -d
docker compose logs -f n8n

Open http://localhost:5678 on the machine, or the public hostname once the tunnel is set up, and create the owner account.

What do the environment variables do?

Variable What it does
N8N_HOST, N8N_PROTOCOL, N8N_PORT Tell n8n the address it's served on, so links in the editor are right
N8N_WEBHOOK_URL The public base URL n8n uses when it shows webhook URLs. On versions before 2.30.0 the name is WEBHOOK_URL
GENERIC_TIMEZONE Timezone for the Schedule Trigger and other time-based nodes. Defaults to New York if unset
TZ The container's system timezone, so logs and dates match
N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS Locks down permissions on n8n's config file
N8N_RESTRICT_FILE_ACCESS_TO Limits file read and write nodes to the /files folder

The webhook URL is the one people get wrong. Without it, n8n shows webhook URLs that start with http://localhost:5678, and anything you paste that into from outside fails. The rename to N8N_WEBHOOK_URL is recent, so if you're pinned to an older image, use the old name.

The 127.0.0.1:5678 port binding means n8n is only reachable from the machine itself. The tunnel or reverse proxy is the only way in from outside, which is what you want.

Do you need a reverse proxy?

On a home network, it's nice but optional. I use Nginx so I can type n8n.local instead of an IP and a port. If you do the same, remember that the n8n editor keeps a live connection open for updates, so pass the upgrade headers through:

server {
    listen 80;
    server_name n8n.local;

    location / {
        proxy_pass http://127.0.0.1:5678;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_buffering off;
    }
}

On a VPS with a public IP, the n8n compose example uses Traefik with automatic Let's Encrypt certificates instead. That works well if you'd rather not touch Cloudflare.

How do you expose n8n webhooks safely?

Your signup form, Stripe, or any outside service needs to reach your webhook URL. The bad way is opening port 5678 on your router. The better way is a tunnel.

Cloudflare Tunnel runs a small connector on your machine that dials out to Cloudflare. Traffic comes in through Cloudflare's network and down that connection, so no inbound ports are open. Here's the setup, following Cloudflare's tunnel docs:

  1. In the Cloudflare dashboard, go to Networking, then Tunnels, and create a tunnel.
  2. Pick Docker as the environment and copy the token from the command it shows you. Paste it into CF_TUNNEL_TOKEN in your .env.
  3. Add a route of type Published application. Set the subdomain to n8n, pick your domain, and set the service URL to http://n8n:5678.
  4. Run docker compose up -d again. The tunnel should show as Healthy.

Note the service URL in step 3. Cloudflare's docs use localhost, but cloudflared runs in its own container here, so it reaches n8n by the compose service name.

A public hostname means the editor login page is public too. A few things I'd do:

  1. Use a long, unique password on the owner account.
  2. Put the editor behind Cloudflare Access so only your email can reach it, and if you do, give webhooks a separate hostname that skips Access.
  3. Turn on authentication in the Webhook node (header auth or basic auth) for any webhook that triggers something that matters.

How do you back up self-hosted n8n?

Everything lives in the n8n_data volume: the SQLite database with your workflows and execution history, and the encryption key that protects your saved credentials. Lose the key and your credentials can't be decrypted, even if you still have the database.

I do two kinds of backup.

Export workflows and credentials with the CLI

n8n has built-in export commands. The --backup flag writes one file per workflow or credential:

docker compose exec n8n n8n export:workflow --backup --output=/files/backups/latest/
docker compose exec n8n n8n export:credentials --backup --output=/files/backups/latest/

Because /files is mounted to ./local-files, the export lands on the host where you can copy it elsewhere. Credentials export encrypted by default, so they need the same encryption key to import.

Snapshot the whole volume

docker compose stop n8n
docker run --rm -v n8n_n8n_data:/data -v "$(pwd)":/backup alpine \
  tar czf /backup/n8n_data-$(date +%F).tar.gz -C /data .
docker compose start n8n

Compose prefixes the volume name with the folder name, so if your folder is called n8n, the volume is n8n_n8n_data. Run docker volume ls to check. Copy the tarball off the machine. A backup on the same SSD as the thing it's backing up only covers half the problems.

How do you update n8n in Docker?

Back up first, then:

docker compose pull
docker compose down
docker compose up -d

n8n publishes a stable line and a beta line, and the docs recommend stable for production. The latest tag follows stable and next follows beta, so stay off next unless you like surprises. If you want to control upgrades exactly, pin a version number like n8nio/n8n:2.45.7 and bump it after reading the release notes. Big version jumps sometimes rename settings, like the webhook variable above, so skim the notes before you pull.

When should you use Postgres instead of SQLite?

The default SQLite database is fine for one person running a handful of workflows. If you run lots of executions, want queue mode with workers, or already have Postgres running, switch with the DB_TYPE=postgresdb and DB_POSTGRESDB_* variables. Keep the .n8n volume mounted even then, because the encryption key still lives there.

Where to go from here

Once it's running, build something small that matters to you. My first real one was a signup alert workflow: a webhook, a Set node, a Google Sheets row and a Gmail alert. If you're still deciding whether self-hosting is worth it at all, my n8n vs Zapier vs Make comparison covers the trade-offs.

Questions people ask

How much RAM does self-hosted n8n need?

Less than you'd think for normal workflows. I run n8n in Docker on a mini PC with 8GB of RAM alongside Postgres, Redis and a small local AI model. Heavy workflows that load big files or run many in parallel need more.

Why do my n8n webhook URLs show localhost?

n8n builds webhook URLs from its own settings, so behind a proxy or tunnel it doesn't know its public address. Set N8N_WEBHOOK_URL (WEBHOOK_URL on versions before 2.30.0) to your public HTTPS address and restart the container.

Is it safe to expose self-hosted n8n to the internet?

It can be, if you don't open router ports and you protect the editor. Use a tunnel like Cloudflare Tunnel, keep n8n's owner account on a strong password, and add authentication to any Webhook node that triggers something sensitive.

How do I update n8n in Docker?

Run docker compose pull, then docker compose down and docker compose up -d. Your workflows and credentials live in the mounted volume, so they survive the container being replaced. Back up the volume first.

Keep reading