cloudflared Setup
Run the cloudflared connector next to Labelgate.
Use this when you publish containers with labelgate.tunnel.* labels. Labelgate writes the tunnel routes to Cloudflare. cloudflared is the connector that carries the traffic, so it must run too.
1. Create the tunnel
- Open Cloudflare Zero Trust → Networks → Tunnels → Create a tunnel → Cloudflared.
- Name it and save. Skip the "public hostname" step: Labelgate adds those.
- Copy two values:
- the tunnel ID →
LABELGATE_CLOUDFLARE_TUNNEL_ID(for Labelgate); - the token from the install command (the long string after
--token) →TUNNEL_TOKEN(for cloudflared).
- the tunnel ID →
Labelgate also needs LABELGATE_CLOUDFLARE_ACCOUNT_ID, the account that owns the tunnel.
2. Add cloudflared to Compose
services:
cloudflared:
image: cloudflare/cloudflared:latest
restart: unless-stopped
command: tunnel --no-autoupdate run --token ${TUNNEL_TOKEN}
app:
image: nginx:alpine
labels:
labelgate.tunnel.app.hostname: "app.example.com"
labelgate.tunnel.app.service: "http://app:80"cloudflared picks up new routes by itself. You never restart it when labels change.
For the full file with the labelgate service, see Single Host → Tunnel.
Reaching your services
cloudflared opens the service URL itself, so the address must work from the cloudflared container.
| Service runs... | Use in service | Extra setup |
|---|---|---|
| in the same Compose file | http://app:80 (service name) | none |
| in another Compose project | http://app:80 | put both containers on one shared network (below) |
| directly on the host | http://host.docker.internal:8080 | add extra_hosts to cloudflared (below) |
Shared network for containers from other Compose projects:
services:
cloudflared:
networks: [tunnel]
networks:
tunnel:
name: tunnel # other projects join it with: networks: { tunnel: { external: true } }A service on the host itself:
services:
cloudflared:
extra_hosts:
- "host.docker.internal:host-gateway"How to check it works
docker compose logs cloudflaredshowsRegistered tunnel connectionlines.- The tunnel shows Healthy in the Zero Trust dashboard.
- The tunnel's hostname routes in the dashboard list the hostnames from your labels.
curl -I https://app.example.comanswers from your app.
Common problems
| Symptom | Fix |
|---|---|
502 Bad Gateway | cloudflared cannot reach the service. Use the service name, not localhost, and check the networks. |
Hostname gives Cloudflare error 1033 | cloudflared is not running or uses the token of another tunnel. The token must belong to LABELGATE_CLOUDFLARE_TUNNEL_ID. |
Labelgate shows the route active, but cloudflared ignores it | cloudflared runs with a local config.yml (a locally managed tunnel). Create the tunnel in the dashboard as above and run cloudflared with --token only. |
Agent host: 502 for some requests | Each host that runs cloudflared for a tunnel gets a share of its traffic. Give every host its own tunnel; see Multi-Host. |