DEV Community

Data Goose
Data Goose

Posted on

Running disposable Linux terminal sandboxes for docs on Cloudflare Containers and Durable Objects

Most developer quickstarts still fail at the same step: a reader lands on your docs, hits brew install ... or docker run ..., decides not to install anything on their laptop, and bounces.

In-browser Wasm terminals solve this for pure JavaScript, Python, or SQLite, but they break as soon as your docs showcase native Linux CLIs like duckdb, trivy, k6, clickhouse-local, or container workflows. On the other hand, running your own pool of firecracker/Docker VMs means weeks of plumbing WebSockets, idle teardown, egress firewalls, and per-IP concurrency limits.

We built KilnVM to solve that with a Worker-native stack on Cloudflare. Here is how the pieces fit together.

Architecture Overview

KilnVM splits cleanly into two surfaces:

  1. kilnvm-web (kilnvm.dev): Static assets, documentation, framework guides, SEO pages, and lightweight client embed scripts.
  2. kilnvm-engine (api.kilnvm.dev): Control plane and container orchestrator built on Cloudflare Workers, Cloudflare Containers, Durable Objects, D1, and Cloudflare Turnstile.

1. Container Lifecycle Inside a Durable Object (SandboxSession)

Each live sandbox session maps to a SandboxSession Durable Object (scheduling_policy: "durable_object") backed by a Cloudflare Container.

One lesson we learned early is that one timeout mechanism is never enough when real users close laptop lids mid-session or scripted traffic forgets to attach a WebSocket:

  • Unattached timeout: If a client creates a session via POST /v1/sessions but never upgrades to WebSocket, a Durable Object alarm terminates the session shortly after creation.
  • Inactivity timeout (idleTtl): Both container.setInactivityTimeout(...) and ctx.storage.setAlarm(...) track recent terminal activity so an abandoned tab scales down automatically (60s on public demo sessions, 120s-600s across Free/Pro/Scale).
  • Hard max TTL (maxTtl): An absolute ceiling (expiresAt) ensures even an active infinite loop is destroyed when the session budget ends.

When the Durable Object wakes up, blockConcurrencyWhile checks now >= stored.expiresAt, unattached expiration, and idle expiration before scheduling or refreshing alarms, preventing zombie containers from lingering across cold DO activations.

2. Concurrency Gating Before Boot (ConcurrencyGate)

Before booting any container, api.kilnvm.dev checks tenant and IP concurrency inside a dedicated ConcurrencyGate Durable Object:

  • Account-wide concurrency caps (e.g., 2 on Free, 25 on Pro, 100 on Scale).
  • Per-IP session caps and short-window rate limits for anonymous traffic.
  • HTTP 429 concurrency_exceeded responses with Retry-After headers when saturated.

Gating inside a Durable Object avoids race conditions when 20 docs visitors click "Run" during the same second.

3. Securing Embeddable Terminals (networkBlockAll + Origin Locking)

Running anonymous Linux shells on the public internet has two immediate risks:

  1. Outbound abuse: People using your demo box as a proxy, miner controller, or scanner.
  2. Quota theft: Someone copying your two-line <script> embed snippet onto their own blog and burning your monthly sessions.

KilnVM mitigates both at the boundary:

  • Outbound firewall by default: Every sandbox boots with networkBlockAll outbound egress disabled. If your CLI actually needs to talk to your sandbox API, Pro (up to 2 HTTPS hosts) and Scale (up to 20 HTTPS hosts) allow explicit host allowlists.
  • Cloudflare Turnstile + Origin Verification: Public sessions verify Turnstile tokens, while embedded snippets on paid plans validate CSP frame-ancestors, Referer, and signed HMAC embed grants bound to your configured origins.

You can test the egress lock directly on the homepage demo at kilnvm.dev by clicking the "Test egress" quick preset (curl --connect-timeout 2 -I https://example.com).

Shipping Interactive Docs via Two Lines of HTML or CI

Once the sandbox API was solid, we packaged zero-dependency client integrations under kilnvm/sdks and published them on npm with Sigstore SLSA provenance:

<div data-kilnvm-project="demo"></div>
<script src="https://kilnvm.dev/kilnvm-embed.js" defer></script>
Enter fullscreen mode Exit fullscreen mode
import { KilnSandbox } from "kilnvm-react";

export function Quickstart() {
  return <KilnSandbox project="your-project-id" autoBoot />;
}
Enter fullscreen mode Exit fullscreen mode
  • Docusaurus v2 / v3 (docusaurus-plugin-kilnvm): Injects the lightweight runtime globally so docs pages can mount <KilnSandbox> or <div data-kilnvm-project="...">.

For longer step-by-step walkthroughs (/lab/<slug>), labs are defined declaratively in YAML with bash check verification commands and synced automatically on push using our GitHub Action (kilnvm/sync-action):

- uses: kilnvm/sync-action@v1
  with:
    api-key: ${{ secrets.KILNVM_API_KEY }}
    files: "labs/*.yaml"
Enter fullscreen mode Exit fullscreen mode

Try It Yourself

There is a live DuckDB + Parquet terminal running on the front page of kilnvm.dev (no signup needed), plus four standalone 3-step showcase labs for Trivy IaC, clickhouse-local, Grafana k6, and DuckDB.

If you are building developer docs or shipping workloads on Cloudflare Containers, give it a spin and let me know how cold boots and container ergonomics feel for your use case.

Top comments (0)