Multi-Host (Agent Mode)
Run one main instance and a small agent on every other Docker host.
Use this when your containers run on more than one Docker host.
- One main instance holds the Cloudflare token and makes every change.
- Every other host runs an agent. It only reads Docker labels and sends them to the main. It has no Cloudflare token.
edge1 (agent) ──┐
├──> main ──> Cloudflare
edge2 (agent) ──┘What goes where
| Setting | Main | Agent |
|---|---|---|
Cloudflare token, account, tunnels (cloudflare.*) | ✓ | |
Agent listener: agent.enabled, agent.listen, agent.tls.* | ✓ | |
Agent list with tokens and allow-lists: agent.agents.<id>.* | ✓ | |
Shared token and its allow-lists: agent.accept_* | ✓ | |
Agent mode: mode: agent (LABELGATE_MODE=agent) | ✓ | |
Connection: connect.* (endpoint, agent ID, token, TLS) | ✓ | |
Container labels (labelgate.dns.*, labelgate.tunnel.*, ...) | ✓ | ✓ |
All allow-lists live on the main. Setting them on an agent does nothing.
Recipe: agent connects to main
This is the usual setup. It works when the agent is behind NAT. The example agent is called edge1 and gets its own tunnel (see Tunnels for agent hosts).
1. Make a token for the agent and keep it for both hosts:
openssl rand -hex 322. On the main host, create labelgate.yaml. Agents are configured in this file only; there are no environment variables for agent.agents.
cloudflare:
tunnels:
edge1: # second tunnel, used by the edge1 host
account_id: "your-account-id"
tunnel_id: "edge1-tunnel-id"
agent:
enabled: true
listen: ":28112"
tls:
cert: /etc/labelgate/tls/server.crt
key: /etc/labelgate/tls/server.key
agents:
edge1: # must equal the agent's LABELGATE_CONNECT_AGENT_ID
token: "PASTE_TOKEN_HERE" # from step 1
default_tunnel: edge1
hostnames: ["*.edge1.example.com"]
services: ["whoami"] # host names the agent's tunnel services may useservices:
labelgate:
image: ghcr.io/channinghe/labelgate:v0.3
restart: unless-stopped
group_add: ["${DOCKER_GID}"]
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- labelgate-data:/app/config
- ./labelgate.yaml:/etc/labelgate/labelgate.yaml:ro
- ./tls:/etc/labelgate/tls:ro
environment:
- LABELGATE_CLOUDFLARE_API_TOKEN
- LABELGATE_CLOUDFLARE_ACCOUNT_ID
- LABELGATE_CLOUDFLARE_TUNNEL_ID
- LABELGATE_API_TOKEN
ports:
- "127.0.0.1:28111:28111" # dashboard
- "28112:28112" # agents connect here
cloudflared:
image: cloudflare/cloudflared:latest
restart: unless-stopped
command: tunnel --no-autoupdate run --token ${TUNNEL_TOKEN}
volumes:
labelgate-data:The container runs as UID 65532. Make the files readable for it and private to everyone else:
sudo chown -R 65532 labelgate.yaml tls
sudo chmod 600 labelgate.yaml tls/server.key3. On the agent host (edge1):
services:
labelgate-agent:
image: ghcr.io/channinghe/labelgate:v0.3
restart: unless-stopped
group_add: ["${DOCKER_GID}"]
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
environment:
- LABELGATE_MODE=agent
- LABELGATE_CONNECT_ENDPOINT=wss://main.example.com:28112/ws
- LABELGATE_CONNECT_AGENT_ID=edge1 # must equal agent.agents.edge1 on the main
- LABELGATE_CONNECT_TOKEN=${AGENT_TOKEN} # same token as on the main
cloudflared:
image: cloudflare/cloudflared:latest
restart: unless-stopped
command: tunnel --no-autoupdate run --token ${TUNNEL_TOKEN} # token of the edge1 tunnel
whoami:
image: traefik/whoami
labels:
labelgate.tunnel.whoami.hostname: "whoami.edge1.example.com"
labelgate.tunnel.whoami.service: "http://whoami:80"The agent needs no data volume and no Cloudflare token. DNS labels with target: auto use the agent host's public IP.
Recipe: main connects to agent
Use this when the main can reach the agent, but the agent cannot reach the main. The agent listens on a port, and the main dials it.
agent:
enabled: true
tls:
ca: /etc/labelgate/tls/ca.pem # CA that signed the agent's certificate
agents:
edge2:
token: "PASTE_TOKEN_HERE"
connect_to: wss://192.168.1.12:28113/ws
hostnames: ["*.edge2.example.com"]services:
labelgate-agent:
image: ghcr.io/channinghe/labelgate:v0.3
restart: unless-stopped
group_add: ["${DOCKER_GID}"]
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./tls:/certs:ro
environment:
- LABELGATE_MODE=agent
- LABELGATE_CONNECT_MODE=inbound
- LABELGATE_CONNECT_LISTEN=:28113
- LABELGATE_CONNECT_AGENT_ID=edge2 # must equal agent.agents.edge2 on the main
- LABELGATE_CONNECT_TOKEN=${AGENT_TOKEN}
- LABELGATE_CONNECT_TLS_CERT=/certs/server.crt
- LABELGATE_CONNECT_TLS_KEY=/certs/server.key
ports:
- "28113:28113"The agent's certificate must be valid for the connect_to host. For an IP address like 192.168.1.12, it needs that IP in its SAN list.
Tokens and agent IDs
- Generate every token with
openssl rand -hex 32. Tokens need at least 32 characters and 12 different characters. - The same token goes in two places:
agent.agents.<id>.tokenon the main andconnect.token(LABELGATE_CONNECT_TOKEN) on the agent. - Named agent (
agent.agents.<id>): setconnect.agent_id(LABELGATE_CONNECT_AGENT_ID) to that<id>. Use lower-case letters, digits,.,_and-. - Shared-token agent (
agent.accept_token): the ID is optional. Mount/etc/machine-id:/etc/machine-id:roso it stays the same when the container is recreated.LABELGATE_CONNECT_AGENT_IDtakes priority if set. - With neither, the agent uses the container hostname and logs a warning at startup. The hostname changes on every recreate, so the main sees a new agent and removes the old one's resources after
sync.agent_grace(LABELGATE_SYNC_AGENT_GRACE). - Labelgate does not expand
${VAR}inlabelgate.yaml. Paste the real token into the file.
TLS
The endpoint is a wss:// URL that ends in /ws, for example wss://main.example.com:28112/ws.
| Agent connects to main | Main connects to agent | |
|---|---|---|
| Certificate and key | main: agent.tls.cert, agent.tls.key (LABELGATE_AGENT_TLS_CERT, LABELGATE_AGENT_TLS_KEY) | agent: connect.tls.cert, connect.tls.key (LABELGATE_CONNECT_TLS_CERT, LABELGATE_CONNECT_TLS_KEY) |
| Private CA (otherwise system CAs) | agent: connect.tls.ca (LABELGATE_CONNECT_TLS_CA) | main: agent.tls.ca (LABELGATE_AGENT_TLS_CA) |
- Reverse proxy instead: let Caddy, Traefik or nginx serve
wss://with your usual certificate and forward WebSocket traffic to port 28112. Leaveagent.tlsempty then. ws://works, but the data is not encrypted. Use it only on a trusted LAN or VPN.insecure_skip_verify: true(connect.tls.insecure_skip_verify/agent.tls.insecure_skip_verify) turns off certificate checks. Use it only for testing.
Named agents vs shared token
There are two ways to let an agent in. Both are set on the main.
| Named agent | Shared-token agent | |
|---|---|---|
| Set with | agent.agents.<id> | agent.accept_token (LABELGATE_AGENT_ACCEPT_TOKEN) |
| Token | one per agent | one for all agents |
| Allow-lists | agent.agents.<id>.hostnames, .tunnels, .services, .default_tunnel | agent.accept_hostnames, agent.accept_tunnels, agent.accept_services |
| Where | config file only | config file or environment |
Named agents are safer: each has its own token and its own limits. A shared token lets any agent ID that is not in agent.agents connect.
agent.accept_* never applies to named agents. An agent listed under agent.agents uses only its own hostnames, tunnels and services. It also cannot log in with accept_token.
agent:
accept_services: ["traefik"] # ignored for edge1
agents:
edge1:
token: "PASTE_TOKEN_HERE"
services: ["traefik"] # this is the one edge1 usesA shared-token setup looks like this:
agent:
enabled: true
# accept_token: set LABELGATE_AGENT_ACCEPT_TOKEN instead of writing it here
accept_hostnames: ["*.agents.example.com"]
accept_tunnels: [default]
accept_services: ["traefik"]The accept_* lists also work as comma-separated variables, for example LABELGATE_AGENT_ACCEPT_SERVICES=traefik,web.
Allow-lists
Allow-lists limit what an agent may create. A claim that breaks a list is rejected. The dashboard shows the reason, including the exact key to change. Nothing reaches Cloudflare.
| Controls | Named agent | Shared-token agents | If empty |
|---|---|---|---|
| Hostnames it may use | agent.agents.<id>.hostnames | agent.accept_hostnames | any hostname |
| Tunnels it may use | agent.agents.<id>.tunnels | agent.accept_tunnels | only its default tunnel |
Tunnel for labels without tunnel | agent.agents.<id>.default_tunnel | (always the global default) | the global default tunnel |
Host names in tunnel service URLs | agent.agents.<id>.services | agent.accept_services | IP addresses only |
- Hostname patterns:
*.example.commatchesa.example.comanda.b.example.com, but notexample.com. - A wildcard hostname label (
hostname: "*.example.com") needs exactly that entry in the list. - Tunnel names:
default(the tunnel incloudflare.tunnel_id), a key undercloudflare.tunnels, or a tunnel ID. - A named agent may always use its
default_tunnel.
Agent tunnel services
The main checks every tunnel service URL that comes from an agent:
service points to | Allowed? |
|---|---|
An IP address: http://192.168.1.20:8080, http://172.18.0.5:80 | yes, no setup |
A host name: a Compose service (http://traefik:80), host.docker.internal, any DNS name | only if listed in services |
A local IP: 127.0.0.1, 169.254.x.x, 100.64.0.0/10, 0.0.0.0 | only if that IP or a CIDR containing it is listed |
Other schemes (unix:, bastion), user:pass@ in the URL, short IPs like 127.1 | never |
Agents may use http, https, tcp, ssh and rdp URLs (and hello_world / http_status:<code>). Containers on the main host itself are not checked.
To allow host names, add them to that agent's services on the main. Entries can be names, patterns, IP addresses or CIDRs:
agent:
agents:
edge1:
token: "PASTE_TOKEN_HERE"
services:
- traefik # http://traefik:80
- "*.svc.internal" # pattern
- 172.20.0.0/16 # CIDRTunnels for agent hosts
cloudflared opens the service URL. A name like http://whoami:80 only works for a cloudflared on the same host and Docker network. Cloudflare can send a tunnel's traffic to any of its cloudflared connectors. So give every agent host its own tunnel:
- Create one tunnel per agent host (see cloudflared).
- On the main, add it under
cloudflare.tunnels.<name>(file only) and setagent.agents.<id>.default_tunnel: <name>. - On the agent host, run cloudflared with that tunnel's token.
The first recipe does exactly this.
When an agent goes offline
- Its DNS records and tunnel routes stay for
sync.agent_grace(LABELGATE_SYNC_AGENT_GRACE, default24h, minimum5m) after it was last seen. - After that they are deleted after
sync.remove_delay(LABELGATE_SYNC_REMOVE_DELAY, default30m), unless the label setscleanup: "false". - Restarting the main does not reset this.
- Dashboard status:
active(connected),disconnected(offline, still kept),removed(resources released).
How to check it works
- Main log:
docker compose logs labelgate | grep agentshowsmsg="agent connected" ... agent_id=edge1. - Agent log:
docker compose logs labelgate-agentshowsmsg="connected to main". - Dashboard → Agents: the agent is
active. Its resources areactive; rejected ones show the reason. - The endpoint is reachable:
curl https://main.example.com:28112/healthreturns{"status":"ok"}. curl -I https://whoami.edge1.example.comanswers from the agent host.
Common problems
| Symptom | Fix |
|---|---|
Main logs agent handshake failed | The tokens differ, or the agent ID is not a key under agent.agents. Copy the token again and compare the ID. |
connect.endpoint ... has no path (did you mean .../ws?) | Add /ws to LABELGATE_CONNECT_ENDPOINT. |
Agent logs x509: certificate signed by unknown authority | Set LABELGATE_CONNECT_TLS_CA to your CA file, or use a public certificate. |
must be at least 32 characters or distinct characters | Make a new token with openssl rand -hex 32. A ${VAR} written in labelgate.yaml is not expanded and fails this check. |
service host "traefik" is a host name, which agents may only use if allow-listed | Add traefik to that agent's services on the main. See Troubleshooting. |
Added to agent.accept_services, still rejected | The agent is a named agent. Use agent.agents.<id>.services. |
rejected a second connection for an agent ID that is already connected | Two hosts use the same agent ID. Give each host its own. |
conflict: the ... is already managed by ... | A hostname belongs to one host at a time. Use a different hostname. See Ownership. |
Tunnel gives 502 for agent services | The request reached a cloudflared on another host. Use one tunnel per host. |
| v0.2 agent cannot connect | Upgrade the main and all agents to v0.3 together. See Upgrading. |