One-program sandboxes
Pass command or entrypoint to run one program in a sandbox, like
docker run. Unlike a session, it accepts no commands from your code and
stdin is closed. Wait for exit, then read output.
with Sandbox.create( "alpine:3.20", command=["sh", "-c", "echo out; echo err >&2; exit 3"], name="exits-three", cpus=2, memory_mib=2048, seed=7,) as sandbox: print(sandbox.wait()) output = sandbox.output() print(output.stdout) print(output.stderr) print(sandbox.state().kind)await using sandbox = await Sandbox.create("alpine:3.20", { command: ["sh", "-c", "echo out; echo err >&2; exit 3"], name: "exits-three", cpus: 2, memoryMib: 2048, seed: 7,});console.log(await sandbox.wait());const output = await sandbox.output();console.log(text(output.stdout));console.log(text(output.stderr));console.log((await sandbox.state()).kind);let sandbox = client .create_sandbox("alpine:3.20") .command(["sh", "-c", "echo out; echo err >&2; exit 3"]) .name("exits-three") .cpus(2) .memory_mib(2048) .seed(7) .await?;println!("{}", sandbox.wait().await?);let output = sandbox.output().await?;println!("{:?}", String::from_utf8_lossy(output.stdout.bytes()));println!("{:?}", String::from_utf8_lossy(output.stderr.bytes()));println!("{}", sandbox.state().await?.kind());What runs
Section titled “What runs”commandis argv, replacing the image’sCMD.entrypointreplaces the image’sENTRYPOINT, withcommandas its arguments.
with Sandbox.create("alpine:3.20", entrypoint="sh", command=["-c", "uname -a"]) as sandbox: sandbox.wait() print(sandbox.output().stdout.decode(), end="")await using sandbox = await Sandbox.create("alpine:3.20", { entrypoint: "sh", command: ["-c", "uname -a"],});await sandbox.wait();console.log(text((await sandbox.output()).stdout));let sandbox = client .create_sandbox("alpine:3.20") .entrypoint("sh") .command(["-c", "uname -a"]) .await?;sandbox.wait().await?;print!( "{}", String::from_utf8_lossy(sandbox.output().await?.stdout.bytes()));The program inherits the image’s environment, user, and working directory,
plus any env you pass (Environment and secrets).
Images must be public; pulls are anonymous.
Size, seed, and name
Section titled “Size, seed, and name”| Default | Range | |
|---|---|---|
cpus |
1 | 1 to 8 vCPUs |
memory (memory_mib, memoryMib) |
1024 MiB | 128 to 16384 MiB |
seed |
0 | 0 to 253 − 1 |
name |
from the image | up to 63 lowercase letters, digits and -, not starting or ending with - |
Your plan may cap resources below these ranges; larger requests fail with your plan’s limit (Plans, limits and credit). The same image, command, and seed run the same way, except where the program reads something from outside that differs, such as the network (What a recording holds). Name is a label for you.
These options also apply to a session (a sandbox accepting commands).
Wait and read output
Section titled “Wait and read output”waitreturns the exit code once the program exits, or raises the matching error if stopped or failed (Errors).outputreturns stdout and stderr written so far, as bytes.
For a session, output returns the session server’s
response frames rather than command output
(The sandbox protocol). Use run there.
Pause, resume, stop, and reconnect
Section titled “Pause, resume, stop, and reconnect”with Sandbox.create("alpine:3.20", command=["sh", "-c", "while :; do :; done"]) as sandbox: sandbox.pause() print(sandbox.state().kind) sandbox.resume()
elsewhere = Sandbox.connect(sandbox.id) elsewhere.stop() elsewhere.close() print(sandbox.state().kind)await using sandbox = await Sandbox.create("alpine:3.20", { command: ["sh", "-c", "while :; do :; done"],});await sandbox.pause();console.log((await sandbox.state()).kind);await sandbox.resume();
const elsewhere = await Sandbox.connect(sandbox.id);await elsewhere.stop();console.log((await sandbox.state()).kind);let sandbox = client .create_sandbox("alpine:3.20") .command(["sh", "-c", "while :; do :; done"]) .await?;sandbox.pause().await?;println!("{}", sandbox.state().await?.kind());sandbox.resume().await?;
let elsewhere = client.connect_sandbox(sandbox.id()).await?;elsewhere.stop().await?;println!("{}", sandbox.state().await?.kind());pausefreezes the sandbox;resumecontinues it.stopterminates it (no-op if already ended). Stopped sandboxes cannot be inspected; exited ones can.statereturnsqueued,running, orpaused, thenexited(with exit code),stopped, orfailed(with reason).connectattaches another handle by sandbox ID from any process using an API key on the same account.
A sandbox that never exits runs until its time limit, then fails with that
reason (Plans, limits and credit). Exiting a with
block, await using, or scope stops a running one-program sandbox.