axio-tools-docker¶
Docker sandbox tools for axio.
DockerSandbox is an async context manager that spins up an isolated Docker
container on entry and removes it on exit. Inside the context it exposes six
axio tools - the same shell, write_file, read_file, list_files,
run_python, and patch_file as axio-tools-local, but every operation runs
inside the container, never on the host.
Requirements¶
Docker must be installed and running:
docker info # should succeed
The package talks to the Docker Engine API directly via
aiodocker - the docker CLI is not
required.
Installation¶
pip install axio-tools-docker
Quick start¶
import asyncio
from axio.agent import Agent
from axio.context import MemoryContextStore
from axio.testing import StubTransport, make_text_response
from axio_tools_docker import DockerSandbox
async def main() -> None:
transport = StubTransport([make_text_response("Done.")])
async with DockerSandbox(image="python:3.12-alpine") as sandbox:
agent = Agent(
system="You are a coding assistant. Use the sandbox tools to run code safely.",
tools=sandbox.tools,
transport=transport,
)
ctx = MemoryContextStore()
result = await agent.run("Print hello from Python.", ctx)
print(result)
asyncio.run(main())
Sandbox tools¶
These mirror axio-tools-local exactly - same names and field schemas:
Tool |
Description |
|---|---|
|
Run a shell command; returns stdout + stderr. Supports |
|
Create or overwrite a file. Parent directories are created automatically. |
|
Read a file with optional |
|
List a directory; directories first with a trailing |
|
Execute a Python snippet in a subprocess. Supports |
|
Replace lines |
Container lifecycle¶
The container is created on __aenter__ and removed on __aexit__ (docker rm -f).
Cleanup runs even when the body raises an exception:
from axio_tools_docker import DockerSandbox
from axio import Agent
async def run(ctx):
agent = Agent(
system="You are a coding assistant. Use the sandbox tools to run code safely.",
tools=ctx.sandbox.tools,
)
async with DockerSandbox(image="alpine:latest") as sandbox:
await agent.run("...", ctx)
# container removed here (unless remove=False)
The image is pulled automatically if not present locally. If the Docker daemon
is unreachable, __aenter__ raises immediately:
RuntimeError: Docker daemon not available at 'unix:///var/run/docker.sock': ...
The running container’s ID is available as sandbox.container_id inside the
async with block.
Named containers and reuse¶
Pass name= to give the container a fixed name. If a container with that name
already exists, the sandbox starts it if needed and attaches instead of creating
a new one. Attached containers are never removed on exit - regardless of
remove:
import asyncio
from axio_tools_docker import DockerSandbox
async def first_session() -> None:
async with DockerSandbox(image="python:3.12-slim", name="my-sandbox", remove=False) as sb:
await sb.exec("pip install requests")
async def second_session() -> None:
async with DockerSandbox(name="my-sandbox") as sb:
result = await sb.exec("python3 -c 'import requests; print(requests.__version__)'")
asyncio.run(first_session())
asyncio.run(second_session())
Named volumes¶
Named volumes are managed by the Docker daemon and persist across container restarts. Use them to share state between sandbox sessions:
import asyncio
from axio_tools_docker import DockerSandbox
async def main() -> None:
async with DockerSandbox(
image="python:3.12-alpine",
named_volumes={"/data": "my-project-data"},
) as sb:
await sb.write_file("/data/state.json", '{"count": 1}')
# Container removed, volume survives.
async with DockerSandbox(
image="python:3.12-alpine",
named_volumes={"/data": "my-project-data"},
volumes_remove=True, # delete the volume on exit
) as sb:
raw = await sb.read_file_bytes("/data/state.json")
assert raw.decode() == '{"count": 1}'
asyncio.run(main())
Docker creates the volume automatically if it does not exist yet.
Configuration¶
import os
from axio_tools_docker import DockerSandbox
sandbox = DockerSandbox(
os.getenv("DOCKER_HOST", "unix:///var/run/docker.sock"), # Docker daemon URL, optional
image="python:3.12-slim",
memory="512m", # memory limit: "256m", "1g", …
cpus="2.0", # CPU limit
network=False, # False=none, True=default, str=explicit mode
workdir="/workspace",
volumes={"/workspace": "/tmp/host-dir"}, # {container_path: host_path}
named_volumes={"/data": "my-project-data"}, # named Docker volumes
volumes_remove=False, # remove named volumes on exit
env={"PYTHONPATH": "/app"},
user="nobody",
name="my-agent-sandbox",
remove=False,
read_only=True, # read-only root filesystem
shm_size="64m", # /dev/shm size
cap_add=["NET_ADMIN"], # add Linux capabilities
cap_drop=["ALL"], # drop Linux capabilities
privileged=False,
ulimits={"nofile": (1024, 65536), "nproc": 512},
tmpfs={"/tmp": "size=128m,mode=1777"},
ports={8080: 8080}, # {container_port: host_port}
platform="linux/amd64",
extra_hosts={"host.docker.internal": "host-gateway"},
devices=["/dev/net/tun", "/dev/sda:/dev/xvda:r"],
dns=["8.8.8.8", "1.1.1.1"],
)
assert sandbox.image == "python:3.12-slim"
assert sandbox.memory == "512m"
assert sandbox.cpus == "2.0"
assert sandbox.network == False
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Docker daemon URL (unix socket or TCP). Positional. |
|
|
|
Container image. Pulled automatically if not present. |
|
|
|
Memory limit. Accepts |
|
|
|
CPU limit as a decimal string. |
|
|
|
Network mode. |
|
|
|
Working directory inside the container. |
|
|
|
Bind mounts as |
|
|
|
Named Docker volumes as |
|
|
|
Remove named volumes on exit. No effect when attached to an existing container. |
|
|
|
Environment variables passed to all commands. |
|
|
|
User to run as (e.g. |
|
|
|
Container name. Attaches to existing container if found; creates new one otherwise. |
|
|
|
Remove container on exit. No effect when attached to an existing container. |
|
|
|
Read-only root filesystem. Combine with |
|
|
|
|
|
|
|
Linux capabilities to add (e.g. |
|
|
|
Linux capabilities to drop (e.g. |
|
|
|
Extended privileges - full capability set and device access. Use with care. |
|
|
|
Resource limits. |
|
|
|
Tmpfs mounts as |
|
|
|
Port bindings as |
|
|
|
Platform override (e.g. |
|
|
|
Extra |
|
|
|
Host devices to expose. Format: |
|
|
|
DNS servers (e.g. |
Part of the axio ecosystem¶
License¶
MIT