# Custom templates
URL: https://docs.munito.ai/sandboxes/custom-templates

A custom template is a container image that you deploy to your workspace under a name. Munito AI builds the image into a template one time. Each sandbox of the template then starts with the files of the image.

- The image comes from a public registry, such as Docker Hub, or from a private registry.
- The image is for linux/amd64.
- A sandbox of a custom template has each feature of a sandbox: sizes, larger disks, commands, files, ports, SSH, persistence and fork.

## Deploy an image [#deploy-an-image]

The `munito templates` commands need version 0.3.0 or later of the [command line tool](https://docs.munito.ai/cli).

```bash
munito templates deploy acme-agent ghcr.io/acme/agent:1.4
```

<Accordions>
<Accordion title="The same call with curl">

```bash
curl -sS --fail-with-body https://api.munito.ai/sandboxes/v1/templates \
  -H "Authorization: Bearer $MUNITO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "acme-agent", "image": "ghcr.io/acme/agent:1.4"}'
```

</Accordion>
</Accordions>

The API answers at once with the template, and the build continues. A build takes from seconds to minutes. A larger image takes more time.

- A name has 1 to 63 lower case letters, digits and hyphens. It starts and ends with a letter or a digit.
- The name of a built-in template, such as `base`, is not available.
- A new name makes a new template. Its `id` starts with `tpl_`.
- A name that exists gets a new version. The last ready version starts new sandboxes until the new version is ready.
- Give the image with a tag or a digest. An image without a tag or a digest gets the tag `latest`.
- An image name without a registry, such as `python:3.12-slim`, is on Docker Hub.

The [Sandboxes console](https://console.munito.ai/console/sandboxes) also deploys, shows and deletes templates.

## Read the build [#read-the-build]

```bash
munito templates get acme-agent --wait
```

`--wait` reads the template every 5 seconds until its build ends. When the build fails, the command shows the error and the end of the build output, and exits with code 1.

<Accordions>
<Accordion title="The same call with curl">

```bash
curl -sS --fail-with-body https://api.munito.ai/sandboxes/v1/templates/acme-agent \
  -H "Authorization: Bearer $MUNITO_API_KEY"
```

```json
{
  "id": "tpl_01k8f6x7g3d2b9v5n4m1q0w8e6",
  "object": "template",
  "name": "acme-agent",
  "status": "ready",
  "version": 1,
  "image": "ghcr.io/acme/agent:1.4",
  "digest": "sha256:4f1c...",
  "size_bytes": 412090368,
  "build": {
    "version": 1,
    "image": "ghcr.io/acme/agent:1.4",
    "status": "ready",
    "digest": "sha256:4f1c...",
    "error": null,
    "log": "",
    "started_at": 1793016000,
    "ended_at": 1793016042
  },
  "created_at": 1793016000,
  "updated_at": 1793016042
}
```

</Accordion>
</Accordions>

| Field                                      | Meaning                                                                                                  |
| ------------------------------------------ | -------------------------------------------------------------------------------------------------------- |
| `status`                                   | `ready` when a version starts sandboxes. Before that, `building`, or `failed` if the newest build failed |
| `version`, `image`, `digest`, `size_bytes` | The version that new sandboxes start from. `null` before a version is ready                              |
| `build`                                    | The newest version. It can build or fail while an older version starts new sandboxes                     |
| `build.error`                              | Why the build failed: a `code` and a `message`                                                           |
| `build.log`                                | The end of the build output, when the build failed                                                       |

`munito templates ls` and `GET /templates` list the templates of your workspace, by name.

## Start a sandbox [#start-a-sandbox]

Set the name of the template, or its `id`, in `template`:

```bash
munito create --template acme-agent --size small
munito run "python3 --version" --template acme-agent
```

<Accordions>
<Accordion title="The same call with curl">

```bash
curl -sS --fail-with-body https://api.munito.ai/sandboxes/v1/sandboxes \
  -H "Authorization: Bearer $MUNITO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"template": "acme-agent", "size": "small"}'
```

</Accordion>
</Accordions>

- The sandbox starts from the ready version of the template.
- A sandbox of a custom template has 512 MiB of memory or more, so the `tiny` size is not available.
- The sandbox boots from the files of the template, and does not restore from a snapshot. It starts in about a second.
- A host that does not have the template yet downloads it before the first start.

| Code                 | Status | When                                                  |
| -------------------- | ------ | ----------------------------------------------------- |
| `template_not_found` | 404    | The workspace has no template with this name or `id`  |
| `template_not_ready` | 409    | The template has no ready version. Wait for its build |

## Private registry [#private-registry]

Give a user name and a password, or an access token, in `registry_auth`:

```bash
echo "$REGISTRY_TOKEN" | munito templates deploy acme-agent ghcr.io/acme/agent:1.4 \
  --username acme --password-stdin
```

<Accordions>
<Accordion title="The same call with curl">

```bash
curl -sS --fail-with-body https://api.munito.ai/sandboxes/v1/templates \
  -H "Authorization: Bearer $MUNITO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "acme-agent", "image": "ghcr.io/acme/agent:1.4", "registry_auth": {"username": "acme", "password": "'"$REGISTRY_TOKEN"'"}}'
```

</Accordion>
</Accordions>

- The build uses the credential one time. Munito AI does not keep it, and no answer contains it.
- Each deploy of a private image needs the credential again.
- The command line tool reads the token from standard input with `--password-stdin`, or from the variable `MUNITO_REGISTRY_TOKEN`. It has no flag for the token, so the token stays out of your shell history.
- Use a token that can only read images. For GitHub Container Registry, use a token with the `read:packages` scope.

## What runs in the sandbox [#what-runs-in-the-sandbox]

- Commands run as `root` in `/workspace`, with the environment (`ENV`) of the image.
- The `ENTRYPOINT` and the `CMD` of the image do not run. To start the server of the image, run its command as a [background command](https://docs.munito.ai/sandboxes/commands).
- The `WORKDIR` of the image is not the work folder. To run a command there, set `cwd`.
- A string command runs in `bash -lc`. If the image has no bash, the command runs in `/bin/sh`.
- If the image has no shell, the command runs in a BusyBox shell. Munito AI adds it in `/opt/munito`.
- SSH, ports, files, persistence and fork operate as in the other templates.

## Limits and price [#limits-and-price]

| Limit                                      | Value                                           |
| ------------------------------------------ | ----------------------------------------------- |
| Platform of the image                      | linux/amd64                                     |
| Files of the image                         | 9 GiB or less. The disk of a template is 10 GiB |
| Time of a build                            | 20 minutes or less                              |
| Templates in a workspace                   | 10                                              |
| Builds at the same time in an organization | 2                                               |

- During the preview, the build and the storage of a template are free.
- A sandbox of a custom template pays the [price of its size](https://docs.munito.ai/sandboxes/pricing), as each sandbox does.
- To get a larger root disk, set `disk_gib` when you create the sandbox.

| Code              | Status | When                                                                     |
| ----------------- | ------ | ------------------------------------------------------------------------ |
| `invalid_request` | 422    | The name or the image name is not valid                                  |
| `template_limit`  | 409    | The workspace has 10 templates. Delete one, or deploy a name that exists |
| `build_limit`     | 429    | Your organization has 2 builds now. Deploy again when one ends           |
| `no_capacity`     | 503    | No host has room for the build. Retry after the `Retry-After` seconds    |

## Delete a template [#delete-a-template]

```bash
munito templates rm acme-agent
```

<Accordions>
<Accordion title="The same call with curl">

```bash
curl -sS --fail-with-body -X DELETE https://api.munito.ai/sandboxes/v1/templates/acme-agent \
  -H "Authorization: Bearer $MUNITO_API_KEY"
```

</Accordion>
</Accordions>

- A delete removes the template and each of its versions.
- The sandboxes that run from the template continue to run.
- A delete cannot be reversed. To use the image again, deploy it again.
