# Sandbox templates



A Sandbox template is a reusable recipe for a Sandbox: a base image, a size,
packages, one setup step, and the services it exposes. Templates belong to an
organization and are managed with `ccp apply`.

## Create a template [#create-a-template]

Write a manifest:

```yaml title="sandbox.yaml"
apiVersion: sandboxes.clusterbase.ai/v1
kind: SandboxTemplate
metadata:
  name: python-tools
spec:
  base: debian
  resources:
    vcpu: 2
    memory_mb: 1024
  packages:
    apt: [jq, python3, python3-venv]
  ports:
    - name: cdp
      port: 9222
      protocol: websocket
  steps:
    - run: |
        python3 -m venv /opt/python
        /opt/python/bin/pip install cowsay==6.1
        mkdir -p /workspace
```

Apply it and wait for the build:

```bash
ccp apply --file sandbox.yaml --wait
```

Then launch Sandboxes from it:

```bash
ccp sandbox create --template python-tools --ttl 30m
```

## Manifest reference [#manifest-reference]

| Field            | Required | Description                                                                 |
| ---------------- | -------- | --------------------------------------------------------------------------- |
| `metadata.name`  | Yes      | Template name, unique in the organization. Used by `--template`.            |
| `spec.base`      | Yes      | Base image. See [Bases](#bases).                                            |
| `spec.resources` | Yes      | `vcpu` and `memory_mb`. See [Sizes](#sizes).                                |
| `spec.packages`  | No       | Package lists by manager: `apt`, `apk`, `pip`, `npm`, `cargo`, `gem`, `go`. |
| `spec.ports`     | No       | Up to eight services. See [Ports](#ports).                                  |
| `spec.steps`     | Yes      | Exactly one `run` script, executed after packages install.                  |

Manifests can be YAML, JSON, or TOML (files ending in `.toml` are read as TOML).
Unknown fields are rejected, so secrets, environment variables, and IDs can't be
added to a manifest. Keep manifests safe to commit.

### Bases [#bases]

| Base          | Package managers                          | Notes                                                                                             |
| ------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------- |
| `debian`      | `apt`                                     | Minimal. Install language runtimes yourself.                                                      |
| `alpine`      | `apk`                                     | Minimal. Install language runtimes yourself.                                                      |
| `development` | `apt`                                     | The built-in `development` image with its tools, idle pause, and public `preview` port.           |
| `browser`     | `apt`, `pip`, `npm`, `cargo`, `gem`, `go` | The browser image managed agents use.                                                             |
| `desktop`     | `apt`                                     | The built-in graphical desktop, with idle pause, the desktop viewer, and a public `preview` port. |

A package list for a manager the base doesn't support is rejected before the
build starts.

On `development` and `desktop`, a template with no `spec.ports` keeps the
base's ports, and idle pause applies either way. Declaring any ports replaces
the base's ports entirely, including the public `preview` service and, on
`desktop`, the desktop viewer used by `ccp sandbox desktop`. On `development`
and `desktop`, install other language packages in the `run` step.

### Sizes [#sizes]

`spec.resources` must be one of these pairs:

| `vcpu` | `memory_mb` |
| ------ | ----------- |
| 1      | 256         |
| 1      | 512         |
| 2      | 1024        |
| 4      | 2048        |
| 4      | 4096        |

### Ports [#ports]

Each entry in `spec.ports` has:

| Field      | Description                                                                 |
| ---------- | --------------------------------------------------------------------------- |
| `name`     | Lowercase DNS label, unique in the template. Used by `ccp sandbox connect`. |
| `port`     | Guest port, unique in the template.                                         |
| `protocol` | `http` or `websocket`.                                                      |

Declaring a port doesn't start anything: your setup must start the service
and listen on the guest's network interface. Declared services are private;
reach them with [`ccp sandbox connect`](/docs/ccp/sandboxes#connect-to-a-service).

## Updates and builds [#updates-and-builds]

`ccp apply` is safe to repeat. An unchanged manifest reuses the existing build.
Changing the base, resources, packages, ports, or setup step creates a new
build; existing builds never change. New Sandboxes use the latest ready build.
Sandboxes already running keep the build they started with.

| Flag        | Effect                                                                                                                                  |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `--dry-run` | Show what would change without writing anything.                                                                                        |
| `--wait`    | Wait up to 30 minutes for the build to be ready. Progress goes to stderr. Exits nonzero if the build fails, is cancelled, or times out. |
| `--json`    | Print the apply result as JSON. With `--wait`, it includes the final status.                                                            |

Stopping `--wait` doesn't cancel the build. `--wait` can't be combined with
`--dry-run`.

## Manage templates in the console [#manage-templates-in-the-console]

**Build → Sandboxes → Templates** in the console lists your templates with
their builds and build logs. Archive a template from its detail page when you no
longer need it.

See [`ccp apply`](/docs/ccp/cli/apply) for every flag.
