Skip to main content
A Sandbox object lets you interact with a running sandbox. This API is similar to Python’s asyncio.subprocess.Process. Refer to the sandbox guide on how to spawn and use sandboxes.

Methods

Sandbox.create

modal.Sandbox.create(
    *args: str,
    app: Optional[App] = None,
    name: Optional[str] = None,
    image: Optional[Image] = None,
    env: Optional[dict[str, Optional[str]]] = None,
    secrets: Optional[Collection[Secret]] = None,
    network_file_systems: dict[Union[str, os.PathLike], NetworkFileSystem] = {},
    timeout: int = 300,
    idle_timeout: Optional[int] = None,
    workdir: Optional[str] = None,
    gpu: GPU_T = None,
    cloud: Optional[str] = None,
    region: Optional[Union[str, Sequence[str]]] = None,
    cpu: Optional[Union[float, tuple[float, float]]] = None,
    memory: Optional[Union[int, tuple[int, int]]] = None,
    block_network: bool = False,
    cidr_allowlist: Optional[Sequence[str]] = None,
    volumes: dict[Union[str, os.PathLike], Union[Volume, CloudBucketMount]] = {},
    pty: bool = False,
    encrypted_ports: Sequence[int] = [],
    h2_ports: Sequence[int] = [],
    unencrypted_ports: Sequence[int] = [],
    custom_domain: Optional[str] = None,
    proxy: Optional[Proxy] = None,
    verbose: bool = False,
    client: Optional[Client] = None,
    environment_name: Optional[str] = None,
) -> Sandbox
Create a new Sandbox to run untrusted, arbitrary code.
args
str
Command and arguments to run in the sandbox (sets the CMD).
app
App
required
Associate the sandbox with an app. Required unless creating from a container.
name
str
Optional name for the sandbox. Unique within an app.
image
Image
Image to run as the container for the sandbox.
env
dict[str, str]
Environment variables to set in the Sandbox.
secrets
Collection[Secret]
Secrets to inject into the Sandbox as environment variables.
timeout
int
default:"300"
Maximum lifetime of the sandbox in seconds.
idle_timeout
int
Idle time in seconds before termination.
workdir
str
Working directory of the sandbox.
gpu
GPU_T
GPU configuration (e.g., “A100”, “T4”).
cpu
Union[float, tuple[float, float]]
CPU cores (request) or (request, limit).
memory
Union[int, tuple[int, int]]
Memory in MiB (request) or (request, limit).
block_network
bool
default:"False"
Whether to block network access.
volumes
dict
Mount points for Modal Volumes and CloudBucketMounts.
pty
bool
default:"False"
Enable a PTY for the Sandbox.
encrypted_ports
Sequence[int]
List of ports to tunnel with TLS encryption.
sandbox
Sandbox
The created Sandbox instance.
Example:
app = modal.App.lookup('sandbox-hello-world', create_if_missing=True)
sandbox = modal.Sandbox.create("echo", "hello world", app=app)
print(sandbox.stdout.read())
sandbox.wait()

sandbox.wait

sandbox.wait() -> int
Wait for the sandbox to finish and return its exit code.
exit_code
int
Exit code of the sandbox process.

sandbox.terminate

sandbox.terminate()
Terminate the sandbox.

sandbox.poll

sandbox.poll() -> Optional[int]
Check if the sandbox has finished. Returns exit code if finished, None otherwise.
exit_code
Optional[int]
Exit code if finished, None if still running.

sandbox.exec

sandbox.exec(
    *args: str,
    pty: bool = False,
) -> ContainerProcess
Execute a command in the running sandbox.
args
str
required
Command and arguments to execute.
pty
bool
default:"False"
Enable PTY for the exec session.
process
ContainerProcess
Process handle for the executed command.
Example:
sb = modal.Sandbox.create("sleep", "infinity", app=app)
process = sb.exec("echo", "hello")
stdout = process.stdout.read()
print(stdout)  # "hello"

Properties

sandbox.stdin

sandbox.stdin -> StreamWriter
Stream for writing to the sandbox’s stdin.

sandbox.stdout

sandbox.stdout -> StreamReader
Stream for reading from the sandbox’s stdout.

sandbox.stderr

sandbox.stderr -> StreamReader
Stream for reading from the sandbox’s stderr.

sandbox.returncode

sandbox.returncode -> Optional[int]
Return code of the sandbox if it has finished, None otherwise.

Build docs developers (and LLMs) love