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 versionanswers. See Installation. - Python 3.12 or newer, and uv or pip.
- Internet access on the first run: the daemon pulls the image of the
shellkit once, and later runs reuse it.
1. Create a project¶
With pip, run the commands of this page without uv run.
2. Check that fastsandbox reaches the daemon¶
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:
4. Run it¶
You get:
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 aClient. - Line 6:
client.create(kit="shell")creates a sandbox from theshellkit, 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 anExecOutput. A list is run as it is, with no shell; a string is run withsh -c. A non-zero exit code is not an error unless you passcheck=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¶
- Your first task: the same work, declared as a task that the framework runs in a fresh sandbox at each call.
- Your first coding agent: a coding agent at work on a private clone of a repository.
- Choose your API: the client, sandbox specs and tasks side by side.