Templates

Build a reusable base image from a Dockerfile, an image or a snapshot, in your own provider account, cached by content hash.

A template is the build-time base a sandbox boots from. You can name an existing provider template by id, or have sbox build one for you from a Dockerfile, an image, or a snapshot.

// 1. An existing provider template / image / snapshot alias (provider-specific string)
await client.create({ template: "python-3.12" });

// 2. Build once, reuse everywhere
const ref = await client.templates!.build(
  {
    name: "desktop",
    source: {
      dockerfile: await readFile("./desktop/Dockerfile", "utf8"),
      context: "./desktop",
    },
    resources: { vcpus: 2, memoryMB: 4096 },
    startCmd: "/usr/local/bin/start-desktop",
  },
  { onEvent: (e) => console.log(e.type === "log" ? e.line : `[${e.status}]`) }
);
const sandbox = await client.create({ template: ref });

// 3. Inline: built on first use, reused by content hash afterwards
await client.create({
  template: { source: { dockerfile, context: "./desktop" } },
  onTemplateEvent: (e) => console.log(e),
});

Sources

SourceMeaning
{ dockerfile, context? }Dockerfile contents (not a path). context is a local directory (Node) or an in-memory { path: body } map. .dockerignore is honored for directories.
{ image, registryAuth? }A registry reference such as debian:bookworm-slim.
{ snapshot }A provider snapshot id used as the base (providers whose templates are snapshots).

Each provider accepts a subset of these: check client.capabilities.flags.templateSources. Asking for an unsupported source throws NotSupportedError before any network call.

Caching

The core computes a content hash over the Dockerfile, every context file, the image or snapshot ref, and the env/workdir/user/startCmd/readyCmd/resources fields. The adapter tags the built template with it, so:

  • a second build() or inline create() with the same inputs does not rebuild. It emits { type: "status", status: "cached" } and returns the existing template.
  • concurrent creates in the same process share one build.
  • { force: true } rebuilds anyway.

templateCacheKey(spec) is exported if you want the hash yourself.

Privacy

Builds run in your own provider account (bring-your-own-key). sbox never pushes an image to a public registry. The docker adapter tags images locally only.

Build events

type TemplateBuildEvent =
  | {
      type: "status";
      status: "cached" | "queued" | "building" | "ready" | "failed";
      message?: string;
    }
  | { type: "log"; line: string; stream?: "stdout" | "stderr"; step?: string };

Managing templates

await client.templates!.get("desktop"); // TemplateRef | undefined (by id, alias or cache key)
for await (const t of client.templates!.list()) console.log(t.id);
await client.templates!.delete(ref);

client.templates is typed undefined on providers without the templateBuild capability.

Templates vs snapshots

A template is the build-time base. Snapshots are runtime checkpoints of a live sandbox. To boot from a snapshot, use client.create({ fromSnapshot: ref }).

On this page