Quick Guide to Setting Up Cloudflare Tunnel to Expose Your Local Service or Application using Docker

How to establish a secure connection between your local service or application and the internet using Cloudflare Tunnel. This guide will walk you through the steps of leveraging Cloudflare Tunnel with Docker, ensuring your locally hosted service or application is easily accessible securely.

Quick Guide to Setting Up Cloudflare Tunnel to Expose Your Local Service or Application using Docker

Why would you use this?

Security: Cloudflare Tunnel establishes an encrypted connection to locally hosted services or applications without exposing your services directly to the internet
Development and Testing: Ideal for showcasing work, collaboration, or testing APIs, perfect for web development and personal projects
Simple Setup: Easily expose services with Docker, no complex networking is required.


Use Case

You currently host a website on your server at home and have exposed your web ports 80 & 443 on your router. This is currently proxied using Cloudflare. It somewhat provides an additional layer of security but only on the surface.

Before you start, make sure you meet the below requirements

Prerequisites

  • Self-hosted website on your local machine
  • CloudFlare Account
  • Cloudflare website
  • Remove any Port Forwarding rules on ports 80 & 443 that are exposed on your router

Create Tunnel

First, navigate to Cloudflare Dashboard-->Zero Trust– >Network--> Tunnels

 

Add a tunnel in the next step

Add Tunnel

Select a connector. I'm using the recommended one in this case

Name your Tunnel. This could be anything. Make it meaningful for you to identify

Install and run the connector. Now you have a few options you can choose from. I've chosen Docker for my use case.

Create a Docker Compose File

Github Repo: https://github.com/phipcode/phiptechblog/tree/main/cloudflaretunnel

Create your docker-compose.yml file

version: '3.8'

services:
  cloudflared-tunnel:
    image: cloudflare/cloudflared:latest
    container_name: cloudflaretunnel
    command: ["tunnel", "--no-autoupdate", "run", "--token", "${TOKEN}", "--hello-world"]
    env_file:
      - .env

Create environment File (.env)

TOKEN="yourtoken"

Build and Deploy Container

This command builds the Docker image.

First run "docker-compose -build" without the -d parameter. This will run temporarily until you exit the container. Then run it with -d parameter after you confirm it's running

docker-compose up --build -d

Validate Container Status

Check the docker logs to validate again. It should look like the below screenshot

docker logs cloudflaretunnel

Validate Cloudflare Tunnel Status

Now, navigate back to the Cloudflare Tunnels page to confirm it is running. You will see Healthy if it's up.

Create Public Hostname

Next, you'll be setting up the connection to your local host. This is the part where you will be entering your local service info.

Navigate to the tab Public Hostname and click Add a public Hostname

Enter the IP of your localhost e.g. 192.168.0.1:7070 and hit Save

And that's it. Test your website is working. This will automatically add a CNAME record in CloudFlare DNS

If you want to remove the container, you can do so with the below command. Make sure you are inside the correct directory where you created the docker-compose file


docker-compose down