FOSSFEEDWhere activity matters more than stars. Discover Fire Pit Noted Submit a project
← All articles

How to Set Up Cloudflare Tunnel for Self-Hosting

A step-by-step guide to setting up Cloudflare Tunnel with Docker and connecting your self-hosted applications to your own domain.

Cloudflare has been one of my favorite tools for managing domains and accessing self-hosted applications for years. When I first started using it, I was mainly interested in its DNS management and security features. Eventually, I started using Cloudflare Tunnel to make my websites accessible from outside my home network without opening ports on my router.

In this guide, I'll walk you through connecting a domain to Cloudflare, creating your first tunnel, and installing cloudflared using Docker. We'll use a self-hosted Ghost website as our example, but the same process works with plenty of other applications.

If you've been following my getting started with self-hosting guides, you may already have Docker installed and a website running on your server. If so, you're ready to get started.

What Is Cloudflare Tunnel? #

Cloudflare Tunnel allows you to make applications running on your home server accessible through your own domain without traditional port forwarding.

Normally, hosting a website from home involves opening ports on your router and directing incoming traffic to the server hosting your application. Cloudflare Tunnel handles things differently.

A lightweight application called cloudflared establishes an outbound connection from your server to Cloudflare. Visitors connect through Cloudflare, which forwards their requests through the tunnel to your application.

This also means visitors accessing your website through the tunnel don't need to connect directly to your home's public IP address.

One thing worth mentioning is that Cloudflare Tunnel and Cloudflare Access are separate features. A tunnel allows traffic to reach your application, while Access can restrict who is allowed to connect. We'll discuss that further down.

Purchase a Domain Name #

Before we do anything with Cloudflare, you'll need a domain name.

I recommend Porkbun for purchasing domains. I've used them for years and have been happy with the service. You're welcome to use another registrar if you prefer, since the setup process is pretty similar.

Once you've purchased a domain, you'll need to connect it to Cloudflare.

If you already have a domain managed through Cloudflare, you can skip ahead to creating your first tunnel.

Register a Free Cloudflare Account #

Head over to Cloudflare and register for a free account.

Once you've verified your email address, you'll be able to add your domain to Cloudflare.

Cloudflare has several paid services, but its free plan is enough for the basic setup we're covering in this guide. You don't need to purchase a subscription just to create a standard Cloudflare Tunnel.

Add Your Domain to Cloudflare #

After signing in to the Cloudflare dashboard, choose the option to add an existing domain.

Enter the domain you purchased and follow the instructions. Cloudflare will scan for existing DNS records and give you an opportunity to review them.

Select the Free plan when prompted.

During setup, Cloudflare will provide two nameservers that you'll need to configure at your domain registrar.

Since I use Porkbun, I'll explain how this works there.

Sign in to Porkbun, find your domain, and open its nameserver settings. Replace the existing authoritative nameservers with the two provided by Cloudflare.

Save your changes and return to Cloudflare.

Cloudflare will check whether your domain is using the correct nameservers. This process can take a little while, so don't worry if the domain doesn't become active immediately.

Once everything is confirmed, Cloudflare will notify you that your domain is ready.

A Quick Note About DNS Records #

Before moving forward, take a moment to review the DNS records Cloudflare imported.

You might see records from your domain registrar or services you've configured previously. Don't delete everything just because you don't recognize it.

For example, if you use your domain for email, removing the wrong DNS records could interrupt email delivery.

Cloudflare's DNS dashboard will be where you manage these records going forward.

Also, keep in mind that simply changing nameservers doesn't automatically hide your home IP address. That's one of the reasons we're setting up Cloudflare Tunnel.

Create Your First Cloudflare Tunnel #

Now we can get to the fun part.

Open the Cloudflare dashboard and navigate to Networking → Tunnels.

Cloudflare moved Tunnel management into its main dashboard, so this is different from the older Zero Trust instructions you might find in tutorials written a few years ago.

Click Create Tunnel and give your tunnel a name.

I recommend something simple that identifies the server or purpose. For example, you might name it homelab if you're planning to use it for multiple applications on your home server.

After creating the tunnel, Cloudflare will guide you through installing the connector.

For this tutorial, we'll use Docker.

Install Cloudflared Using Docker Compose #

Cloudflare provides a Docker command containing a unique tunnel token. This token allows your cloudflared container to establish a connection with your Cloudflare account.

You can use the command Cloudflare generates, but since I prefer Docker Compose, we'll set it up that way instead.

Create a new folder for your Cloudflare Tunnel installation and add the following compose.yaml file.

services:
  cloudflared:
    image: cloudflare/cloudflared:latest
    container_name: cloudflared
    restart: unless-stopped
    command: tunnel --no-autoupdate run
    environment:
      TUNNEL_TOKEN: ${TUNNEL_TOKEN}

You'll also need to provide your tunnel token.

Create a file named .env in the same directory as your Compose file.

TUNNEL_TOKEN=YOUR_CLOUDFLARE_TUNNEL_TOKEN

Replace the placeholder with the actual token Cloudflare provided during tunnel creation.

Important: Keep your token private. Anyone who obtains it may be able to connect another cloudflared instance to your tunnel.

If you use Portainer, you can paste the Compose configuration into a new stack and enter TUNNEL_TOKEN in the stack's environment variables instead.

Deploy the stack or run the following command from the directory containing your Compose file:

docker compose up -d

Docker will download the Cloudflare image and start the container.

Once it's running, return to Cloudflare and check the status of your tunnel.

If everything is working, Cloudflare should show that your connector is connected.

You now have a working tunnel between your server and Cloudflare. We just need to tell Cloudflare which application should receive incoming traffic.

Connect Your Self-Hosted Website #

Select your tunnel from the Cloudflare dashboard and open its Routes tab.

Click Add route, then select Published application.

This replaces the older Public Hostname setup shown in many previous Cloudflare tutorials.

You'll be asked to enter a hostname and service URL.

For example, suppose you own example.com and want your Ghost website accessible at blog.example.com.

You would configure the hostname using:

  • Subdomain: blog
  • Domain: Your domain from the dropdown menu
  • Service URL: The internal address of your Ghost installation

If Ghost is running on your home server at 192.168.1.100 using port 2368, your service URL would be:

http://192.168.1.100:2368

Replace that address with the actual address and port of your application.

If Ghost and Cloudflared are connected to the same Docker network, you can also use Ghost's Docker service name and internal port instead of the server's LAN IP address.

One thing to remember is that localhost inside the Cloudflared container refers to that container itself. It won't automatically reach another Docker container running on the same server.

Once you've entered the correct information, save your route.

Cloudflare will associate the hostname with your tunnel and create the necessary DNS routing configuration.

You can now try visiting your domain in a browser.

If everything is configured correctly, your self-hosted website should load through Cloudflare using HTTPS.

And the best part? You didn't have to open any incoming ports on your router.

Update Your Ghost URL #

If you're using Ghost for this tutorial, there's one more setting you should check.

Ghost needs to know the public URL visitors will use to access your website.

If you installed Ghost using Docker Compose, you'll likely have a url environment variable in your stack.

Make sure it points to the HTTPS address you configured in Cloudflare.

For example:

environment:
  url: https://blog.example.com

Replace the example domain with your own.

After saving your changes, redeploy or recreate your Ghost container so the updated environment variable takes effect.

If you skip this step, you may run into problems with redirects or links pointing to your server's internal IP address.

Adding More Applications to Your Tunnel #

Now that your first application is working, you might be wondering if you'll need a separate tunnel for every service you host.

Thankfully, you don't.

Cloudflare allows you to publish multiple applications through the same tunnel.

For example, you could have your Ghost website using one subdomain and another application using a different subdomain, with both connecting through the same Cloudflared container.

Simply return to your tunnel's Routes tab and create another published application route.

Each hostname can point to a different internal IP address or port.

I've found this particularly convenient when managing multiple applications on the same Docker host.

Just remember that publishing an application makes it reachable through that hostname unless you've configured additional restrictions.

Secure Applications With Cloudflare Zero Trust #

This is an important part of the setup that I don't want anyone overlooking.

Cloudflare Tunnel doesn't automatically protect an application's login page from public access.

If you create a published application route to something like Portainer, visitors may still be able to reach its login page unless you restrict access.

That's where Cloudflare Access comes in.

Cloudflare Access allows you to create authentication policies for applications before visitors can reach them.

For example, you can require someone to authenticate using an approved identity before Cloudflare forwards their request to your self-hosted application.

I generally recommend this for administrative interfaces that have no reason to be publicly accessible.

You can follow my separate tutorial on how to protect self-hosted login pages using Cloudflare Zero Trust policies.

Keep in mind that some applications use desktop clients or mobile apps that may not work properly with browser-based Access authentication. Always test your application's functionality before enabling an additional login requirement.

I've also written a separate article explaining how secure Cloudflare Tunnel actually is for self-hosting, including some limitations worth understanding.

Cloudflare Tunnel Upload Limits #

There's one limitation that deserves mentioning, particularly if you're planning to use Cloudflare Tunnel with applications that transfer large files.

Cloudflare applies HTTP request upload limits based on your account plan.

As of this update, Cloudflare's Free and Pro plans allow request bodies up to 100 MB, while the Business plan allows up to 200 MB. Enterprise accounts can configure higher limits.

These limits matter for applications that accept large uploads. For example, you might encounter an HTTP 413 error if you attempt to upload a file exceeding your account's limit.

Cloudflare Tunnel doesn't automatically bypass these restrictions.

If you're planning to expose a file-hosting application, check the current Cloudflare upload limits before relying on a tunnel for large transfers.

Troubleshooting Cloudflare Tunnel #

If your website isn't loading after completing the setup, there are a few things worth checking.

Tunnel shows as disconnected

First, make sure the Cloudflared container is running.

You can check its status using Portainer or Docker:

docker logs cloudflared

Look for connection errors or messages indicating that Cloudflared cannot reach Cloudflare.

If you're using a restrictive firewall, Cloudflared needs outbound connectivity to Cloudflare, including port 7844.

Cloudflare displays a 502 error

This usually means Cloudflare can reach the tunnel connector, but Cloudflared cannot successfully connect to the application behind it.

Double-check the Service URL configured in your published application route.

Make sure the IP address and port are correct and that the application is actually running.

If you're using Docker service names, confirm that the containers share a Docker network.

Ghost redirects to an internal IP address

Check the url environment variable in your Ghost installation.

It should match the public HTTPS hostname configured in Cloudflare.

The website loads, but some features don't work

Check your application's logs and configuration. Some services require additional settings when operating behind a reverse proxy or tunnel.

If your origin uses HTTPS, you may also need to adjust the origin TLS settings in Cloudflare to match the certificate presented by your application.

Avoid disabling certificate verification without understanding why the connection is failing.

For more troubleshooting information, Cloudflare maintains an extensive collection of Tunnel documentation.

What to Do Next #

Now that you've created your first Cloudflare Tunnel, you can start using your domain to access applications running on your home server.

Before publishing additional services, I recommend reviewing which applications genuinely need to be accessible from the internet.

A public website like Ghost is one thing, but administrative dashboards deserve additional protection.

If you haven't already, take a look at my guide on protecting self-hosted login pages with Cloudflare Zero Trust. It's a good next step after getting your first tunnel working.

You might also find my article on Cloudflare Tunnel security helpful if you're interested in understanding the trade-offs involved.

Final Notes and Thoughts #

Cloudflare Tunnel has made managing public access to my self-hosted applications much easier. I like being able to use my own domains without configuring router port forwarding for every service I want to make available.

For anyone just getting started with self-hosting, I think it's a convenient option worth exploring.

The setup is fairly straightforward once you've gone through it the first time, and adding another application usually takes only a minute or two.

Just remember that Cloudflare Tunnel is responsible for connecting visitors to your application. Keeping that application secure is still your responsibility.

Comments 0

Join the conversation.

Write a comment

No comments yet. Be the first to comment.

Leave a comment

Anonymous comments are reviewed before publication. Sign in to comment as a member.

Navigation