In this guide, we’ll create a custom E2B sandbox with preinstalled dependencies and files.
Once the sandbox is built, we’ll show how to create and control it with our SDK.
Before you create your first custom sandbox, you will need to authenticate in the CLI with your E2B account.
Run the following command in your terminal.
Copy
Ask AI
e2b auth login
You need to have an existing E2B account to login. Sign up here.
Now it’s time to create your custom sandbox based on the sandbox template file (the e2b.Dockefile file) you just created in the previous step.Run the following command inside the template file directory in your terminal.
Pro users can use the --cpu-count= (docs) and --memory-mb= (docs) flags to customize the sandbox compute.
Read more about the compute here.
Copy
Ask AI
e2b template build --name "my-agent-sandbox"
Use the .dockerignore file to exclude files from the sandbox template.
The final output should look similar to this.
Copy
Ask AI
Preparing sandbox template building (1 files in Docker build context).Found ./e2b.Dockerfile that will be used to build the sandbox template.Started building the sandbox template my-agent-sandbox# Truncated for visibility# ...# ...Running postprocessing. It can take up to few minutes.Postprocessing finished.✅ Building sandbox template my-agent-sandbox finished.┌ Usage examples ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐│ ││ You can use E2B Python or JS SDK to spawn sandboxes now. ││ Find more here - https://e2b.dev/docs/legacy/guide/custom-sandbox in Spawn and control your sandbox section. ││ ││───────────────────────────────────────────────────────────────────── Python SDK ─────────────────────────────────────────────────────────────────────││ ││ from e2b import Sandbox ││ ││ # Start sandbox ││ sandbox = Sandbox.create("my-agent-sandbox") ││ ││ # Interact with sandbox. Learn more here: ││ # https://e2b.dev/docs/sandbox/overview ││ ││ # Close sandbox once done ││ sandbox.close() ││ ││─────────────────────────────────────────────────────────────────────── JS SDK ───────────────────────────────────────────────────────────────────────││ ││ import { Sandbox } from 'e2b' ││ ││ // Start sandbox ││ const sandbox = await Sandbox.create('my-agent-sandbox') ││ ││ // Interact with sandbox. Learn more here: ││ // https://e2b.dev/docs/sandbox/overview ││ ││ // Close sandbox once done ││ await sandbox.close() ││ │└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘Execution time: 42.55s
This will create the e2b.toml file storing the sandbox config.
Copy
Ask AI
# This is a config for E2B sandbox templatetemplate_id = "1wdqsf9le9gk21ztb4mo"dockerfile = "e2b.Dockerfile"template_name = "my-agent-sandbox"
Now you can use the E2B SDK to spawn & control your new custom sandbox.The sandbox template name is my-agent-sandbox. We’ll use it as an unique identifier and pass it to the SDK as the template parameter.
This way, we’ll be able to spawn our custom sandbox and control it with the SDK.
Spawn & control your custom sandbox
Copy
Ask AI
import { Sandbox } from 'e2b'// Spawn your custom sandboxconst sandbox = await Sandbox.create({ template: 'my-agent-sandbox' })// Interact with sandbox. Learn more here:// https://e2b.dev/docs/sandbox/overview// Close sandbox once doneawait sandbox.close()