Sandbox Management
Sandboxes are isolated development environments managed by Daytona. This guide covers how to create, manage, and remove Sandboxes using the SDK. By default, Sandboxes auto-stop after 15 minutes of inactivity and use 1 vCPU, 1GB RAM, and 3GiB disk.
Creating Sandboxes
Daytona SDK provides an option to create Sandboxes with default or custom configurations. You can specify the language, Snapshot, resources, environment variables, and volumes for the Sandbox.
By default, the Sandboxes auto-stop after 15 minutes
of inactivity in order to save resources but the timeout can be increased.
Basic Sandbox Creation
Daytona SDK provides methods to create Sandboxes with default configurations, specific languages, or custom labels using Python and TypeScript.
from daytona import Daytona, CreateSandboxFromSnapshotParams
daytona = Daytona()
# Create a basic Sandbox
sandbox = daytona.create()
# Create a Sandbox with specific language
params = CreateSandboxFromSnapshotParams(language="python")sandbox = daytona.create(params)
# Create a Sandbox with custom labels
params = CreateSandboxFromSnapshotParams(labels={"SOME_LABEL": "my-label"})sandbox = daytona.create(params)
import { Daytona } from '@daytonaio/sdk';
const daytona = new Daytona();
// Create a basic Sandboxconst sandbox = await daytona.create();
// Create a Sandbox with specific languageconst sandbox = await daytona.create({ language: 'typescript' });
// Create a Sandbox with custom labelsconst sandbox = await daytona.create({ labels: { SOME_LABEL: 'my-label' } });
Sandbox Resources
Daytona Sandboxes come with 1 vCPU, 1GB RAM, and 3GiB disk by default.
Need more power? Use the Resources
class to define exactly what you need: CPU, memory, and disk space are all customizable.
Check your available resources and limits in the dashboard.
from daytona import Daytona, Resources, CreateSandboxFromImageParams, Image
daytona = Daytona()
# Create a Sandbox with custom resources
resources = Resources( cpu=2, # 2 CPU cores memory=4, # 4GB RAM disk=8, # 8GB disk space)
params = CreateSandboxFromImageParams( image=Image.debian_slim("3.12"), resources=resources)
sandbox = daytona.create(params)
import { Daytona, Image } from "@daytonaio/sdk";
async function main() { const daytona = new Daytona();
// Create a Sandbox with custom resources const sandbox = await daytona.create({ image: Image.debianSlim("3.13"), resources: { cpu: 2, // 2 CPU cores memory: 4, // 4GB RAM disk: 8, // 8GB disk space }, });}
main();
Sandbox Information
Daytona SDK provides methods to get information about a Sandbox, such as ID, root directory, and status using Python and TypeScript.
# Get Sandbox IDsandbox_id = sandbox.id
# Get the root directory of the Sandbox user
root_dir = sandbox.get_user_root_dir()
# Get the Sandbox id, auto-stop interval and state
print(sandbox.id)print(sandbox.auto_stop_interval)print(sandbox.state)
// Get Sandbox IDconst sandboxId = sandbox.id;
// Get the root directory of the Sandbox userconst rootDir = await sandbox.getUserRootDir();
// Get the Sandbox id, auto-stop interval and stateconsole.log(sandbox.id)console.log(sandbox.autoStopInterval)console.log(sandbox.state)
To get the preview URL for a specific port, check out Preview & Authentication.
Stop & Remove Sandbox
Daytona SDK provides methods to stop and delete Sandboxes using Python and TypeScript.
# Stop Sandboxsandbox.stop()
# Delete Sandbox
sandbox.delete()
// Stop Sandboxawait sandbox.stop();
// Delete Sandboxawait sandbox.delete();
Sandbox States and Persistence
Daytona keeps the filesystem in its entirety during the Sandbox lifecycle. The persistence functionality is built into the system, and nothing needs to be explicitly done from the user side.
Auto-stop Interval
It is important to understand the Sandbox states to maintain cost-effectiveness. One useful built-in feature is the auto-stop interval. By default, it triggers after 15 minutes
of inactivity by stopping the Sandbox, but the number can changed to any value, including 0
which disables auto-stopping.
sandbox = daytona.create(CreateSandboxFromSnapshotParams( snapshot="my-snapshot-name", auto_stop_interval=0, # Disables the auto-stop feature - default is 15 minutes))
const sandbox = await daytona.create({ snapshot: "my-snapshot-name", autoStopInterval: 0, // Disables the auto-stop feature - default is 15 minutes});
Auto-archive Interval
Another useful built-in feature is the auto-archive interval. By default, it triggers after a Sandbox has been continuously stopped for 7 days
by archiving it, but the number can changed to any value, including 0
which means the maximum interval of 30 days
will be used.
sandbox = daytona.create(CreateSandboxFromSnapshotParams( snapshot="my-snapshot-name", auto_archive_interval=60 # Auto-archive after a Sandbox has been stopped for 1 hour))
const sandbox = await daytona.create({ snapshot: "my-snapshot-name", autoArchiveInterval: 60 // Auto-archive after a Sandbox has been stopped for 1 hour});
A Daytona Sandbox can have three states during its lifecycle:
Running
Running Sandboxes utilize CPU, memory, and disk storage. Every resource is charged per second of usage. When Sandboxes are not actively used, it is recommended that they be stopped. This can be done:
- Manually using the stop command
- Automatically by setting the autoStop interval
Stopped
Stopped Sandboxes only utilize disk storage. They can be instantly started when needed. The stopped state should be used when the Sandbox is expected to be started again soon. Otherwise, it is recommended to archive the Sandbox to eliminate disk usage costs.
Archived
When Sandboxes are archived, the entire filesystem state is moved to very cost-effective object storage, making it possible to keep Sandboxes available for an extended period.
Performance Considerations
The tradeoff between archived and stopped states is that starting an archived Sandbox takes more time, depending on its size.