Install Cloudflare Tunnel in Alpine Linux

I have a service installed in Proxmox LXC at home and want to make a service on it accessible from the internet. I’m using Cloudflare Tunnel for this, which works by running the cloudflared application. I’ll install this cloudflared in Alpine Linux.

Install cloudflared

First, you need to download and install the cloudflared client, which creates the tunnel.

Run the following command to download the latest version of cloudflared and save it to the /usr/bin directory. The -O flag specifies the output file name, and this is a good practice to avoid name conflicts.

wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -O /usr/bin/cloudflared

Next, make the downloaded file executable with the following command:

chmod +x /usr/bin/cloudflared

This allows the system to run the file as a program.

Create the OpenRC Startup Script

Since Alpine Linux by default use the OpenRC init system, you need to create a specific startup script to ensure cloudflared runs automatically and reliably.

Create a new file for the service script at /etc/init.d/cloudflared. Copy and paste the following script into the file. Be sure to replace <CLOUDFLARE_TUNNEL_TOKEN> with your actual Cloudflare Tunnel token. The token is essential as it links your server to your specific Cloudflare Tunnel.

#!/sbin/openrc-run

name=$(basename $(readlink -f $0))

pidfile="/var/run/$name.pid"

command="/usr/bin/cloudflared"

command_args=" --pidfile /var/run/$name.pid  --autoupdate-freq 24h0m0s tunnel run --token <CLOUDFLARE_TUNNEL_TOKEN>"

command_background=yes

stdout_log="/var/log/$name.log"

stderr_log="/var/log/$name.err"

depend() {
        need net
        after firewall
        use logger dns
}

Enable and Start the Service

To enable the service to start automatically on boot, run:

/etc/init.d/cloudflared enable

To start the cloudflared service immediately, run:

/etc/init.d/cloudflared start