Self-hosting securely with Cloudflare Tunnels

In the world of self-hosting, secure and reliable server access is crucial. Cloudflare Zero Trust offers a solution through Cloudflare Tunnels, allowing secure access to self-hosted services without opening ports or changing firewall settings. By creating an outbound-only connection to Cloudflare, traffic remains encrypted and routed through its global network, enhancing security and performance while protecting your server from direct attacks.

Create Cloudflare Tunnel

Docker compose

To get started, we need to set up a directory to store our docker-compose.yml file.

BASH
1mkdir cloudflared
2cd cloudflared
Click to expand and view more

Next, create a docker-compose.yml file within this directory to define the necessary services for the Cloudflare Tunnel.

BASH
1nano docker-compose.yml
Click to expand and view more

Add the following configuration to the file:

docker-compose.yml
 1services:
 2  cloudflared:
 3    image: cloudflare/cloudflared
 4    container_name: cloudflared
 5    environment:
 6      - TZ=Europe/Amsterdam
 7      - TUNNEL_TOKEN=${TOKEN}
 8    restart: unless-stopped
 9    command: tunnel --no-autoupdate run
10    networks:
11      - cloudflared
12networks:
13  cloudflared:
14    name: cloudflared
Click to expand and view more

Obtain Your Tunnel Token

Before we can run the tunnel, you need to create a Tunnel in your Cloudflare account. Follow these steps:

To do so:

To keep your token secure, create a .env file in the same directory as your docker-compose.yml:

BASH
1nano .env
Click to expand and view more

In the .env file, add the following content:

.env
TOKEN=<Your tunnel token>
Click to expand and view more

Make sure to replace Your tunnel token with the lengthy string of characters found after –token in the command you copied from the Cloudflare website.

Once everything is configured, you can start the tunnel by running:

BASH
1docker compose up -d
Click to expand and view more

If everything is correct you will see your tunnel connected within a couple of seconds.

Add a service

To verify that everything is functioning correctly, let’s start a simple service to test the tunnel using the whoami application. This is a basic HTTP service that displays information about the browser and operating system.

First, create a new directory called whoami and add a docker-compose.yml file:

BASH
1cd ..
2mkdir whoami
3nano whoami/docker-compose.yml
Click to expand and view more

Add the following configuration to the file:

docker-compose.yml
1services:
2  whoami:
3    container_name: simple-service
4    image: traefik/whoami
5    networks:
6        - cloudflared
7networks:
8  cloudflared:
9    name: cloudflared
Click to expand and view more

Run the command below to start the container.

BASH
1docker compose -f whoami/docker-compose.yml up -d
Click to expand and view more

Now that the whoami service is running, we can continue with the tunnel setup. Go back to the Cloudflare dashboard and click Next.

Fill in the following fields:

If the application is on the same docker network as the cloudflare tunnel you can use the container name.

Finally, click Save Tunnel. This will configure the tunnel, allowing you to access your service via Cloudflare.

If you perform an nslookup and traceroute for the specified domain name, you’ll notice that all traffic is routed through Cloudflare, and your own IP address will not be visible. This demonstrates that the Cloudflare Tunnel effectively masks your server’s true IP, enhancing your security by protecting it from direct exposure to the internet.

Copyright Notice

Author: Sven van Ginkel

Link: https://svenvg.com/posts/self-hosting-securely-with-cloudflare-tunnels/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut