Overview
Exposing your local network services to the public is a common requirement. For example, you might want to access the qBittorrent management web interface to launch new download tasks while away from home. To achieve this, you can use Cloudflare Tunnel, a mostly free and secure solution! This post will guide you through setting up Cloudflare Tunnel step by step.
Prerequisite
- You have a registered domain name (e.g.,
nicklin.work). - You have a Cloudflare account.
- Your domain is managed through Cloudflare.
- You have
dockerinstalled on your local server.
Guide
-
Firstly, create a directory to store all required files for
cloudflared.cloudflaredis a command-line tool to manage Cloudflare resources. We will use it to create and set up a tunnel. Replace/somewhere/to/place/your/fileswith the actual path on your local server.bashmkdir -p /somewhere/to/place/your/files chmod 777 /somewhere/to/place/your/files
-
Next, use a Docker container to run
cloudflared. Here, I use a small trick: aliasingcloudflaredas a Docker command. This is very convenient since we will use this command multiple times. I’m using the latest version at the time of writing this post. You can find the image you prefer here.bashdocker pull cloudflare/cloudflared:1759-f9c2bd51ae24 alias cloudflared="docker run --rm -it -v /somewhere/to/place/your/files:/home/nonroot/.cloudflared cloudflare/cloudflared:1759-f9c2bd51ae24"
-
Log in to your Cloudflare account:
bashcloudflared loginYour terminal will likely display the following:
Open the link in your browser and log in. After that, you should see a cert.pemfile in yourcloudflaredfolder. -
Create a tunnel with the following command:
bashcloudflared tunnel create YourTunnelNameYou should see a result like this. Copy the ID:
Created tunnel YourTunnelName with id some-random-id-xxxxxxxxxxxx-xxxxx
-
To access your local network from the public network, you need a domain name and must configure it to resolve to your tunnel. For example, use
tunnel.test.com. Run the following command to let Cloudflare resolve the domain to a specific tunnel. Ensure you have management access totest.comon Cloudflare first!bashcloudflared tunnel route dns YourTunnelName tunnel.test.comThis command creates a
CNAMErecord pointingtunnel.test.comto the tunnel’s domain, which is usually:some-random-id-xxxxxxxxxxxx-xxxxx.cfargotunnel.com. -
Now that the tunnel and DNS are set up, configure the tunnel. Create a
config.ymlfile in yourcloudflaredfolder and copy the following:yamltunnel: some-random-id-xxxxxxxxxxxx-xxxxx credentials-file: ~/.cloudflared/some-random-id-xxxxxxxxxxxx-xxxxx.json ingress: - hostname: tunnel.test.com service: http://192.168.1.100:8080 - service: http_status:404
This configuration tells Cloudflare to redirect requests to
http://192.168.1.100:8080when accessingtunnel.test.com. Ensure you includehttp_status:404as the last line, as it is always required. -
Test your tunnel by running the following command. Access
tunnel.test.comto verify that you can reach your local web service:bashcloudflared tunnel run YourTunnelName
-
Finally, create a Docker Compose file for easier tunnel management. Below is an example:
yamlservices: cloudflared: container_name: cloudflare-tunnel image: cloudflare/cloudflared:1759-f9c2bd51ae24 restart: unless-stopped command: tunnel --no-autoupdate run volumes: - /host_data/cloudflared:/home/nonroot/.cloudflared networks: - web
If you want to expose a web service running in a container, ensure that your tunnel is on the same network as the container.