Skip to content

Declarative Builder

View as Markdown

Declarative Builder provides a powerful, code-first approach to defining dependencies for Daytona Sandboxes. Instead of importing images from a container registry, you can programmatically define them using the Daytona SDK.

The declarative builder system supports two primary workflows:

  1. Declarative images: build images with varying dependencies on demand when creating sandboxes
  2. Pre-built Snapshots: create and register ready-to-use Snapshots that can be shared across multiple sandboxes

Build declarative images

Daytona provides an option to create declarative images on-the-fly when creating sandboxes. This is ideal for iterating quickly without creating separate snapshots.

Declarative images are cached for 24 hours, and are automatically reused when running the same script. Thus, subsequent runs on the same runner will be almost instantaneous.

# Define a declarative image with python packages
declarative_image = (
Image.debian_slim("3.12")
.pip_install(["requests", "pytest"])
.workdir("/home/daytona")
)
# Create a new sandbox with the declarative image and stream the build logs
sandbox = daytona.create(
CreateSandboxFromImageParams(image=declarative_image),
timeout=0,
on_snapshot_create_logs=print,
)

For more information, see the Python SDK and TypeScript SDK references:

CreateSandboxFromImageParams (Python SDK)

CreateSandboxFromImageParams (TypeScript SDK)

Create pre-built Snapshots

Daytona provides an option to create pre-built snapshots that can be reused across multiple sandboxes.

The snapshot remains visible in the Daytona Dashboard ↗ and is permanently cached, ensuring instant availability without rebuilding.

# Create a python data science image
snapshot_name = "data-science-snapshot"
image = (
Image.debian_slim("3.12")
.pip_install(["pandas", "numpy"])
.workdir("/home/daytona")
)
# Create the snapshot and stream the build logs
daytona.snapshot.create(
CreateSnapshotParams(
name=snapshot_name,
image=image,
),
on_logs=print,
)
# Create a new sandbox using the pre-built snapshot
sandbox = daytona.create(
CreateSandboxFromSnapshotParams(snapshot=snapshot_name)
)

For more information, see the Python SDK and TypeScript SDK references:

CreateSnapshotParams (Python SDK)

CreateSnapshotParams (TypeScript SDK)

Image configuration

Daytona provides an option to define images programmatically using the Daytona SDK. You can specify base images, install packages, add files, set environment variables, and more.

For a complete API reference and method signatures, see the Python and TypeScript SDK references.

Base image selection

Daytona provides an option to select base images. The following snippets demonstrate how to select and configure base images:

# Create an image from a base
image = Image.base("python:3.12-slim-bookworm")
# Use a Debian slim image with Python 3.12
image = Image.debian_slim("3.12")

For more information, see the Python SDK and TypeScript SDK references:

base (Python SDK)

base (TypeScript SDK)

debian_slim (Python SDK)

debianSlim (TypeScript SDK)

Package management

Daytona provides an option to install packages and dependencies to your image. The following snippets demonstrate how to install packages and dependencies to your image:

# Add pip packages
image = Image.debian_slim("3.12").pip_install("requests", "pandas")
# Install from requirements.txt
image = Image.debian_slim("3.12").pip_install_from_requirements("requirements.txt")
# Install from pyproject.toml (with optional dependencies)
image = Image.debian_slim("3.12").pip_install_from_pyproject("pyproject.toml", optional_dependencies=["dev"])

For more information, see the Python SDK and TypeScript SDK references:

pip_install (Python SDK)

pipInstall (TypeScript SDK)

pip_install_from_requirements (Python SDK)

pipInstallFromRequirements (TypeScript SDK)

pip_install_from_pyproject (Python SDK)

pipInstallFromPyproject (TypeScript SDK)

File system operations

Daytona provides an option to add files and directories to your image. The following snippets demonstrate how to add files and directories to your image:

# Add a local file
image = Image.debian_slim("3.12").add_local_file("package.json", "/home/daytona/package.json")
# Add a local directory
image = Image.debian_slim("3.12").add_local_dir("src", "/home/daytona/src")

For more information, see the Python SDK and TypeScript SDK references:

add_local_file (Python SDK)

add_local_dir (Python SDK)

addLocalFile (TypeScript SDK)

addLocalDir (TypeScript SDK)

Environment configuration

Daytona provides an option to configure environment variables and working directories. The following snippets demonstrate how to configure environment variables and working directories:

# Set environment variables
image = Image.debian_slim("3.12").env({"PROJECT_ROOT": "/home/daytona"})
# Set working directory
image = Image.debian_slim("3.12").workdir("/home/daytona")

For more information, see the Python SDK and TypeScript SDK references:

env (Python SDK)

workdir (Python SDK)

env (TypeScript SDK)

workdir (TypeScript SDK)

Commands and entrypoints

Daytona provides an option to execute commands during build and configure container startup behavior. The following snippets demonstrate how to execute commands during build and configure container startup behavior:

# Run shell commands during build
image = Image.debian_slim("3.12").run_commands(
'apt-get update && apt-get install -y git',
'groupadd -r daytona && useradd -r -g daytona -m daytona',
'mkdir -p /home/daytona/workspace'
)
# Set entrypoint
image = Image.debian_slim("3.12").entrypoint(["/bin/bash"])
# Set default command
image = Image.debian_slim("3.12").cmd(["/bin/bash"])

For more information, see the Python SDK and TypeScript SDK references:

run_commands (Python SDK)

entrypoint (Python SDK)

cmd (Python SDK)

runCommands (TypeScript SDK)

entrypoint (TypeScript SDK)

cmd (TypeScript SDK)

Dockerfile integration

Daytona provides an option to integrate existing Dockerfiles or add custom Dockerfile commands. The following snippets demonstrate how to integrate existing Dockerfiles or add custom Dockerfile commands:

# Add custom Dockerfile commands
image = Image.debian_slim("3.12").dockerfile_commands(["RUN echo 'Hello, world!'"])
# Use an existing Dockerfile
image = Image.from_dockerfile("Dockerfile")
# Extend an existing Dockerfile
image = Image.from_dockerfile("app/Dockerfile").pip_install(["numpy"])

For more information, see the Python SDK and TypeScript SDK references:

dockerfile_commands (Python SDK)

from_dockerfile (Python SDK)

dockerfileCommands (TypeScript SDK)

fromDockerfile (TypeScript SDK)