Templates
A template is defined in code and built by the platform. Every sandbox created from it boots that image in milliseconds. The build is cached step by step, so a rebuild that changes the last step reruns only that step.
from svm import Sandbox, SandboxClient, Template
template = ( Template("docs-fastapi", client=SandboxClient()) .from_python_image("3.12") .pip_install(["fastapi", "uvicorn"]) .set_envs({"APP_ENV": "docs"}) .set_ready_cmd("python3 -c 'import fastapi'"))
# on_log follows the build to its end and raises BuildError if it fails.build = template.build(on_log=lambda line: print(line.stream, line.line))print("build", build.id, build.info.status)
with Sandbox.create(template="docs-fastapi") as sandbox: result = sandbox.commands.run("python3 -c 'import fastapi; print(fastapi.__version__)'") print(result.stdout, sandbox.commands.run("echo $APP_ENV").stdout)import { Sandbox, SandboxClient, Template } from "@svm/sandbox";
const client = new SandboxClient();const template = new Template(client, "docs-python-app", { cpuCount: 2, memoryMb: 1024 }) .fromPythonImage("3.12") .pipInstall(["requests", "flask"]) .setEnvs({ APP_ENV: "docs" }) .setStartCmd("python -m flask --app /app run --port 8080") .setReadyCmd("curl -fsS http://127.0.0.1:8080/ || exit 1");
const build = await template.build({ wait: true, onLog: (line) => console.log(line.timestamp, line.line),});console.log(build.id, build.info.status);
const sandbox = await Sandbox.create({ template: "docs-python-app" }, client);try { console.log((await sandbox.commands.run("echo $APP_ENV")).stdout);} finally { await sandbox.kill();}template, err := client.NewTemplate("docs-python", svm.TemplateOptions{CPUCount: 2, MemoryMB: 1024})if err != nil { return err}if template, err = template.FromPythonImage("3.12"); err != nil { return err}if template, err = template.PipInstall("requests"); err != nil { return err}if template, err = template.SetEnvs(map[string]string{"APP_ENV": "docs"}); err != nil { return err}template.SetReadyCmd("python3 -c 'import requests'")
build, err := template.Build(ctx, svm.BuildOptions{Wait: true, OnLog: svm.DefaultBuildLogger(os.Stdout)})if err != nil { return err}fmt.Println("build", build.ID, "done")
sandbox, err := client.CreateSandbox(ctx, svm.CreateSandboxOptions{Template: "docs-python"})if err != nil { return err}defer sandbox.Kill(ctx)fmt.Println("sandbox", sandbox.Info().SandboxID, "from", sandbox.Info().TemplateID)require "svm"
template = SVM::Template.new("data-tools", cpu_count: 2, memory_mb: 2048) .from_python_image("3.12") .pip_install(%w[pandas requests]) .set_envs({ "APP_ENV" => "docs" }) .set_ready_cmd("python3 -c 'import pandas'")
build = template.build(on_log: ->(line) { puts line }) # streams the build log, raises SVM::BuildError on failureputs "build #{build.id} is #{build.info['status']}"
box = SVM::Sandbox.create(template: "data-tools")begin puts box.commands.run("python3 -c 'import pandas; print(pandas.__version__)'").stdoutensure box.killendimport dev.svm.sandbox.Client;import dev.svm.sandbox.Sandbox;import dev.svm.sandbox.Template;import dev.svm.sandbox.TemplateBuild;import java.util.List;import java.util.Map;
public class TemplatesExample { public static void main(String[] args) { Client client = Client.fromEnvironment();
Template template = new Template(client, "docs-python-app") .fromPythonImage("3.12") .pipInstall(List.of("requests")) .runCmd("mkdir -p /home/user/app") .setEnvs(Map.of("APP_ENV", "docs")) .setReadyCmd("python3 -c 'import requests'");
// follow = true reads the build logs to the end TemplateBuild build = template.build(true, line -> System.out.println(line.line())); System.out.println("build " + build.id() + ": " + build.status());
try (Sandbox sandbox = Sandbox.create(client, "docs-python-app")) { System.out.print(sandbox.commands().run("echo $APP_ENV && python3 --version").stdout()); } }}Builder helpers
Section titled “Builder helpers”| Helper | What it does |
|---|---|
from_image(ref), from_python_image("3.12"), from_node_image, from_ubuntu_image, from_debian_image, from_alpine_image |
Choose the base image |
from_catalog_template("python") |
Start from a platform catalog template instead of an image |
from_dockerfile(text) |
Import a Dockerfile |
apt_install, pip_install, npm_install, bun_install |
Install packages. apt_install uses the base image’s own package manager. |
copy, copy_items, make_dir, make_symlink, remove, rename |
Files |
git_clone(url, path, branch=, depth=) |
Clone a public HTTPS repository |
set_envs, set_workdir, set_user, set_default_user |
Runtime defaults |
set_start_cmd, set_ready_cmd |
A process to start, and the check that says the sandbox is ready |
add_mcp_server |
Turn on an MCP server for every sandbox of the template |
skip_cache |
Force a fresh build |
Ready-check helpers return the shell text that set_ready_cmd takes: wait_for_port(8080), wait_for_url(url),
wait_for_process("nginx"), wait_for_file(path) and wait_for_timeout(ms).
Private registries
Section titled “Private registries”Pass registry_secret to from_image with the name of a team secret that holds user:password. For Google
Artifact Registry, use from_gcp_registry(ref, secret) with a _json_key:<key JSON> secret.
From the CLI: svm template build, svm template list, svm template publish --tag.