DEV Community

Sandscale
Sandscale

Posted on Originally published at sandscale.dev

Custom sandbox templates: give your agent's sandboxes your toolchain, pinned by digest

Short answer: if every agent run starts by installing the same compilers and packages, put them in an image instead. Build and push the image yourself, pin its reference by digest (name@sha256:...), register it with sandscale sandbox template build, and start sandboxes with --template NAME. A digest names one exact image, so every run gets the same bits, and a changed upstream tag cannot change your agent's environment underneath it.

All commands here are from the live sandscale sandbox CLI reference, which is generated from the CLI itself.

Template, snapshot or setup script?

Setup script per run Snapshot Template
What it is Commands the agent runs first An archive of a sandbox's /workspace An image pinned by digest, with a default size
Good for Small, fast installs A prepared project directory (dependencies installed, data unpacked) System packages, toolchains, anything outside /workspace
Needs network at run time Usually No No

A snapshot saves only /workspace, so system-level tools belong in a template. The two combine: start from a template, then from a snapshot of a prepared project. See starting sandboxes from a prepared snapshot.

1. Build and push your image

Sandscale builds no images; that part is yours, with whatever you already use. A minimal example:

FROM python:3.12-slim
RUN pip install --no-cache-dir pandas==2.2.3 pytest==8.3.3
Enter fullscreen mode Exit fullscreen mode

Build it, push it to your registry, and note the digest your registry reports for the pushed image.

2. Write the template file

sandscale sandbox template init --name data-py --image registry.example.com/team/data-py@sha256:DIGEST
Enter fullscreen mode Exit fullscreen mode

init writes the template file in the current folder and sends nothing. Use your own registry path, and put the digest your registry reported in place of DIGEST. Keeping the file in the repository next to the Dockerfile means a template change goes through review like any other change.

3. Register it

sandscale sandbox template build --version 1.0.0 --cpus 2 --mem 4096
Enter fullscreen mode Exit fullscreen mode

build registers the template the file and the options describe. --cpus, --mem and --disk set its default size. The vCPU you can use per box depends on your plan (Free up to 2, Pro and Team up to 16), and each vCPU comes with 4 GiB of memory; memory above that counts as extra vCPU-hours.

sandscale sandbox template list shows the templates you may use, and sandscale sandbox template delete NAME --yes removes one.

4. Start sandboxes from it

From the CLI:

sandscale sandbox start --template data-py --ttl 1h --network none
sandscale sandbox run --sync . --template data-py -- pytest -q
Enter fullscreen mode Exit fullscreen mode

From the TypeScript SDK, template takes a template ID of your organization:

const box = await sandscale.sandboxes.create({ template: 'TEMPLATE_ID', timeoutSeconds: 1800 });
Enter fullscreen mode Exit fullscreen mode

Use the ID that sandscale sandbox template list shows. With a template, nonzero cpuCount values must match it; the server checks.

Updating a template

Rebuild and push the image, take the new digest, update the reference in the template file, and run template build again with a new --version. With the template file in git, its history shows which image each version pointed to. Reproducible agent runs covers what else to pin.

Policy still applies

Which templates, sizes and run times members may use is set by your organization's policies, and a request outside them is refused before anything starts. A custom template does not get around that. Every Sandscale sandbox started from it is still its own Firecracker microVM.

FAQ

How do I create a custom sandbox template in Sandscale?

Build and push your own image, run sandscale sandbox template init with the image reference pinned by digest, then sandscale sandbox template build to register it. Start sandboxes with --template NAME.

Does Sandscale build the image for me?

No. The CLI reference says Sandscale builds no images. You build and push the image yourself and register its digest-pinned reference as a template.

Why pin the image by digest instead of a tag?

A tag can point to a different image tomorrow. A digest names exactly one image, so every sandbox from the template starts with the same contents.

Should I use a template or a snapshot?

Use a template for system packages and toolchains, and a snapshot for a prepared /workspace, such as installed project dependencies. A snapshot covers only /workspace.

Related

Originally published on the Sandscale blog.

Top comments (0)