Overview

Custom templates

A template sets the base image, environment variables, files to copy, setup commands and a start command.

Scaffold with the CLI#

e2b template init

Or set it up by hand (requires E2B SDK 2.3.0 or newer):

1
Install the packages

npm install e2b dotenv or pip install e2b python-dotenv, and put E2B_API_KEY=e2b_*** in .env.

2
Define the template

Describe the image in template.ts or template.py.

3
Write a build script

One script per environment — a dev tag and a production tag.

4
Build

npx tsx build.dev.ts or python build_dev.py.

5
Start sandboxes from it

Pass the tag to Sandbox.create().

Define#

// template.ts
import { Template, waitForTimeout } from 'e2b'
 
export const template = Template()
  .fromBaseImage()
  .setEnvs({
    HELLO: 'Hello, World!',
  })
  .setStartCmd('echo $HELLO', waitForTimeout(5_000))
# template.py
from e2b import Template, wait_for_timeout
 
template = (
    Template()
    .from_base_image()
    .set_envs({"HELLO": "Hello, World!"})
    .set_start_cmd("echo $HELLO", wait_for_timeout(5_000))
)

Build#

// build.dev.ts
import 'dotenv/config'
import { Template, defaultBuildLogger } from 'e2b'
import { template } from './template'
 
await Template.build(template, 'template-tag-dev', {
  cpuCount: 1,
  memoryMB: 1024,
  onBuildLogs: defaultBuildLogger(),
})
# build_dev.py
from dotenv import load_dotenv
from e2b import Template, default_build_logger
from template import template
 
load_dotenv()
 
Template.build(
    template,
    'template-tag-dev',
    cpu_count=1,
    memory_mb=1024,
    on_build_logs=default_build_logger(),
)

Builds can run for up to 1 hour. CPU, memory, disk and concurrent-build limits depend on your plan.

Use#

import { Sandbox } from 'e2b'
 
const dev = await Sandbox.create('template-tag-dev')
const prod = await Sandbox.create('template-tag')
from e2b import Sandbox
 
dev = Sandbox.create(template="template-tag-dev")
prod = Sandbox.create(template="template-tag")

Many similar templates#

Layer per-customer or per-project config on a shared base with fromTemplate() / from_template(). Builds reuse the base's cached layers, so they finish faster.

import { Template } from 'e2b'
 
export const template = Template()
  .fromTemplate('my-base-template')
  .copy('./customers/acme', '/app/config')
  .setEnvs({ CUSTOMER_ID: 'acme' })
from e2b import Template
 
template = (
    Template()
    .from_template("my-base-template")
    .copy("./customers/acme", "/app/config")
    .set_envs({"CUSTOMER_ID": "acme"})
)

Next#

Updated

Was this page helpful?