Skip to main content
Templates let you create reusable sandbox images from Dockerfiles. Pre-install languages, tools, and dependencies so your sandboxes start ready to go.

Building a Template

from opensandbox import Sandbox

sandbox = await Sandbox.create()

template = await sandbox.templates.build('python-ml', """
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip3 install numpy pandas scikit-learn
""")

print(template.template_id)  # tpl-abc123
print(template.status)       # building → ready

await sandbox.templates.build(name, dockerfile)

Builds a new template from a Dockerfile.
name
str
required
A unique name for the template.
dockerfile
str
required
The Dockerfile content to build from.
Returns: TemplateInfo

Listing Templates

templates = await sandbox.templates.list()

for t in templates:
    print(f"{t.name} ({t.tag}) — {t.status}")

await sandbox.templates.list()

Returns all templates. Returns: list[TemplateInfo]

Getting a Template

template = await sandbox.templates.get('python-ml')
print(template)

await sandbox.templates.get(name)

Gets a template by name.
name
str
required
Template name.
Returns: TemplateInfo

Deleting a Template

await sandbox.templates.delete('python-ml')

await sandbox.templates.delete(name)

Deletes a template by name.
name
str
required
Template name to delete.
Returns: None

TemplateInfo

TemplateInfo is a dataclass:
FieldTypeDescription
template_idstrUnique template identifier
namestrTemplate name
tagstrImage tag
statusstrBuild status: "building", "ready", "error"

Using a Template

Once built, pass the template name when creating a sandbox:
async with Sandbox.create(template='python-ml') as sandbox:
    result = await sandbox.commands.run(
        'python3 -c "import numpy; print(numpy.__version__)"'
    )
    print(result.stdout)  # 1.24.0