Skip to content

Here's a step-by-step guide on how to host the Cloudflare Tunnel Docker image on TrueNAS SCALE and use it to expose your self-hosted applications without requiring port forwarding.


Hosting Cloudflare Tunnel on TrueNAS SCALE

This guide will walk you through deploying Cloudflare Tunnel (Cloudflared) as a Docker container on TrueNAS SCALE. This allows you to securely expose your self-hosted services without opening ports on your router.


1. Prerequisites

  • TrueNAS SCALE 24.10 (or latest)
  • Cloudflare Account
  • A Cloudflare-protected domain
  • Docker Installed on TrueNAS SCALE
  • Access to Cloudflare Zero Trust Dashboard

2. Setup Cloudflare Tunnel on Cloudflare Dashboard

  1. Log in to Cloudflare Zero Trust:
  2. Go to Cloudflare Zero Trust Dashboard.
  3. Navigate to Access β†’ Tunnels and click Create a tunnel.

  4. Create a New Tunnel:

  5. Name your tunnel (e.g., truenas-tunnel).
  6. Select Docker as your environment.

  7. Generate the Cloudflare Configuration File:

  8. Cloudflare will give you a command similar to:

    docker run -v /path/to/cloudflared:/etc/cloudflared cloudflare/cloudflared:latest tunnel --no-autoupdate run
    
  9. Copy the tunnel token that is generated.


3. Deploy Cloudflare Tunnel as a Docker Container on TrueNAS

Option 1: Using TrueNAS SCALE UI

  1. Go to Apps β†’ Launch Docker Image
  2. Enter Image Details:
  3. Image Repository: cloudflare/cloudflared:latest
  4. App Name: cloudflare-tunnel

  5. Set Environment Variables:

  6. Add the following environment variable:
    TUNNEL_TOKEN=<your-cloudflare-tunnel-token>
    
  7. Replace <your-cloudflare-tunnel-token> with the token copied earlier.

  8. Mount a Persistent Volume:

  9. Mount a dataset for Cloudflare’s configuration:

    Host Path: /mnt/data/cloudflared
    Container Path: /etc/cloudflared
    

  10. Network Settings:

  11. Set the networking mode to host.

  12. Deploy the Container by clicking "Save & Deploy".


Option 2: Using TrueNAS SCALE CLI

If you prefer the command line, SSH into your TrueNAS server and run:

docker run -d --name cloudflare-tunnel \
  --restart unless-stopped \
  -e TUNNEL_TOKEN=<your-cloudflare-tunnel-token> \
  -v /mnt/data/cloudflared:/etc/cloudflared \
  cloudflare/cloudflared:latest tunnel run

4. Exposing Self-Hosted Applications

Once the Cloudflare tunnel is running, you need to configure routes for your self-hosted applications.

Add an Application Route in Cloudflare

  1. Go to Cloudflare Zero Trust Dashboard β†’ Access β†’ Tunnels.
  2. Select the tunnel you created (truenas-tunnel).
  3. Click Public Hostname β†’ Add a Public Hostname.
  4. Subdomain: app
  5. Domain: yourdomain.com
  6. Service: http://192.168.1.100:3000 (Change to your actual service IP and port)
  7. Click Save.

5. Verify Everything Works

  1. Run the following command inside the container to check logs:
    docker logs -f cloudflare-tunnel
    
  2. Open a browser and visit https://app.yourdomain.com.
  3. You should see your self-hosted application without needing to open any ports!

6. Additional Configurations

Updating the Cloudflare Tunnel Container

To update the cloudflared container:

docker pull cloudflare/cloudflared:latest
docker stop cloudflare-tunnel
docker rm cloudflare-tunnel
docker run -d --name cloudflare-tunnel \
  --restart unless-stopped \
  -e TUNNEL_TOKEN=<your-cloudflare-tunnel-token> \
  -v /mnt/data/cloudflared:/etc/cloudflared \
  cloudflare/cloudflared:latest tunnel run

Running Cloudflare Tunnel as a Systemd Service (Optional)

If you want to run cloudflared outside Docker, you can install it directly on TrueNAS SCALE:

curl -fsSL https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o /usr/local/bin/cloudflared
chmod +x /usr/local/bin/cloudflared
cloudflared service install <your-cloudflare-tunnel-token>
systemctl start cloudflared
systemctl enable cloudflared

7. Summary

βœ… No need for port forwarding
βœ… Secure connection through Cloudflare
βœ… Easily expose multiple self-hosted apps
βœ… Automatic SSL with Cloudflare

You can now securely serve TrueNAS Web UI, Home Assistant, Gitea, and any other self-hosted applications using Cloudflare Tunnel! πŸš€