Move a laptop-hosted Cloudflare named tunnel site to Fly without touching DNS: run cloudflared inside the container
Situation: a custom domain served by cloudflared tunnel run from a laptop to vite preview on localhost:4173 (site returns HTTP 530 whenever the laptop/tunnel is down). Goal: always-on hosting without editing Cloudflare DNS (cloudflared's cert.pem can't edit arbitrary records).
Approach: deploy the app to Fly and run the *same* named tunnel inside the container. Store the tunnel credentials JSON (~/.cloudflared/<UUID>.json) as a secret (e.g. CF_TUNNEL_CREDENTIALS), and in the start script write it to /tmp, generate a config whose ingress points the hostnames at http://localhost:8080, then cloudflared --no-autoupdate tunnel --config /tmp/tunnel.yml run <UUID> & before exec uvicorn .... Running by UUID with credentials-file needs no cert.pem. The existing CNAME to <UUID>.cfargotunnel.com keeps working and Cloudflare still terminates TLS. Stop the laptop copy of the tunnel, or Cloudflare will balance requests across both connectors.
Related gotchas:
- The frontend called a relative /api, and in dev Vite proxied it (vite preview inherits server.proxy). In a single container, mount the built dist from FastAPI with a catch-all GET registered *after* the API routes; 404 anything under api/ and fall back to index.html for SPA routes.
- Set auto_stop_machines = "off" and min_machines_running = 1 in fly.toml, so long SSE agent runs aren't cut off and the tunnel connector stays up.
- Keep large git-ignored data (GBs) out of the image: put it on a Fly volume mounted where the code expects data/, and upload it once. Stage a minimal build context (backend + dist) so fly deploy --remote-only doesn't ship the data on every deploy.