Skip to content

Quickstart: your first sandbox

In this tutorial, a Python program of your own creates a throw-away sandbox, runs one command in it, prints what the command printed, and checks that the sandbox is gone. It needs no AI account, no API key and no Git repository: only Docker Sandboxes and Python.

What you need

  • Docker Sandboxes, installed and its daemon running for your user, on Linux or macOS: sbx version answers. See Installation.
  • Python 3.12 or newer, and uv or pip.
  • Internet access on the first run: the daemon pulls the image of the shell kit once, and later runs reuse it.

1. Create a project

uv init hello-sandbox
cd hello-sandbox
uv add fastsandbox
mkdir hello-sandbox
cd hello-sandbox
python3 -m venv .venv
. .venv/bin/activate
pip install fastsandbox

With pip, run the commands of this page without uv run.

2. Check that fastsandbox reaches the daemon

uv run fastsandbox info

The output looks like this, with your own socket, versions and count:

socket       /home/you/.local/state/sandboxes/sandboxes/sandboxd/sandboxd.sock
sbx          v0.54.1 (healthy)
api          0.52.0 (supported >=0.52.0, <0.53.0)
sandboxes    0
kit cache    memory, kits forged without the sbx CLI

The api version must fall in the supported range, whose maximum is excluded. If you get an error instead, see If it fails.

3. Write the program

Create hello_sandbox.py in the project:

hello_sandbox.py
import fastsandbox


async def main() -> None:
    client = await fastsandbox.connect()
    async with client.create(kit="shell") as sandbox, sandbox.running():
        print("sandbox:", sandbox.name)
        output = await sandbox.exec(["echo", "Hello from the sandbox"])
        print(output.stdout, end="")
        print("exit code:", output.exit_code)
    # Leaving the block deleted the sandbox: the daemon no longer lists it.
    names = [info.name for info in await client.sandboxes.list()]
    print("still there:", sandbox.name in names)


if __name__ == "__main__":
    fastsandbox.run(main())

4. Run it

uv run python hello_sandbox.py

You get:

sandbox: fastsandbox-f0ca461b
Hello from the sandbox
exit code: 0
still there: False

The name of the sandbox ends with a random suffix. The first run waits while the daemon pulls the image of the kit; on a machine that already has it, the program takes a few seconds.

What the program did

  • Line 5: connect() finds the socket of the daemon, checks that the daemon is healthy and that its API version is in the supported range, and returns a Client.
  • Line 6: client.create(kit="shell") creates a sandbox from the shell kit, a plain Linux system. sandbox.running() holds a session while the block runs: a sandbox stays running only while a session is held.
  • Line 8: exec() runs a command to completion and returns an ExecOutput. A list is run as it is, with no shell; a string is run with sh -c. A non-zero exit code is not an error unless you pass check=True.
  • Lines 12 and 13: once the block has ended, the daemon no longer lists the sandbox.

Where the code runs

Your program runs on the host, with your user's rights. Only what it asks the sandbox to run, here echo, runs inside the sandbox. The isolation is that of Docker Sandboxes, one microVM per sandbox: fastsandbox drives it through the socket of the daemon and adds no isolation of its own. See Architecture.

Cleanup, and when it does not happen

The async with block deletes the sandbox when it ends: when the code in it returns, raises an exception, or is cancelled, Ctrl-C included. If the deletion itself fails, the block's own error goes on, with the failure noted on it, and a block that ended well raises the failure (see Leaving a block whose cleanup fails).

No code runs when the process is killed (kill -9, and a plain kill, which Python does not handle by default), crashes, or the machine stops; a second Ctrl-C can also interrupt the deletion. The sandbox then stays, stopped about 30 seconds later. sbx ls lists the sandboxes of your user, and sbx rm NAME removes one: check the name first, since the daemon holds the sandboxes of every program of your user. Sandboxes this program creates are named fastsandbox- and a random suffix.

What stays on purpose: the image of the shell kit, in the image store of the daemon, which the next sandboxes reuse. The lifecycle page lists what each case leaves.

If it fails

Symptom What to do
DaemonUnavailableError Docker Sandboxes is not installed, or its daemon does not run for this user: check that sbx version answers, as the same user.
UnsupportedVersionError The daemon speaks an API version this release of fastsandbox was not checked against: upgrade fastsandbox or sbx so that api falls in the range.
The first run waits for minutes The daemon pulls the image of the shell kit, once.

Troubleshooting covers the other errors.

Next