Skip to content

feat: run the control plane without Cloudflare, in one container - #123

Merged
kalaspuff merged 9 commits into
mainfrom
fctl/64-make-it-possible-to-self-host-fctl-contr
Oct 20, 2026
Merged

kalaspuff merged 9 commits into
mainfrom
fctl/64-make-it-possible-to-self-host-fctl-contr

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Running your own fctl took a Cloudflare account with a domain on it, Wrangler signed in and an API token with five permissions. That's right for app.fctl.sh, but a lot to ask of someone who wants to try fctl on a spare machine, a homelab or a Kubernetes cluster they already run. Now the control plane also runs as one container, with no account anywhere:

docker run -d --name fctl --restart unless-stopped -p 8787:8787 -v fctl-data:/data \
  -e FCTL_PUBLIC_URL=http://192.168.1.20:8787 ghcr.io/hyperstructure/fctl-control
docker logs fctl   # the link that creates the first account

How

It's the same Worker and web app that run on Cloudflare, run by workerd (Cloudflare's open-source Workers runtime) through Miniflare, the same runtime wrangler dev and cargo xtask dev use. No code in the Worker knows where it runs. ADR 0026 explains why this beats a native port: a second implementation of D1, Durable Objects, alarms, hibernating WebSockets and WebCrypto behind 20,000 lines of control plane would drift from production.

deploy/standalone/server.mjs runs it with the bindings of crates/fctl-control/wrangler.toml and its state (D1 and every Durable Object, as SQLite files) in FCTL_DATA_DIR. It does what Cloudflare does around the Worker:

  • Migrations apply as it starts (wrangler d1 migrations apply --local, so d1_migrations records them as on Cloudflare).
  • Client addresses. A front proxy sets CF-Connecting-IP from the connection, or from X-Forwarded-For behind FCTL_TRUST_PROXY proxies. Without it, Miniflare keeps a CF-Connecting-IP the client sends, so anyone could dodge the sign-in rate limit and write any address into the audit log. The proxy also drops MF-* headers (which steer Miniflare), answers /cdn-cgi/ (Miniflare's own pages) with a 404, and turns off Miniflare's stack-trace error pages.
  • Housekeeping. It runs the hourly cron trigger, which Miniflare doesn't.
  • Setup. It makes a master key in the data directory when FCTL_MASTER_KEY isn't set (and refuses a different key later), and prints a setup link while the instance has no account.
  • One server per data directory. A lock file keeps a second server off the same state; two would each dispatch the same sessions. It also stops cleanly on SIGTERM (Miniflare's own handlers would exit at once), and exits when workerd stops answering so Docker or Kubernetes restarts it.

Also in this PR:

  • The image, deploy/standalone/Dockerfile → ghcr.io/hyperstructure/fctl-control. The Worker and web app build once on the build platform, and the runtime stage is Node 22 slim with wrangler, Miniflare and workerd, running as node under tini. It runs fine with a read-only root. compose.yaml and kubernetes.yaml (a one-replica StatefulSet with probes, a read-only root, a Service and an Ingress) are examples.
  • Plain http at LAN addresses. The sign-in cookies were __Host- cookies without Secure there, which browsers drop, so nobody could sign in at http://192.168.1.20:8787. Where cookies can't be Secure, they are now named fctl, fctl-oauth and fctl-2fa (fctl_domain::cookies). https and localhost instances keep their names. The server warns at startup about what plain http gives up.
  • cargo xtask serve runs the standalone server from a clone, without Docker. cargo xtask dev --server <url> runs the host and the smoke test against a control plane that's already running.
  • CI: a new "Standalone image" job builds the image, starts it on a fresh volume, runs cargo xtask dev --smoke --server against it, and checks that it stops cleanly. Release gets a "Standalone image" job that publishes it for amd64 and arm64 (:<version>, plus :<major>.<minor> and :latest when it's the newest release they cover).
  • Docs: docs/self-hosting.md now covers both ways (the shared GitHub, Google, email and hosts sections apply to both), plus architecture, security, development, README, AGENTS.md, the roadmap and the release skill.

Verified

  • cargo xtask check passes, including new tests for fctl_domain::cookies, the server's 23 node --test tests (proxy headers and client addresses, a real WebSocket through the proxy, config, the lock, the schedule) and its Biome check. actionlint (with shellcheck) passes on both workflows, and kubeconform -strict (Kubernetes 1.33) passes on kubernetes.yaml.
  • cargo xtask dev --smoke (the wrangler path) still passes after the dev.rs changes.
  • cargo xtask serve from the checkout, then cargo xtask dev --smoke --server http://localhost:8787: a host connects and the session commits, waits for its background task, takes its result and stops.
  • The image, built with rootless Podman here (this sandbox can't run Docker): started with --read-only --tmpfs /tmp, it made its master key, applied the 13 migrations and passed the same smoke test. SIGTERM through tini stops it cleanly and releases the lock. Restarted on the same volume, it kept the owner, the session and its event log, with nothing re-applied. The healthcheck command passes. It uses about 430 MB at rest (node 141 MB, workerd 285 MB), and the image is 488 MB. My local build needed three sandbox-only tweaks (apt as root, no chown, no USER) because Podman here maps a single UID. CI's "Standalone image" job builds the real Dockerfile with Docker (amd64), and on this PR it passed: as node, the image made its key, applied the migrations, ran the smoke test's session and exited 0 on docker stop.
  • Plain http in a browser (Playwright/Chromium) at http://192.168.64.39:8788. First, Chromium drops a __Host- cookie set over http at that address and keeps a plain one. Then the image: creating the owner through /setup signs in (cookie fctl, HttpOnly, not Secure), and the session survives a reload (/api/v1/auth/me 200, the floor loads).
  • Outbound HTTPS from workerd inside the image works: GitHub's API answers 200, and Google's and Resend's 400 and 401 without credentials, so the TLS handshakes succeed. The housekeeping call (scheduled with a scheduledTime) returns ok against the real Worker. A data directory the server can't write to now gets a one-line message naming the user that needs access, instead of a stack trace.
  • Spoofing: a POST /setup sent with CF-Connecting-IP: 6.6.6.6 and X-Forwarded-For: 7.7.7.7 went into the audit log as 127.0.0.1. /cdn-cgi/handler/scheduled and /cdn-cgi/local/platform-proxy answer 404. A second server on the same data waits 45 s, then refuses. With a small Durable Object, an alarm that came due while the server was stopped fired as soon as it started again.

For a person

  • The package's visibility. The first release that includes this creates ghcr.io/hyperstructure/fctl-control. If GitHub makes it private, make it public under the org's Packages → fctl-control → Package settings, as was done for the sandbox image.
  • Required checks. "Standalone image" isn't one; .github/rulesets/ decides that. I didn't add it, since a broken image shouldn't block deploying app.fctl.sh unless you want it to.
  • Timing. Merging pins fctl.sh's docs to these, while the image comes with the next release. Until then docker run ghcr.io/hyperstructure/fctl-control fails, but building from a clone works.
  • The landing page. docs(landing): say the control plane runs in one container too fctl-site#29 changes fctl.sh's "Yours to run" card to name both ways. Merge it after the release that publishes the image.

Session: https://app.fctl.sh/s/ses_01M5CBK2S0RJZ0GWZ7HAD137H4

fctl-sh Bot added 6 commits October 20, 2026 10:54
The sign-in cookies are __Host- cookies, which browsers take only with
Secure, from a secure origin. An instance at http://192.168.1.20:8787 set
them without Secure, so browsers dropped them and nobody could sign in from
the web app. Where cookies can't be Secure (fctl_domain::cookies::secure:
plain http outside localhost and loopback addresses), they are now named
without the prefix: fctl, fctl-oauth and fctl-2fa. Instances on https and
localhost keep the names they had.
deploy/standalone/server.mjs runs the Worker and the web app, as built for
Cloudflare, on workerd through Miniflare, with the bindings wrangler.toml
gives wrangler dev and its state (D1, every Durable Object) in
FCTL_DATA_DIR. It does what Cloudflare does around the Worker:

- applies the D1 migrations as it starts (wrangler d1 migrations apply
  --local, so d1_migrations records them);
- sets CF-Connecting-IP from the connection, or from X-Forwarded-For
  behind FCTL_TRUST_PROXY proxies, and drops what clients must not set:
  CF-*, MF-* (which steer Miniflare) and /cdn-cgi/ (Miniflare's pages);
- runs the hourly cron trigger, which Miniflare doesn't.

It reads the Worker's variables and secrets from the environment (or
<NAME>_FILE), makes a master key in the data directory and a setup link
when they aren't given, holds a lock on the data directory so that a
second server can't run the same Durable Objects, stops cleanly on
SIGTERM, and exits when workerd stops answering. It is an npm workspace
with its own Biome config and node --test tests.
`cargo xtask serve` builds the Worker and the web app for production and
runs the standalone control plane from the checkout, as its container
does, with its state in .fctl-serve/. `cargo xtask dev --server <url>`
runs the host (and with --smoke, the session) against a control plane
that already runs there, bootstrapping the dev owner with the dev setup
token, instead of starting wrangler dev; CI uses it on the image.
`cargo xtask check` runs the standalone server's Biome check and tests.
…bernetes examples

deploy/standalone/Dockerfile builds ghcr.io/hyperstructure/fctl-control
from the repository: the Worker and the web app on the build machine's
platform, then a Node.js image with wrangler, Miniflare and workerd for
the target's, running server.mjs under tini as the node user, with its
state in /data. compose.yaml runs it with Docker Compose; kubernetes.yaml
runs it as a StatefulSet of one (two servers on the same state would run
the same sessions), read-only, with probes, a Service and an Ingress.
…release

CI's "Standalone image" job builds the image, starts it on a fresh volume
and runs `cargo xtask dev --smoke --server` against it, then checks that
it stops cleanly. The Release workflow publishes it for amd64 and arm64 as
ghcr.io/hyperstructure/fctl-control:<version>, with :<major>.<minor> and
:latest when the release is the newest they cover.
docs/self-hosting.md now has two ways to run the control plane, on your
own machine or cluster (Docker, Kubernetes, a clone) and on Cloudflare,
followed by what is the same for both. ADR 0026 records why the
standalone control plane is the same Worker on workerd rather than a
native port; architecture, security and development say what it does and
how to work on it.
@kalaspuff
kalaspuff merged commit 73d1917 into main Oct 20, 2026
8 of 9 checks passed
@kalaspuff
kalaspuff deleted the fctl/64-make-it-possible-to-self-host-fctl-contr branch October 20, 2026 11:14
kalaspuff pushed a commit that referenced this pull request Oct 20, 2026
Follows #125.

The self-hosting guide's example of a new standalone server's first log
(added in #123) says it applies 13 database migrations. #125, merged
right after, added `0014_workspace_skip_invites.sql`, so a server
started from `main` applies 14. This updates that line; nothing else in
the guide counts migrations.

## Verification

`FCTL_SETUP_TOKEN=dev-setup-token cargo xtask serve` with a new data
directory, on a tree that is this branch's (`main` with #123 and #125,
plus this line), logs `Applied 14 database migrations`. On the same
tree, `cargo xtask dev --smoke --server http://localhost:8787` passed,
`fctl workspaces set --skip-invites on|off` worked against that server,
and `cargo xtask check` passed.

Session: https://app.fctl.sh/s/ses_01M5CCM54M0DDN4RFRH5J5C0KW

Co-authored-by: fctl-sh[bot] <fctl-sh[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant