Sandbox templates
Sandbox templates allow you to customize the sandbox environment to your needs.
To create a sandbox template, you specify the e2b.Dockerfile
. We then take this Dockerfile and create a new sandbox template from it and give you back a template ID.
You can then use this template ID to create a new sandbox with the SDK based on the template you created.
How to create custom sandbox
Steps
- Install E2B CLI
- Initialize sandbox template
- Customize
e2b.Dockerfile
- Build your sandbox template
- Start your custom sandbox
1. Install E2B CLI
Install E2B CLI with npm:
npm i @e2b/cli
2. Initialize sandbox template
The following command will create a basic e2b.Dockerfile
in the current directory.
e2b template init
3. Customize e2b.Dockerfile
Now you can customize your sandbox template by editing the e2b.Dockerfile
file.
e2b.Dockerfile
# Make sure to use this base image
FROM e2bdev/code-interpreter:latest
# Install some Python packages
RUN pip install cowsay
4. Build your sandbox template
Now you can build your sandbox template. We'll use Docker and the E2B CLI. What is going to happen is that E2B CLI will call Docker to build the image and then push it to the E2B cloud. Then we convert the Docker image to a micro VM that can be then launched as a sandbox with our SDK.
e2b template build -c "/root/.jupyter/start-up.sh"
This process will take a moment. In the end, you'll see your template ID that you'll need to use to create a sandbox with the SDK.
5. Start your custom sandbox
Now you can use the template ID to create a sandbox with the SDK.
import { sandbox } from '@e2b/code-interpreter'
// Your template ID from the previous step
const templateID = 'id-of-your-template'
// Pass the template ID to the `Sandbox.create` method
const sandbox = await Sandbox.create(templateID)
// The template installed cowsay, so we can use it
const execution = await sandbox.runCode(`
import cowsay
cowsay.say('Hello from E2B!')
`)
console.log(execution.stdout)