Complete Deployment Guide – n8n with Docker on Ubuntu Server with Cloudflare Tunnel

n8n

This guide shows you how to set up the n8n automation tool at your home for free, using a spare computer or a small device like a Raspberry Pi.

The biggest problem this guide solves is access. Usually, things you run at home are stuck at home. This guide shows you how to securely use your n8n from anywhere in the world like your phone or your laptop at a coffee shop simply by visiting your own private web address.

n8n with Docker on Ubuntu Server with Cloudflare Tunnel

⚙️ Prerequisites (Before You Start)

To follow the tutorials in this category, you’ll need:

🖥️ Hardware

  • An Ubuntu Server (physical or virtual)
  • At least 64 GB storage space (SSD preferred for performance)
  • A stable internet connection

💻 Software & Access

  • Ubuntu Server (latest LTS release) installed and updated
  • SSH access to your server (e.g., via PuTTY, VS Code Remote SSH, or terminal)
  • Sudo privileges (you must be able to run sudo commands)
  • Docker & Docker Compose installed
  • A Cloudflare account (for secure remote access)
  • Basic understanding of:
    • Linux terminal commands
    • Networking and ports
    • Docker basics (containers, volumes)

🧩 STEP 1 — System Preparation

Update and upgrade your Ubuntu Server system:

sudo apt update && sudo apt upgrade -y

Install dependencies:

sudo apt install ca-certificates curl gnupg lsb-release -y

🐳 STEP 2 — Install Docker and Docker Compose

Add Docker’s official repository:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
  https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install Docker Engine & Compose:

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

Test:

sudo docker run hello-world

If you see the success message, Docker is ready ✅

⚙️ STEP 3 — Set Up n8n with Docker Compose

Create a working directory:

mkdir ~/n8n
cd ~/n8n

Create your docker-compose.yml file:

version: "3.7"

services:
  n8n:
    image: n8nio/n8n:latest
    container_name: n8n
    restart: unless-stopped
    ports:
      - "5678:5678"
    environment:
      - GENERIC_TIMEZONE=Asia/Kolkata
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=admin
      - N8N_BASIC_AUTH_PASSWORD=yourpassword
      - N8N_HOST=localhost
      - N8N_PORT=5678
      - WEBHOOK_URL=https://n8n.yourdomain.com/
      - N8N_SECURE_COOKIE=false
    volumes:
      - ./n8n_data:/home/node/.n8n

💡 Replace yourpassword and n8n.yourdomain.com with your own values.

Start n8n:

sudo docker compose up -d

Verify:

sudo docker ps

✅ You should see n8n running on port 5678.

Access locally:

http://<server_ip>:5678

☁️ STEP 4 — Install Cloudflare Tunnel (cloudflared)

Add Cloudflare repository and install:

sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared jammy main" | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt update
sudo apt install cloudflared -y

🔐 STEP 5 — Authenticate Cloudflare Tunnel

Run:

cloudflared tunnel login

This opens a browser → log in to your Cloudflare account → select your domain (e.g., yourdomain.com).

✅ After success, it creates:

~/.cloudflared/cert.pem

🚧 STEP 6 — Create and Configure the Tunnel

Create a tunnel:

cloudflared tunnel create mytunnel

This outputs a UUID and creates a credentials file:

/home/ubuntu/.cloudflared/<UUID>.json

Create the tunnel config file:

nano ~/.cloudflared/config.yml

Paste this:

tunnel: <UUID>
credentials-file: /home/ubuntu/.cloudflared/<UUID>.json

ingress:
  - hostname: n8n.yourdomain.com
    service: http://localhost:5678
  - service: http_status:404

Replace <UUID> and n8n.yourdomain.com with your real values.

Save (Ctrl + O, Enter, Ctrl + X).

🌍 STEP 7 — Create the Public DNS Record Automatically

Run:

cloudflared tunnel route dns mytunnel n8n.yourdomain.com

✅ This step:

  • Automatically creates a CNAME record in Cloudflare DNS:
  • n8n.yourdomain.com<UUID>.cfargotunnel.com

This connects your domain to the Cloudflare network.

No need to worry about your ISP IP or DDNS — Cloudflare handles it completely.

🧠 STEP 8 — Run the Tunnel as a Background Service

Test manually first:

cloudflared tunnel run mytunnel

If it works → stop it (Ctrl + C).

Then install as a system service:

sudo cloudflared service install

Enable it to start at boot:

sudo systemctl enable cloudflared
sudo systemctl start cloudflared
sudo systemctl status cloudflared

🔁 STEP 9 — Make Sure Docker Auto-Restarts on Boot

n8n’s restart: unless-stopped handles that automatically.

You can confirm:

sudo docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' n8n

If it shows:

unless-stopped

✅ it will auto-restart after reboot.

🔄 STEP 10 — Verify After Reboot

Reboot your server:

sudo reboot

After it comes up:

sudo systemctl status cloudflared
sudo docker ps

✅ You should see both n8n and cloudflared running.

Visit:

https://n8n.yourdomain.com

It should open securely via HTTPS, even if your public IP changed.

🧰 TROUBLESHOOTING

Problem Likely Cause Fix
cloudflared fails to start Wrong UUID or file path Check config.yml and .json file
n8n not loading Container crashed Run sudo docker logs n8n
Domain not resolving DNS not created Run cloudflared tunnel route dns mytunnel n8n.yourdomain.com again
502 Bad Gateway Wrong service URL Verify localhost:5678 is correct
SSL issues Cloudflare SSL/TLS mode Set to “Full” or “Full (strict)” in Cloudflare dashboard

💡 BONUS — Optional: Auto HTTPS Redirect

In your Cloudflare dashboard:

  • Go to Example: SSL/TLS → Edge Certificates
  • Enable Always Use HTTPS
  • Enable Automatic HTTPS Rewrites

✅ This ensures http://n8n.yourdomain.com automatically redirects to https://n8n.yourdomain.com.

✅ FINAL CHECKLIST

Task Status
Docker installed and running
n8n running in Docker
Cloudflared installed and authenticated
Tunnel created and linked
CNAME record created automatically
Cloudflared running as a service
Survives reboot
No DDNS needed (ISP IP changes irrelevant)

Features of n8n.

Click here to get templates to start your n8n journey.

Picture of Altamash Shaikh
Altamash Shaikh
I’m Altamash Shaikh, a passionate Product Manager with a keen interest in technology. In my free time, I enjoy writing blog posts about the latest tech trends and innovations. As a tech enthusiast, I thrive on exploring new advancements and sharing insights with others. Join me on my journey through the ever-evolving world of tech!

Leave a Comment