Skip to content

Docker Sandboxes for Python

Run code and
coding agents
in isolated sandboxes.

A typed async Python client and a declarative task framework for Docker Sandboxes (sbx). Each run gets its own microVM, with network rules and keys scoped to it, and is removed when it ends.

Requires Docker Sandboxes on the machine: fastsandbox connects to the socket of its daemon.

An independent project, not affiliated with Docker, Inc.

fastsandbox is a Python package for Docker Sandboxes: a typed async client of its daemon, and FastSandbox, a framework in which a function runs in a throw-away sandbox at each call. Docker Sandboxes provides the isolation, a microVM per sandbox; fastsandbox drives it from Python and decides what crosses into each sandbox: host directories, network rules, credentials.

Install

uv add fastsandbox      # or: pip install fastsandbox
  • Requires Docker Sandboxes on the machine, its daemon running for your user, on Linux or macOS, and Python 3.12 or newer. fastsandbox connects to the socket of the daemon and does nothing without it.
  • Status: alpha. The API may change before 1.0, and each release supports a range of versions of the private socket API of the daemon. See Compatibility.

A task in a sandbox

first_app.py
import fastsandbox
from fastsandbox import Context, FastSandbox

app = FastSandbox("hello")


@app.task
async def uname(ctx: Context) -> str:
    """Says which system the sandbox runs."""
    output = await ctx.sandbox.exec("uname -sm", check=True)
    return output.stdout.strip()


if __name__ == "__main__":
    print(fastsandbox.run(uname))

fastsandbox run first_app.py uname creates a sandbox from the shell kit, runs uname -sm in it, prints the result and removes the sandbox.

agent.py
from pathlib import Path
from typing import Annotated

from fastsandbox import Context, FastSandbox, WorkspaceDir
from fastsandbox.exceptions import TaskError

app = FastSandbox("quickstart")


@app.agent("claude", timeout="15m")
async def explain(ctx: Context, repo: Annotated[Path, WorkspaceDir(clone=True)]) -> str:
    """Has Claude Code explain a project, working on a private clone of it."""
    workdir = str(repo.resolve())
    run = await ctx.agent.prompt(
        "Explain what this project does and how its code is laid out, "
        "in twenty lines at most, and write the explanation to EXPLAINED.md.",
        working_dir=workdir,
    )
    if not run.ok:
        raise TaskError(f"Claude Code exited with {run.exit_code}: {run.stderr.strip()}")
    # The file is in the clone, inside the sandbox: the host is untouched.
    return await ctx.sandbox.read_text(f"{workdir}/EXPLAINED.md")

fastsandbox run agent.py explain --repo . runs Claude Code on a private clone of the repository, in its own sandbox; your directory is not changed. It needs a Claude subscription or an Anthropic API key.

The function runs on the host, in your process; only what it asks the sandbox to run, a command or an agent, runs inside it.

Start here

  • Run a command


    A sandbox, one command, its output, and the sandbox gone. No AI account needed.

    Quickstart

  • Run a task


    A typed function run in a fresh sandbox at each call, from Python or the command line.

    Your first task

  • Run a coding agent


    Claude Code, Codex or seven other agents at work on a private clone of a repository.

    Your first coding agent

  • Build an environment


    Kits and mixins: the tools, files and allowed hosts a sandbox starts with.

    Kits

What it gives you

  • Tasks from functions. @app.task and @app.agent declare the kit, network rules, deadline and credentials of each run's sandbox; the signature validates the inputs and the result. FastSandbox
  • Every route of the daemon, typed. An async client with Pydantic models for every payload, and an error class for each kind of failure. The client
  • Coding agents, one way. The nine agents sbx knows, run unattended, streamed event by event, or in an interactive session. Agents
  • Network rules per sandbox. Hosts allowed or denied for one sandbox, on top of the daemon's global policy, which they do not replace. Network model
  • Keys held by the proxy. For most agents, an API key goes to the proxy of one sandbox, which writes it into the requests to its service; the sandbox holds a placeholder. Credentials
  • Tests without a daemon. A fake daemon serves the socket API in memory, for any CI. Testing

fastsandbox is an independent project, not affiliated with Docker, Inc. Why fastsandbox · Architecture · Security model