Sandboxes
Guides show how to do one task at a time, with the cere CLI and the Python SDK side by side. For why sandboxes
work the way they do, read Sandboxes in Concepts.
A sandbox is a gVisor container on a node of the cluster. You create it from an image, run commands in it, move files in and out, and terminate it. The examples assume you are signed in (Signing in).
Create a sandbox
Section titled “Create a sandbox”cere sandbox create starts a sandbox from an image and waits until it runs; Sandbox.create does the same.
cere sandbox create --image python:3.12-slim --timeout 300sb-01m89qnegv0g7cq352t9ck4qvm placed on node d4007f488e29; starting python:3.12-slim ...running on node d4007f488e29 (started in 173 ms)sb-01m89qnegv0g7cq352t9ck4qvmThe ID goes to standard output and the progress lines to standard error. In a script, -q prints only the ID:
ID=$(cere sandbox create --image python:3.12-slim --timeout 300 -q)The rest of this page uses $ID. End the first sandbox with cere sandbox terminate <id>
(below), or let its timeout end it.
import cere
sb = cere.Sandbox.create(image="python:3.12-slim", timeout=300)print(sb.object_id, sb.status)sb-01m89qq2ep7kdfw72dvzxs1mkm runningSandbox.create waits until the sandbox runs (wait=False returns while it is still starting).
The sandbox runs sleep infinity until its timeout or until you terminate it, and its bucket pays for it while
it runs. The image comes from a public registry (docker.io, ghcr.io, quay.io or public.ecr.aws) and needs
sh and sleep: the default command is sleep infinity, and file copies run through sh.
Set options at create
Section titled “Set options at create”Size, timeouts, environment, working directory and labels are set when the sandbox is created.
APP=$(cere sandbox create --image python:3.12-slim \ --cpu 0.5 --memory 512 --timeout 300 --idle-timeout 120 \ --env MODE=demo --workdir /workspace/app --label app=docs -q)cere sandbox exec $APP -- sh -c 'echo "$MODE in $(pwd)"'demo in /workspace/appapp = cere.Sandbox.create( image="python:3.12-slim", cpu=0.5, memory=512, # vCPU; MiB timeout=300, idle_timeout=120, # seconds env={"MODE": "demo"}, workdir="/workspace/app", labels={"app": "docs"},)print(app.exec("sh", "-c", 'echo "$MODE in $(pwd)"').stdout.read(), end="")demo in /workspace/app| CLI flag | Python | Default | What it sets |
|---|---|---|---|
--image |
image= |
required | The image, from a public registry |
--cpu |
cpu= |
1 | vCPUs reserved; also the CPU quota |
--memory |
memory= |
1024 | MiB reserved, at least 128; also the memory limit |
--pids |
pids= |
2048 | Process limit |
--timeout |
timeout= |
600 | Seconds (CLI: or a duration, 10m) until the sandbox is terminated |
--idle-timeout |
idle_timeout= |
off | Terminate after this long with no exec or file call |
--env K=V |
env={} |
none | Environment variables. Not secret: see Egress and secrets for credentials |
--workdir |
workdir= |
/workspace |
Working directory of the main process and of exec |
--label k=v |
labels={} |
none | Labels to find the sandbox by; the cere. prefix is reserved |
--allow RULE |
egress=[cere.Allow(...)] |
none | Hosts the sandbox may reach (Egress and secrets) |
-- command... |
positional arguments | sleep infinity |
The main process |
--wait-timeout |
wait_timeout= |
10m; 600 s | How long to wait for the sandbox to start, image pull included |
On devnet nodes that already run requests and limits, the quota and the memory limit can be higher than the reservation; see Resources and billing.
Limits on size and time are in Resources and billing.
Give the sandbox a main command
Section titled “Give the sandbox a main command”A command after -- (CLI) or positional arguments (Python) replace sleep infinity. The sandbox ends when that
process exits.
TICK=$(cere sandbox create --image busybox:1.36 --timeout 120 -q \ -- sh -c 'for i in 1 2 3; do echo "tick $i"; sleep 2; done')cere sandbox logs $TICKtick 1tick 2tick 3tick = cere.Sandbox.create("sh", "-c", 'for i in 1 2 3; do echo "tick $i"; sleep 2; done', image="busybox:1.36", timeout=120)for stream, text in tick.logs(): print(stream, text, end="")stdout tick 1stdout tick 2stdout tick 3List and find sandboxes
Section titled “List and find sandboxes”List the bucket’s sandboxes, newest first, filtered by status and labels; get one by its ID.
cere sandbox list --label app=docs --status runningcere sandbox get $APPID STATUS IMAGE NODE CREATED CPU-S EXIT LABELSsb-01m89qnh4w9k76n9mxmzf9e2t6 running python:3.12-slim 9f97fd7fc247 7s ago 0.00 - app=docsid sb-01m89qnh4w9k76n9mxmzf9e2t6status runningbucket <bucket-id>node 9f97fd7fc247038ffa7944d5fa8fbd23e85c9002360a1bfcda58683faffec8ccimage python:3.12-slimcommand sleep infinityresources cpu 0.5, memory 512 MiB, pids 2048timeout 300s (idle 120s)egress -env keys MODElabels app=docscreated 2026-11-25T16:12:22Zstarted 2026-11-25T16:12:22Zstartup 192 msended -exit -retained -billed 0.5 vCPU × 8.1 s = 4.1 CPU-s (measured 0.1), 0.5 GiB × 8.1 s = 4.1 GiB-s (measured 0.1)usage wall 8.1s, cpu 0.10s, memory peak 24 MiB, 0.08 GiB-s, net out 0 B in 628 B, disk peak 0.0 MiB--label repeats (all must match); --status is starting, running, terminated, failed or unknown;
--limit N (default 100) or --all.
for s in cere.Sandbox.list(label="app=docs", status="running"): print(s.object_id, s.status, s.data["labels"])
found = cere.Sandbox.from_id("sb-...") # an ID from the listprint(found.status, found.data["resources"], found.data["workdir"])sb-01m89qq3p4kjjwtbpxxzghc595 running {'app': 'docs'}running {'cpu': 0.5, 'memory_mib': 512, 'pids': 2048, 'disk_mib': 0, 'gpu': None} /workspace/applabel takes "k=v", a list of them or a dict (all must match); limit defaults to 100 (None for all).
found.data is the sandbox as the API last reported it; found.refresh() reads it again.
Run a command
Section titled “Run a command”exec runs a command in a running sandbox and returns its output and exit code.
cere sandbox exec $ID -- python -c "print(6 * 7)"cere sandbox exec $ID -- sh -c 'echo "to stderr" >&2; exit 3'; echo "exit code: $?"echo 'print("from stdin")' | cere sandbox exec $ID --stdin -- python -cere sandbox exec $ID --workdir /tmp --env GREETING=hello -- sh -c 'echo "$GREETING from $(pwd)"'42to stderrexit code: 3from stdinhello from /tmpcere sandbox exec writes the command’s stdout and stderr to its own and exits with the command’s exit code.
p = sb.exec("python", "-c", "print(6 * 7)")print(p.stdout.read(), end="")
p = sb.exec("sh", "-c", 'echo "to stderr" >&2; exit 3')print(p.wait(), repr(p.stderr.read()))
p = sb.exec("python", "-", stdin='print("from stdin")')print(p.stdout.read(), end="")
p = sb.exec("sh", "-c", 'echo "$GREETING from $(pwd)"', workdir="/tmp", env={"GREETING": "hello"})print(p.stdout.read(), end="")423 'to stderr\n'from stdinhello from /tmpp.stdout.read() and p.stderr.read() return everything once the command has ended; p.wait() returns the
exit code, also kept in p.returncode.
Each exec has a timeout: 600 seconds by default, at most 3600. When it passes, the call returns with exit code 124:
cere sandbox exec $ID --timeout 2 -- sleep 10; echo "exit code: $?"cere: the command did not finish within 2 s and was stoppedexit code: 124p = sb.exec("sleep", "10", timeout=2)print(p.wait(), p.timed_out)124 TrueStream output
Section titled “Stream output”Output arrives while the command runs. The CLI prints it as it comes; in Python, iterate over p.stdout for
lines as they arrive.
cere sandbox exec $ID -- python -u -c "import timefor i in range(3): print('step', i); time.sleep(1)" | while read -r line; do echo "$(date +%T) $line"; done19:12:35 step 019:12:36 step 119:12:37 step 2import time
p = sb.exec("python", "-u", "-c", "import time\nfor i in range(3):\n print('step', i); time.sleep(1)")for line in p.stdout: print(time.strftime("%H:%M:%S"), line, end="")print("exit code", p.wait(), "in", p.duration_ms, "ms")19:13:27 step 019:13:28 step 119:13:29 step 2exit code 0 in 3037 msp.iter_output() yields ("stdout" | "stderr", text) chunks in the order they arrive, for both streams:
p = sb.exec("sh", "-c", "echo out; echo err >&2; echo out again")for stream, chunk in p.iter_output(): print(stream, repr(chunk))stdout 'out\n'stdout 'out again\n'stderr 'err\n'Copy files in and out
Section titled “Copy files in and out”put writes a local file into the sandbox (parent directories are created) and get-file reads one out.
printf 'name,score\nada,3\nbob,5\n' > data.csvcere sandbox put $ID data.csv /workspace/in/data.csvcere sandbox exec $ID -- sh -c 'wc -l < /workspace/in/data.csv > /workspace/out.txt'cere sandbox get-file $ID /workspace/out.txtcere sandbox get-file $ID /workspace/out.txt out.txtecho "hello" | cere sandbox put $ID - /workspace/hello.txtwrote data.csv (23 B) to sb-01m89qng3kr30x9ba5xj2j80en:/workspace/in/data.csv3wrote sb-01m89qng3kr30x9ba5xj2j80en:/workspace/out.txt (2 B) to out.txtwrote - (6 B) to sb-01m89qng3kr30x9ba5xj2j80en:/workspace/hello.txtWithout a local path get-file writes to standard output; put with - reads standard input.
sb.write_file("/workspace/in/data.csv", "name,score\nada,3\nbob,5\n") # str or bytessb.exec("sh", "-c", "wc -l < /workspace/in/data.csv > /workspace/out.txt").wait()print(sb.read_file("/workspace/out.txt")) # bytes
with sb.open("/workspace/notes.txt", "w") as f: # text, written on close f.write("written with open()\n")print(sb.read_file("/workspace/notes.txt"))b'3\n'b'written with open()\n'A file is at most 16 MiB each way. A larger one, or a missing one, is refused:
head -c 17000000 /dev/zero > big.bincere sandbox put $ID big.bin /workspace/big.bincere sandbox get-file $ID /workspace/missing.txterror: 413 too_large: file is larger than the limiterror: 404 file_not_found: no file /workspace/missing.txt in sandbox sb-01m89qng3kr30x9ba5xj2j80entry: sb.read_file("/workspace/missing.txt")except cere.CereError as e: print(e.status, e.code)404 file_not_foundwrite_file does not check the size first: a write over 16 MiB is cut off by the API and fails with a connection
error (urllib.error.URLError), not a CereError. Check len(data) before you write.
Read the main process’s logs
Section titled “Read the main process’s logs”logs streams the stdout and stderr of the sandbox’s main process (the command it was created with) until it
ends. The example in Give the sandbox a main command reads them. Exec output is
not in the logs: it goes to the caller of exec.
Logs stay readable for 10 minutes after the sandbox ends. To keep output longer, write it to a file and retain it (below).
Terminate a sandbox and keep files
Section titled “Terminate a sandbox and keep files”Terminate a sandbox when you are done; billing stops when it stops. --retain (retain=) keeps files readable
after the end, and --reason (reason=) is recorded in its exit reason.
cere sandbox exec $ID -- sh -c 'echo "{\"answer\": 42}" > /workspace/result.json'cere sandbox terminate $ID --retain /workspace/result.json,/workspace/out.txt --reason "docs example done"sb-01m89qng3kr30x9ba5xj2j80en terminated: terminated by API: docs example donebilled 1.0 vCPU × 25.5 s = 25.5 CPU-s (measured 0.5), 1.0 GiB × 25.5 s = 25.5 GiB-s (measured 0.3)usage: wall 25.5s, cpu 0.54s, memory peak 18 MiB, 0.32 GiB-s, net out 210 B in 1.0 KiB, disk peak 0.0 MiBretained: /workspace/out.txt, /workspace/result.jsonsb.exec("sh", "-c", 'echo "{\\"answer\\": 42}" > /workspace/result.json').wait()sb.terminate(retain=["/workspace/result.json"], reason="docs example done")print(sb.status, sb.exit_reason, sb.data["retained"])terminated terminated by API: docs example done ['/workspace/result.json']Read a retained file after the end the same way as before it, from any machine signed in to the bucket:
cere sandbox get-file $ID /workspace/result.jsoncere sandbox get-file $ID /workspace/in/data.csv{"answer": 42}error: 410 file_gone: sandbox sb-01m89qng3kr30x9ba5xj2j80en has ended and /workspace/in/data.csv was not retainedsb = cere.Sandbox.from_id("sb-...")print(sb.read_file("/workspace/result.json"))try: sb.read_file("/workspace/in/data.csv")except cere.CereError as e: print(e.status, e.code)b'{"answer": 42}\n'410 file_gone| Retained files | |
|---|---|
| Size | Each file up to 16 MiB; one terminate keeps its files together within 64 MiB per bucket on the node, or keeps none |
| How long | 24 hours after the sandbox ends |
| A path that is missing or too large | Skipped; the terminate still succeeds, and retained lists what was kept |
| A file that was not retained | 410 file_gone, with the reason when the budget was the cause; a sandbox that kept no files answers 404 not_found once its logs are gone |
Terminating a sandbox that has already ended returns it unchanged.
Wait for a sandbox to end
Section titled “Wait for a sandbox to end”In Python, sb.wait() blocks until the sandbox ends (its main command exits, it times out or is terminated) and
returns its exit code; sb.poll() returns None while it runs. From the CLI, read its state with
cere sandbox get.
JOB=$(cere sandbox create --image busybox:1.36 --timeout 120 -q -- sh -c 'sleep 5; exit 7')cere sandbox get $JOB | grep -E '^(status|exit)'sleep 10cere sandbox get $JOB | grep -E '^(status|exit)'status runningexit -status terminatedexit exited with code 7 (exit code 7)job = cere.Sandbox.create("sh", "-c", "sleep 5; exit 7", image="busybox:1.36", timeout=120)print(job.poll())print(job.wait(), job.status, job.exit_reason)None7 terminated exited with code 7States and exit reasons
Section titled “States and exit reasons”cere sandbox get shows a sandbox’s status (starting, running, terminated, failed, unknown) and, once it
has ended, its exit reason, such as exited with code 7, timeout (30s) or terminated by API: <reason>. Every
status and exit reason is listed in Sandboxes: lifecycle.
Related
Section titled “Related”- Next: Resources and billing
- Sandboxes: lifecycle, isolation, images, limits
- CLI: every
cere sandboxflag - Python SDK:
cere.Sandboxandcere.Process