E2B templates allow you to define custom sandboxes.
You can define the base image, environment variables, files to copy, commands to run, and
a start command that will be already running when you spawn the sandbox.
This way, you can have fully configured sandboxes with running processes ready to use with zero wait time for your users.
There are two ways how you can start creating a new template:
- using the CLI
- manually using the SDK
CLI
You can use the E2B CLI to create a new template.
Install the E2B CLI
Install the latest version of the E2B CLI Follow the prompts
Follow the prompts to create a new template.
Done
Check the generated README.md file to see how to build and use your new template.
Manual
Install the packages
Requires the E2B version at least 2.1.0
Create the .env file
Create a new template file
Create a template file with the following name and content
import { Template, waitForTimeout } from "e2b";
export const template = Template()
.fromBaseImage()
.setEnvs({
HELLO: "Hello, World!",
})
.setStartCmd("echo $HELLO", waitForTimeout(5_000));
Create a development build script
import "dotenv/config";
import { template } from "./template";
import { Template } from "e2b";
async function main() {
await Template.build(template, {
alias: "template-tag-dev",
cpuCount: 1,
memoryMB: 1024,
onBuildLogs: (logEntry) => console.log(logEntry.toString()),
});
}
main().catch(console.error);
Build the development template
Create a production build script
import "dotenv/config";
import { template } from "./template";
import { Template } from "e2b";
async function main() {
await Template.build(template, {
alias: "template-tag",
cpuCount: 1,
memoryMB: 1024,
onBuildLogs: (logEntry) => console.log(logEntry.toString()),
});
}
main().catch(console.error);
Build the production template
Create a new Sandbox from the Template
import "dotenv/config";
import { Sandbox } from "e2b";
// Create a Sandbox from development template
const sandbox = await Sandbox.create("template-tag-dev");
// Create a Sandbox from production template
const sandbox = await Sandbox.create("template-tag");
The template alias is the identifier that can be used to create a new Sandbox.