# Templates

Build your own template from a minisandbox.yaml recipe, version it and roll it back.

A template is what a miniVM starts from: a disk and a memory snapshot, built from a `minisandbox.yaml` recipe. Your tools are installed, your service is already running, and every new miniVM starts from that point.

Templates belong to your organization. Every build makes a new immutable **version** (`v1`, `v2`…). A miniVM always keeps the version it started from.

## Base templates

Start your recipe `from:` one of these, or from your own image or Dockerfile.

| Template     | What it holds                                                                                                                                                                                                                                                                    |
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dev-base`   | Ubuntu 24.04, git, the GitHub CLI `gh`, build tools, and Docker (the daemon is running in the snapshot; `user` is in the `docker` group). Egress presets: `ubuntu`, `github`, `docker-hub`, `ghcr`, `pypi`, `npm`, `crates`.                                                     |
| `dev-agents` | `dev-base` plus Node.js, pnpm, Python 3.12, uv, Rust, and the coding agents Claude Code, Codex and OpenCode. Egress presets: those of `dev-base` plus `llm-apis` (the model APIs and Claude sign-in). It is the base of [agent runs](https://onshore.sh/docs/guides/agent-runs). |

Every field of a recipe is described in the [`minisandbox.yaml` reference](https://onshore.sh/docs/reference/minisandbox-yaml).

### What `dev-agents` puts on the `PATH`

- Node.js 26 is in `/opt/node/bin`.
- The coding agents and the pnpm they use (a recent major) are in `/home/user/.agents-tools/node_modules/.bin`, first on the `PATH`. A project that pins another pnpm in its `packageManager` field gets that pnpm only if you put it first: run it with `npx pnpm@<version>`, or install it in `setup` to a directory you put ahead on the `PATH` in your recipe's `env`.
- `sudo` keeps its own `PATH`, without `/opt/node/bin`: `sudo npm` answers `command not found`. Call Node tools without `sudo`, or by their full path (`sudo /opt/node/bin/npm`).

On the managed service, base templates are bases for your recipes, not templates you run: a create (`ons create --template`, `Sandbox.create`) names a template of your organization. `ons create` and the E2B SDKs use your organization's template `base` when you name none. It is requested when you sign up, and built once your organization can run builds. On a [Hybrid](https://onshore.sh/docs/guides/hybrid) pool, you run the public templates `dev-base` and `dev-agents` directly.

> **Note:** A recipe `from:` a base template inherits its disk and environment variables, not its `egress`, `resources`, `ports` or `start`. Write the egress you need in your own recipe.

## Network access of the `base` template

Your organization's `base` template reaches the package registries and nothing else: Ubuntu
(`apt-get`), PyPI (`pip`), npm and crates.io (`cargo`), the presets `ubuntu`, `pypi`, `npm` and
`crates`. So `apt-get install` and `pip install` work in a miniVM created with no template. The
template has Python but not Node.js or Rust: install them first
(`sudo apt-get install -y nodejs npm`, `sudo apt-get install -y cargo`), then `npm install` and
`cargo build` reach their registries. GitHub, Docker Hub and every other host are refused
until you allow them. Add them when you create the miniVM:

### ons

```bash
ons create --json --template base --egress-add github
```

### Python

```python
from e2b import Sandbox

sbx = Sandbox.create(network={"allow_out": [
    "pypi.org", "files.pythonhosted.org", "github.com", "api.github.com",
]})
```

### JavaScript

```javascript
import { Sandbox } from 'e2b'

const sbx = await Sandbox.create({
  network: { allowOut: ['pypi.org', 'files.pythonhosted.org', 'github.com', 'api.github.com'] },
})
```

- `--egress-add` adds to what the template allows. It takes presets (`github`, `docker-hub`,
  `ghcr`, `llm-apis`, …) or domain names. See [Presets](https://onshore.sh/docs/guides/egress#presets) for the hosts of
  each preset.
- `network.allowOut` sets the miniVM's whole list, by domain name: the template's names it does
  not repeat are closed. Name the registries you still need, as above for PyPI.
- On a running miniVM, run `ons egress allow <minivm-id> github`. `<minivm-id>` is the id
  `ons create` printed.
- A name that is not allowed fails as a DNS error inside the miniVM ("Temporary failure in name
  resolution"). To see which names were refused, within about a minute:
  `ons egress denials <minivm-id>`, or, without `ons`,
  `GET /v2/sandboxes/<minivm-id>/egress/denials` with your API key: see
  [See what a miniVM was refused](https://onshore.sh/docs/guides/egress#see-what-a-minivm-was-refused). With the E2B SDK,
  `<minivm-id>` is `sbx.sandbox_id` (`sbx.sandboxId` in JavaScript).
- `--egress-allow` only narrows to names the template already allows. Use `--egress-add`, which
  adds names up to your organization's ceiling.
- Templates you build yourself keep the rule: their egress is closed unless their recipe lists it.
  See [Egress](https://onshore.sh/docs/guides/egress).
- An organization created before 30 November 2026 keeps the `base` built for it, which may allow
  less, often no egress at all. `ons egress ls <minivm-id>` shows what a miniVM allows. Add the
  registries when you create the miniVM:
  `ons create --template base --egress-add ubuntu --egress-add pypi --egress-add npm --egress-add crates`.

## Build your first template

1. **Write a recipe**

   In the root of your project, run:

   ```bash
   ons template init
   ```

   It writes a minimal `minisandbox.yaml` named after the directory: `from: dev-base`, or `dockerfile: ./Dockerfile` when the directory has a Dockerfile. Then edit it. For example:

   ```yaml minisandbox.yaml
   version: 1
   name: my-api
   from: dev-agents
   resources:
     vcpu: 2
     memory: 4GiB
   files:
     - src: .
       dest: /home/user/app
       ignore: [node_modules, .git]
   workdir: /home/user/app
   setup:
     - uv venv && uv pip install -r requirements.txt
   start:
     cmd: .venv/bin/python -m http.server 8000
     ready:
       http: http://localhost:8000/
   egress:
     presets: [pypi, github]
   ports: [8000]
   ```

   Every field is described in the [minisandbox.yaml reference](https://onshore.sh/docs/reference/minisandbox-yaml).

2. **Check it offline**

   ```bash
   ons template validate
   ons template build --dry-run
   ```

   Errors name the file, line and column, and what to do:

   ```text
   minisandbox.yaml:12:3: unknown field `setpu` · did you mean `setup`?
   ```

3. **Build it**

   ```bash
   ons template build
   ```

   `ons` packs and uploads the files the recipe copies, starts the build and follows its logs to the end. Pass `--no-wait` to return once the build is queued, then follow it later:

   ```bash
   ons template logs <build-id>
   ```

   `<build-id>` is the id `ons template build` printed.

4. **Start a miniVM from it**

   ```bash
   ons create --template my-api
   ```

   With the E2B SDK, pass the name as the template: `Sandbox.create("my-api")` in Python, `Sandbox.create('my-api')` in JavaScript.

## What a build does

1. **Source.** It pulls the `image`, builds the `dockerfile`, or starts from the `from` template's disk. Images come from public registries.
2. **Files.** It copies each `files` item to its `dest`, and creates the recipe's `user` (default `user`) if the image lacks it: see [`user`](https://onshore.sh/docs/reference/minisandbox-yaml#user).
3. **Setup.** It boots a miniVM with the recipe's resources and egress, and runs `setup` line by line. A non-zero exit fails the build.
4. **Snapshot.** It starts `start`, waits for its `ready` check, then snapshots memory and disk.
5. **Publish.** The new version becomes `ready` and current.

A failed build publishes nothing: the current version stays as it was. Build logs say which phase failed and why.

### Writing `setup`

- **Each line runs in its own shell**, as the recipe's `user` (default `user`, with passwordless `sudo`). Join commands that share variables or a directory on one line.
- **Services do not start during `setup`.** Package installs cannot start daemons in a build (`invoke-rc.d: policy-rc.d denied execution of start`). To have a service such as PostgreSQL running in every miniVM, start it in `start`, with a `ready` check: it is running in the snapshot, so each miniVM starts with it.
- **Install packages with `--no-install-recommends`.** A recommended package can break the miniVM's network. For example `systemd-resolved` replaces `/etc/resolv.conf` with a link that does not exist in a miniVM, and every name lookup then fails with `Temporary failure in name resolution` (`EAI_AGAIN`), with nothing in `ons egress denials`. If a package must bring it, restore the file at the end of `setup`.
- **A host `setup` needs must be in the recipe's `egress`.** The build reaches only what the recipe allows.

The files you send for a build are deleted within 7 days. The recipe and the built image stay with the version.

When nothing that changes the disk has changed since a ready version, the build reuses that version's result instead of building again. If only `start`, `resources`, `ports` or `lifecycle` changed, the build is refused with `409`: a rebuild of the snapshot alone is not available yet. Change `setup` or `files` to build again, or keep that version's values.

### Faster Ubuntu packages in builds

On the managed service, template builds cache Ubuntu packages fetched over HTTP from
`snapshot.ubuntu.com`: the `RUN` lines of a `dockerfile:` and the `setup` lines of every recipe.
miniVMs started from the template later do not use that cache. A plain `apt-get --snapshot <date>`
does not use it either: apt fetches the snapshot over HTTPS. Point the snapshot at the HTTP address
to use it, in one line, since each `setup` line runs in its own shell:

```yaml
setup:
  - snapshot="--snapshot 20261117T000000Z -o Acquire::Snapshots::URI::Override::Origin::Ubuntu=http://snapshot.ubuntu.com/ubuntu/@SNAPSHOTID@/" && sudo apt-get update $snapshot && sudo apt-get install -y $snapshot <package>
```

- `<package>` is the Ubuntu package to install, for example `ffmpeg`.
- `20261117T000000Z` is a snapshot id: a UTC time as `YYYYMMDDTHHMMSSZ` that Ubuntu has
  published, such as this one, which `dev-base` was built with.
- In a Dockerfile `RUN` line, write the same line without `sudo`: it runs as root.

apt still checks the signed Release files and the hash of every package, over HTTP as over HTTPS.
The recipe needs the `ubuntu` egress preset (or `snapshot.ubuntu.com` in its `allow` list):
without it, the name does not resolve during the build and apt reports that it cannot resolve
`snapshot.ubuntu.com`.

## Versions and rollback

```bash
ons template ls
ons template versions my-api
ons template rollback my-api <version>
```

- `ls` lists your organization's templates with their current version.
- `versions` lists the versions of one template, each with its status and digest. The current one is marked `*`.
- `rollback` makes an older **ready** version current again. `<version>` is its number or its id, from `ons template versions`.

New miniVMs start from the current version. Running and paused miniVMs keep theirs.

A version that is no longer current is deleted 90 days after it stopped being current, once no miniVM or paused miniVM uses it.

## In the console

**Templates** in the console lists your organization's templates with their version, size and status. **Build history** shows a template's builds and their logs. When the `base` template the service builds for a new organization failed, **Retry** asks for that build again (for example, after you add a payment method). Build your own templates with `ons`.

## Several templates in one repository

Keep one recipe per template, for example in `.minisandbox/<name>.yaml`, and pass it with `--file`:

```bash
ons template build --file .minisandbox/<name>.yaml
```

`<name>` is the template's file name. Paths in `files` and `dockerfile` are relative to the recipe's own directory.

## Keep secrets out of templates

Everything in a template is copied into every miniVM started from it. Never put a secret in `env`, `files`, `setup` or a Dockerfile `ARG`, and never run `gh auth login` or a similar sign-in in `setup`: the token would be frozen in every snapshot.

Pass credentials when you create the miniVM instead. Environment variable values you pass at create are not stored: they are kept only while the miniVM runs. `env` values in a recipe are stored with the template version. [Egress credentials](https://onshore.sh/docs/guides/egress) add a secret header to the miniVM's outbound requests without the miniVM ever seeing it.

With a Dockerfile, `ons` warns when the build context holds `.git` or a `.env*` file. Add them to `.dockerignore`.

## Limits

- Recipe: 256 KiB.
- Files a build uploads: 2 GiB and 100 000 entries, after `ignore`.
- `start.ready.timeout`: at most 10 minutes.
- Builds per organization and per key are rate limited. See [Quotas and limits](https://onshore.sh/docs/account/quotas-and-limits).

## Related

- [minisandbox.yaml reference](https://onshore.sh/docs/reference/minisandbox-yaml)
- [Egress](https://onshore.sh/docs/guides/egress)
- [CLI reference](https://onshore.sh/docs/reference/cli)

Markdown version: https://onshore.sh/docs/guides/templates.md
