Skip to main content
This tutorial shows you how to configure a sandbox at startup by passing options to Sandbox.run(). It’s for developers who have already created a basic sandbox and want to control the container image, resource allocation, and pre-populated files for their workload.

Pass configuration to Sandbox.run()

Configuration options control how the sandbox is provisioned. Pass them as keyword arguments directly to Sandbox.run():
from cwsandbox import Sandbox

with Sandbox.run(
    container_image="python:3.11",
    max_lifetime_seconds=300,
    tags=["my-app"],
) as sandbox:
    sandbox.exec(["python", "--version"]).result()

Resources

Configure CPU and memory to match your workload’s needs and avoid over-provisioning or under-provisioning. Use ResourceOptions to set requests and limits:
from cwsandbox import ResourceOptions, Sandbox

with Sandbox.run(
    resources=ResourceOptions(
        requests={"cpu": "1", "memory": "1Gi"},
        limits={"cpu": "2", "memory": "2Gi"},
    ),
) as sandbox:
    sandbox.exec(["python", "compute.py"]).result()
ResourceOptions uses Kubernetes resource syntax:
ResourceFormatExamples
CPUCores or millicores"1", "2", "500m" (0.5 CPU)
MemoryBytes with unit suffix"512Mi", "1Gi", "2Gi"

Mounted files

Mounted files let you ship configuration, scripts, or other static content into the sandbox before it starts, so your workload has what it needs the moment it runs. Pre-populate files at sandbox startup:
with Sandbox.run(
    mounted_files=[
        {"path": "/app/config.json", "content": '{"debug": true}'},
        {"path": "/app/script.py", "content": "print('hello')"},
    ]
) as sandbox:
    sandbox.exec(["python", "/app/script.py"]).result()
Mounted files are read-only. Use write_file() for files that need modification.

Next steps

You now have a sandbox that starts with the container image, resource allocation, and pre-mounted files your workload requires. For reusable configuration across multiple sandboxes, use SandboxDefaults. See the sandbox configuration guide for all ResourceOptions fields, GPU resources, QoS classes, and config-library interop.
Last modified on May 29, 2026