Sessions
A sandbox created without command or entrypoint is a session: an
interactive sandbox that accepts commands and file operations until you end
it. Its first process is the session server instead of the image’s default
program. Pass a command or entrypoint to run a
one-program sandbox instead.
Run a command
Section titled “Run a command”Call run to start a command, send its input, and wait for it to finish.
result = sandbox.run( 'echo "$GREETING from $(pwd) as $(whoami)"; cat', env={"GREETING": "hello"}, cwd="/tmp", user="nobody", stdin=b"and this came in on stdin\n",)print(result.exit_code)print(result.stdout.decode(), end="")const result = await sandbox.run('echo "$GREETING from $(pwd) as $(whoami)"; cat', { env: { GREETING: "hello" }, cwd: "/tmp", user: "nobody", stdin: bytes("and this came in on stdin\n"),});console.log(result.exitCode);console.log(text(result.stdout));let result = sandbox .run(r#"echo "$GREETING from $(pwd) as $(whoami)"; cat"#) .env("GREETING", "hello") .cwd("/tmp") .user("nobody") .stdin(b"and this came in on stdin\n") .await?;println!("{}", result.exit_code);print!("{}", String::from_utf8_lossy(&result.stdout));- Shell vs. argv. A string runs as a script via
/bin/bash -lcif the image has/bin/bash, or/bin/sh -cotherwise. A list runs directly as argv with no shell, finding the executable onPATH. - Result. Returns the exit code plus stdout and stderr as bytes. A
nonzero exit code is a result, not an error. A signal-killed command exits
with
128 + signal, a missing executable exits 127, and a non-runnable one exits 126 with the reason on stderr. - Environment and directory. Commands inherit the sandbox’s environment,
with
envoverlaid. Withoutcwd, they run in the image’s working directory. - User. Commands run as root unless
usersets another (same format asdocker exec --user: name or UID, optionally:groupor:gid, from/etc/passwdand/etc/group). An unknown user still starts the command, which writes the error to stderr and exits 126. - Input.
stdinis sent and closed; without it, input is empty.
Multiple commands can run concurrently in one session.
Start and interact with a command
Section titled “Start and interact with a command”Call start to launch a command and get a handle once it starts. Without
stdin, input stays open so you can write and close it later.
cat = sandbox.start(["cat"])cat.write_stdin(b"one ")cat.write_stdin(b"two\n")cat.close_stdin()print(cat.wait().stdout)
sleeper = sandbox.start(["sleep", "600"])sleeper.signal(15)print(sleeper.wait().exit_code)const cat = await sandbox.start(["cat"]);await cat.writeStdin(bytes("one "));await cat.writeStdin(bytes("two\n"));await cat.closeStdin();console.log(text((await cat.wait()).stdout));
const sleeper = await sandbox.start(["sleep", "600"]);await sleeper.signal(15);console.log((await sleeper.wait()).exitCode);let cat = sandbox.start(["cat"]).await?;cat.write_stdin(b"one ").await?;cat.write_stdin(b"two\n").await?;cat.close_stdin().await?;println!("{:?}", String::from_utf8_lossy(&cat.wait().await?.stdout));
let sleeper = sandbox.start(["sleep", "600"]).await?;sleeper.signal(15).await?;println!("{}", sleeper.wait().await?.exit_code);write_stdinqueues bytes without waiting for the command to read them.signal(n)sends signaln(1 to 64) to the command’s process group. The server sends no response; check the exit code.waitreturns the same result asrun. Output is collected as it arrives, so a command that writes a lot holds it all in memory until then.
Read and write files
Section titled “Read and write files”Call write_file to write a whole file and read_file to read one, as bytes.
sandbox.write_file("/tmp/hello.sh", b"#!/bin/sh\necho hello from a file\n", mode=0o755)print(sandbox.run(["/tmp/hello.sh"]).stdout)print(sandbox.read_file("/etc/os-release").decode().splitlines()[0])await sandbox.writeFile("/tmp/hello.sh", bytes("#!/bin/sh\necho hello from a file\n"), { mode: 0o755 });console.log(text((await sandbox.run(["/tmp/hello.sh"])).stdout));console.log(text(await sandbox.readFile("/etc/os-release")).split("\n")[0]);sandbox .write_file("/tmp/hello.sh", b"#!/bin/sh\necho hello from a file\n") .mode(0o755) .await?;let hello = sandbox.run(["/tmp/hello.sh"]).await?;println!("{:?}", String::from_utf8_lossy(&hello.stdout));let release = sandbox.read_file("/etc/os-release").await?;println!( "{}", String::from_utf8_lossy(&release) .lines() .next() .unwrap_or(""));- New files get mode
0o644minus the umask unless you pass a mode; existing files keep their mode. - The parent directory must exist; none is created.
- Writes are not atomic; a failed write can leave a partial file.
read_filereads regular files only; directories and missing files fail with the reason.- Relative paths start from the image’s working directory.
To read files after exit, seek to a point (a recorded moment in the run) (Inspecting a finished run).
End the session
Section titled “End the session”Call end to close session input and wait for exit. Running commands are
killed. Returns the sandbox’s exit code (0 once the session server stops).
print(sandbox.end())console.log(await sandbox.end());println!("{}", sandbox.end().await?);Resume a paused sandbox before ending it, or stop it instead. A with
block, await using, or scope handles cleanup automatically
(Python, TypeScript,
Rust).
Only sandboxes that exited (as end makes them) can be inspected. Stopped
sandboxes cannot.
Pause, resume, stop
Section titled “Pause, resume, stop”These controls work on any sandbox; see One-program sandboxes.
pausefreezes the sandbox;resumecontinues it.stopterminates it immediately. Stopped sandboxes cannot be inspected.statereturnsqueued,running, orpaused, thenexited(with exit code),stopped, orfailed(with reason).