SPAWNBOX

Configuration

Change the server settings in /etc/spawnbox/spawnbox.env: the flags of spawn-api serve and their defaults.

To change a setting:

  1. On the host, open the settings file:

    sudo nano /etc/spawnbox/spawnbox.env
  2. Add the flags to the SPAWNBOX_ARGS line.

  3. Save the file.

  4. Restart the service:

    sudo systemctl restart spawnbox

CAUTION: A restart deletes running sandboxes. Paused sandboxes stay. Running persistent sandboxes pause, or become cold.

To see all the flags, type /opt/spawnbox/bin/spawn-api serve --help.

Flag Default Function
--backend auto firecracker, gvisor or auto. auto uses Firecracker if /dev/kvm opens, and gVisor if it does not. Refer to Firecracker or gVisor.
--listen 127.0.0.1:8080 API address
--tls-cert, --tls-key None HTTPS for the API (PEM files). Spawnbox reads the files again when they change.
--preview-listen 127.0.0.1:8081 Preview proxy address
--preview-url http://{label}.localhost:8081 URL pattern for previews
--preview-tls-cert, --preview-tls-key None HTTPS for previews, usually a wildcard certificate
--data-dir /var/lib/spawnbox Database, builds, paused sandboxes and logs
--max-sandboxes 50 Maximum number of sandboxes that run at the same time
--memory-overcommit-pct 150 Percent of host memory that the memory_mib of running sandboxes can add up to. Over 100 fits more sandboxes that do not use all their memory. 100 means the host never gives out more memory than it has.
--vcpus, --memory-mib, --upper-disk-gib 2, 1024, 20 Size of base, and the default size for templates
--max-timeout-s 86400 Maximum timeout_s of a sandbox
--memory-pressure-pct 10 Pause idle sandboxes to disk and refuse new ones when free host memory is below this percent, or processes wait for memory (PSI) this percent of the time. 0 turns it off.
--ksm off Let the kernel merge equal memory pages across Firecracker VMs. Saves memory when sandboxes run the same programs, but a sandbox can learn what another holds by timing its writes. Use it only when all sandboxes trust each other. Needs Linux 6.7 or later.
--pin-vcpus on Keep each Firecracker vCPU on one host CPU. A VM’s vCPUs share one L3 cache while it has free CPUs, and VMs go to the least used L3 caches, so they do not share a link to memory. Firecracker’s disk and network threads run on any CPU. vCPUs that move between CPUs lose their caches: on a 96-core bare-metal host, a Node.js allocation benchmark took 25 to 45% longer unpinned. Under nested virtualization they can also crash (KVM_EXIT_FAIL_ENTRY in the sandbox log) once many VMs share each CPU. One L3 has one link to memory, so a VM’s memory bandwidth is that of one L3 (on the same host, STREAM fell from about 140 to 53 GB/s). auto pins only when the host is itself a VM; off never pins, which lets the kernel spread a VM over L3 caches and move busy vCPUs to idle CPUs.
--disk-io-engine sync Firecracker’s disk IO engine. async is opt-in and uses Firecracker’s io_uring engine, which Firecracker marks as a developer preview. It needs Linux 5.10.51 or later. Snapshots keep the engine they were made with, so a change makes all builds outdated: Spawnbox builds the templates again at the next start, and snapshot templates can no longer be used. Paused sandboxes keep their engine.
--huge-pages off Back Firecracker sandboxes’ memory with 2 MiB pages from the host’s hugetlbfs pool, so the guest maps memory with 512 times fewer entries. Memory heavy work gets faster: on a 96-core bare-metal host, a Better-Auth typecheck took 10% less time, its build 7% less and its pnpm install 28% less. Set the pool aside first, for example sysctl vm.nr_hugepages=40960 for 80 GiB; that memory is then used for nothing else. Each sandbox takes its whole memory_mib from the pool when it starts, and once the pool is full, sandboxes use normal pages. Its memory is copied in from the snapshot in the background, and before that wherever the guest touches it first. Pausing it saves all its memory, not only what changed.
--idle-pause-s 600 Pause a sandbox after this time without requests. 0 means never.
--paused-retention-s 604800 Delete paused sandboxes after this time. Persistent sandboxes are kept.
--shutdown-pause-s 30 On SIGTERM, time for running persistent sandboxes to pause. The ones that do not pause in this time stop and become cold.
--forward-listen None Address of the forward listener for your gateway. Off if not set.
--forward-tls-cert, --forward-tls-key None HTTPS for the forward listener
--forward-max-wakes, --forward-wake-queue-s 8, 10 Wakes from forwarded requests that run at once, and how long a request waits for one
--forward-max-in-flight 1024 Forwarded and preview requests in flight for one sandbox
--forward-max-response-s 600 Longest X-Spawnbox-Timeout-Ms a gateway can ask for, in seconds
--endpoint-propagation-s 2 After an endpoint switch, the time before the old target starts to drain
--max-builds 2 Maximum number of template builds at the same time
--egress-policy {"mode":"allow_all"} Network policy for all sandboxes
--dns 1.1.1.1 DNS server that the DNS forwarder sends queries to
--dns-port 15053 Host port (UDP and TCP) of the DNS forwarder. Only sandboxes can connect to it.
--secrets-key-file <data-dir>/secrets.key Key that encrypts secrets
--allow-unreadable-env Off Start even if the secrets key cannot read the stored env of some sandboxes. Spawnbox leaves those sandboxes as they are. It does not help with a different key: the egress CA key and stored secrets need the right one.
--database-url None Keep the state in this Postgres database, not in SQLite. Primaries on the same database fail over. You can also use SPAWNBOX_DATABASE_URL. Refer to Keep the API up when the primary stops.
--database-ca-file None PEM CA certificates for the TLS certificate of the database
--import-sqlite None On the first start, copy this SQLite database into the empty Postgres database
--events-retention-s 604800 Time to keep events. 0 means always.
--events-output-bytes 65536 Bytes of output kept in exec.finished
--warm-pool-memory-pct 10 Percent of host memory for warm VMs. 0 turns warm pools off.
--warm-pool-min 1 Minimum number of warm VMs for base
--build-store None Object store for builds: s3://bucket/prefix or file:///path. Refer to Keep builds in an object store.
--build-cache-gib 0 Maximum disk for builds on the host. 0 means no limit. Needs --build-store.
--otlp-endpoint None OpenTelemetry collector for events and traces. You can also use OTEL_EXPORTER_OTLP_ENDPOINT.
--metrics-listen None Address for Prometheus metrics at /metrics, for example 127.0.0.1:9464. It has no authentication. Refer to See metrics in Prometheus.
--fleet-listen None Make this host a primary. Workers connect to this address. Refer to Use more than one host.
--primary None Make this host a worker of the primary at this URL, for example https://10.0.0.1:7443. For primaries that fail over, give each URL, separated by commas.
--node-listen None Worker: address for requests from the primary
--advertise None Worker: URL of --node-listen for the primary, for example https://10.0.0.2:7444
--join-token None Worker: token from POST /v1/nodes/tokens. You can also use SPAWNBOX_JOIN_TOKEN. Necessary only for the first start.
--primary-ca-sha256 None Worker: ca_sha256 from the same response. Necessary only for the first start.
--self-fence Off Worker: stop persistent VMs after --fence-after-s without the primary. Use it on every worker. A leader always stops them when it loses its lease. Refer to Fence lost hosts.
--fence-after-s 60 Seconds without the primary before a worker with --self-fence stops its persistent VMs. A new leader waits this time plus --fence-margin-s before the old leader’s persistent sandboxes become cold.
--fence-margin-s 30 Primary: seconds added to the fence time of a worker before its persistent sandboxes become cold
--persistent-placement primary Primary: where the first VM of a new persistent sandbox starts. primary, or any for a worker that has a build of its template, else the primary
--autoscale-hook None Primary: program (absolute path) or webhook URL that adds and removes workers. Refer to Add and remove workers automatically.
--autoscale-secret None Key to sign webhook requests. You can also use SPAWNBOX_AUTOSCALE_SECRET.
--autoscale-min-workers 0 Minimum number of workers
--autoscale-max-workers 10 Maximum number of workers
--autoscale-up-pct 70 Add a worker when the load is above this percent
--autoscale-up-after-s 120 Seconds the load must stay high
--autoscale-down-pct 30 Remove a worker when the load is below this percent
--autoscale-down-after-s 1200 Seconds the load must stay low

To show previews on the internet:

  1. Point a wildcard DNS name, for example *.preview.example.com, to the host.
  2. Add these flags: --preview-listen 0.0.0.0:8081 --preview-url https://{label}.preview.example.com.
  3. Keep --listen on 127.0.0.1, or use --tls-cert and --tls-key.

Use a registrable domain only for previews, such as *.example-preview.com, not a name under the domain of your own site. An app in a sandbox can set cookies for its parent domains. Under your own domain, it could set cookies that your site reads. Previews of different sandboxes can also set cookies for each other. To stop that, add the preview domain to the Public Suffix List.

This page is the “Configuration” section of README.md ↗. Found a mistake? Edit it on GitHub ↗