This guide deploys Cloudflare Tunnel using Docker Compose to expose a self-hosted service through HTTPS, without requiring a public IP address or router port forwarding.
It is designed for Debian or Ubuntu environments and uses the Cloudflare dashboard for tunnel creation and configuration.
1. System Requirements
- Debian or Ubuntu server
- Root or sudo access
- Docker and Docker Compose
- A domain name managed by Cloudflare
- A running local web service
- Outbound internet connectivity
2. Docker and Compose Installation
We use Docker to ensure reproducible deployment.
2.1 Docker and Compose Installation
sudo apt update && sudo apt upgrade -y
sudo apt install -y docker.io docker-compose-v2
Enable Docker:
sudo systemctl enable --now docker
Verify:
docker --version
docker compose version
3. Cloudflare Tunnel Creation
Cloudflare Tunnel establishes an outbound connection between the local server and Cloudflare.
This allows external access to local services without exposing inbound ports.
3.1 Domain Configuration
Log in to the Cloudflare dashboard:
Ensure that the domain is managed by Cloudflare.
Example domain:
example.com
3.2 Tunnel Creation
- Navigate to Networking → Tunnels.
- Click Create a tunnel.
- Select Cloudflared if prompted.
- Enter a tunnel name:
my-tunnel
- Save the tunnel.
3.3 Tunnel Token
Select Docker as the connector environment.
Cloudflare provides a command similar to:
docker run cloudflare/cloudflared:latest tunnel --no-autoupdate run --token YOUR_TUNNEL_TOKEN
Copy the token following --token.
The Tunnel Token is different from a Cloudflare API Token.
Keep the token private.
4. Project Directory
Create a dedicated directory:
sudo mkdir -p /opt/cloudflared
cd /opt/cloudflared
Project structure:
/opt/cloudflared/
└── compose.yaml
5. Docker Compose Configuration
Create the Compose file:
sudo nano compose.yaml
Add the following configuration:
services:
cloudflared:
image: cloudflare/cloudflared:latest
container_name: cloudflared
restart: unless-stopped
command: tunnel --no-autoupdate run --token YOUR_TUNNEL_TOKEN
Replace YOUR_TUNNEL_TOKEN with the token provided by Cloudflare.
Since the token is stored directly in the Compose file, restrict access to it:
sudo chmod 600 compose.yaml
Start the service:
sudo docker compose up -d
Check the container status:
sudo docker ps | grep cloudflared
Check the logs:
sudo docker compose logs --tail=50 cloudflared
Successful output should include:
Registered tunnel connection
Return to the Cloudflare dashboard and verify that the tunnel status is Healthy.
6. Public Hostname Configuration
The tunnel must be configured to forward incoming requests to a local service.
6.1 Local Service Verification
Verify that the local service is running.
Example:
curl -I http://192.168.1.100:8080
An HTTP response indicates that the service is reachable.
6.2 Application Route Configuration
In the Cloudflare dashboard:
- Navigate to Networking → Tunnels.
- Select
my-tunnel. - Open Routes.
- Add a Published application route.
- Configure the hostname and local service.
Example configuration:
| Setting | Value |
|---|---|
| Subdomain | app |
| Domain | example.com |
| Path | Leave empty |
| Service Type | HTTP |
| Service URL | http://192.168.1.100:8080 |
Save the configuration.
Cloudflare normally creates the required DNS record automatically.
6.3 HTTPS Verification
Open the public URL:
https://app.example.com
Alternatively:
curl -I https://app.example.com
The local service should now be accessible through HTTPS.
7. Service Management
7.1 Service Startup
cd /opt/cloudflared
sudo docker compose up -d
7.2 Service Restart
sudo docker compose restart
7.3 Service Shutdown
sudo docker compose down
7.4 Service Logs
sudo docker compose logs --tail=50 cloudflared
Follow logs in real time:
sudo docker compose logs -f cloudflared
7.5 Service Update
sudo docker compose pull
sudo docker compose up -d
8. Tunnel Security
8.1 Token Protection
The Tunnel Token is stored directly in compose.yaml.
Restrict access:
sudo chmod 600 compose.yaml
Do not upload the Compose file containing a real token to a public repository.
If the token is exposed, rotate it through the Cloudflare dashboard.
8.2 Token Rotation
Rotate the Tunnel Token from the Cloudflare dashboard.
Update the Compose file:
sudo nano compose.yaml
Replace the old token with the new token.
Recreate the container:
sudo docker compose up -d --force-recreate
Review existing connectors and revoke unauthorized connections if necessary.
9. Network Configuration
Cloudflare Tunnel uses outbound connections to Cloudflare.
No inbound public firewall rules are required for the tunnel itself.
Cloudflared generally uses:
- UDP port
7844for QUIC - TCP port
7844for HTTP/2
Ensure that the local service is reachable from the cloudflared container.
For example:
http://192.168.1.100:8080
If a firewall is enabled, allow the required outbound connections.
10. Backup and Restore
10.1 Configuration Backup
Create a backup directory:
sudo mkdir -p /opt/backup/cloudflared
Back up the Compose configuration:
sudo cp /opt/cloudflared/compose.yaml /opt/backup/cloudflared/
sudo chmod 600 /opt/backup/cloudflared/compose.yaml
The Compose file contains the Tunnel Token and should be stored securely.
10.2 Configuration Restore
Restore the configuration:
sudo cp /opt/backup/cloudflared/compose.yaml /opt/cloudflared/
sudo chmod 600 /opt/cloudflared/compose.yaml
Start the service:
cd /opt/cloudflared
sudo docker compose up -d --force-recreate
If the token has been rotated, update the Compose file before starting the container.
11. Troubleshooting
11.1 Service Logs
sudo docker compose logs --tail=50 cloudflared
Check container status:
sudo docker ps -a | grep cloudflared
11.2 Invalid Tunnel Token
A common error is:
Provided Tunnel token is not valid.
Possible causes:
- Incorrect Tunnel Token
- Cloudflare API Token used instead of Tunnel Token
- Incorrect token in the Compose command
- Revoked or rotated token
Test the token directly:
docker run --rm cloudflare/cloudflared:latest \
tunnel --no-autoupdate run \
--token YOUR_TUNNEL_TOKEN
Successful output:
Registered tunnel connection
Press Ctrl+C to stop the test.
If the direct command works but Docker Compose fails, check the token in compose.yaml and recreate the container.
11.3 QUIC Connection Warnings
Cloudflared may report:
failed to dial to edge with quic: timeout
Temporary connection timeouts do not necessarily indicate tunnel failure.
Check whether other tunnel connections remain active.
If QUIC repeatedly fails, investigate UDP connectivity or consider HTTP/2 transport.
11.4 UDP Buffer Warning
The following warning may appear:
failed to sufficiently increase receive buffer size
This warning is related to UDP receive buffer configuration.
It can generally be ignored for small deployments if the tunnel remains stable.
11.5 HTTP 502 Error
An HTTP 502 error may indicate that cloudflared cannot reach the local service.
Verify the local service:
curl -I http://192.168.1.100:8080
Check the service URL configured in Cloudflare.
Avoid using localhost unless the container networking configuration explicitly supports it.
11.6 Tunnel Error 1033
Cloudflare Error 1033 generally indicates that Cloudflare cannot find a healthy tunnel connection.
Check:
- cloudflared container status
- Tunnel status in Cloudflare
- Connector logs
- Tunnel Token validity
- Outbound connectivity
11.7 Common Issues
- Invalid Tunnel Token
- Incorrect service URL
- Local service unavailable
- Incorrect DNS configuration
- Tunnel connector offline
- Outbound connectivity blocked
12. Quick Checklist
- Docker running
- Cloudflare Tunnel created
- Tunnel Token configured
- cloudflared container started
- Tunnel status Healthy
- Local service reachable
- HTTPS access working
- Configuration backed up securely