Post

Running n8n in ngrok

Running n8n in ngrok

Substack Image

Install Docker and Docker Compose

1
2
3
4
5
6
7
mkdir -p ~/.docker/cli-plugins/

curl -SL https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose

chmod +x ~/.docker/cli-plugins/docker-compose

docker compose version

DNS Setup

Create an .env file

1
2
3
mkdir n8n-compose
cd n8n-compose
nano .env

Paste the following:

# DOMAIN_NAME and SUBDOMAIN together determine where n8n will be reachable from

# The top level domain to serve from
DOMAIN_NAME=[your ngrok domain]

# The subdomain to serve from
SUBDOMAIN=[your ngrok subdomain]

# Example: serves n8n at https://n8n.example.com

# Optional timezone for Cron and other scheduling nodes
# New York is default if not set
GENERIC_TIMEZONE=Europe/Berlin

# Email address to use for TLS/SSL certificate creation
SSL_EMAIL=user@example.com

Example values: DOMAIN_NAME=ngrok-free.app SUBDOMAIN=close-especially-opossum


Create local files directory

1
mkdir local-files

Create docker-compose.yml

1
nano docker-compose.yml

Paste the following YAML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
services:
  traefik:
    image: "traefik"
    restart: always
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.web.http.redirections.entryPoint.to=websecure"
      - "--entrypoints.web.http.redirections.entrypoint.scheme=https"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.mytlschallenge.acme.tlschallenge=true"
      - "--certificatesresolvers.mytlschallenge.acme.email=${SSL_EMAIL}"
      - "--certificatesresolvers.mytlschallenge.acme.storage=/letsencrypt/acme.json"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - traefik_data:/letsencrypt
      - /var/run/docker.sock:/var/run/docker.sock:ro

  n8n:
    image: docker.n8n.io/n8nio/n8n
    restart: always
    ports:
      - "127.0.0.1:5678:5678"
    labels:
      - traefik.enable=true
      - traefik.http.routers.n8n.rule=Host(`${SUBDOMAIN}.${DOMAIN_NAME}`)
      - traefik.http.routers.n8n.tls=true
      - traefik.http.routers.n8n.entrypoints=web,websecure
      - traefik.http.routers.n8n.tls.certresolver=mytlschallenge
      - traefik.http.middlewares.n8n.headers.SSLRedirect=true
      - traefik.http.middlewares.n8n.headers.STSSeconds=315360000
      - traefik.http.middlewares.n8n.headers.browserXSSFilter=true
      - traefik.http.middlewares.n8n.headers.contentTypeNosniff=true
      - traefik.http.middlewares.n8n.headers.forceSTSHeader=true
      - traefik.http.middlewares.n8n.headers.SSLHost=${DOMAIN_NAME}
      - traefik.http.middlewares.n8n.headers.STSIncludeSubdomains=true
      - traefik.http.middlewares.n8n.headers.STSPreload=true
      - traefik.http.routers.n8n.middlewares=n8n@docker
    environment:
      - N8N_HOST=${SUBDOMAIN}.${DOMAIN_NAME}
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - NODE_ENV=production
      - WEBHOOK_URL=https://${SUBDOMAIN}.${DOMAIN_NAME}/
      - GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
    volumes:
      - n8n_data:/home/node/.n8n
      - ./local-files:/files

volumes:
  n8n_data:
  traefik_data:

Start Docker Compose

1
sudo docker compose up -d
  • If it doesn’t work, try running without sudo.
  • To stop the containers:
1
sudo docker compose stop

Download ngrok

Substack Image

  1. Login to ngrok.com and download the zip file for Windows.
  2. Extract it to C:\Program Files\ngrok.
  3. Add the location to your System PATH.
  4. Verify installation:
ngrok --version

Running ngrok Server

1
ngrok http --url=[your ngrok web url] 5678
  • Use port 5678 because n8n is configured on this port.
  • Authorize with your ngrok token:
1
ngrok config add-authtoken [paste your token]
  • Start the server again:
1
ngrok http --url=[your ngrok web url] 5678

You will now see the n8n login page at your ngrok web URL.

Note: Start the Docker instance first before running ngrok.

This post is licensed under CC BY 4.0 by the author.