Sandbox Execution
Run Claude Code in completely isolated environments for safe experimentation, testing, or executing untrusted code.Overview
Claude Code Templates supports three sandbox providers:- E2B - Cloud-based sandboxes with automatic cleanup
- Docker - Local containerized environments
- Cloudflare Workers - Serverless edge computing
E2B Cloud Sandboxes
E2B provides isolated cloud environments perfect for safe code execution without affecting your local machine.Setup
1
Get E2B API Key
Sign up at e2b.dev and get your API key from the dashboard.
2
Set Environment Variable
3
Run Sandbox
Using E2B with Components
Install specific components before execution:- Create an isolated E2B cloud environment
- Install specified agents/commands/MCPs
- Execute your prompt with Claude Code
- Download all generated files to your local machine
- Automatically clean up the sandbox
How It Works
The E2B launcher (e2b-launcher.py) handles the entire lifecycle:
- Automatic timeout management - Extends sandbox lifetime during operations
- Real-time streaming - See Claude Code output as it happens
- File download - All generated files copied to
sandbox-{id}/directory - WebSocket retry logic - Handles connection issues gracefully
- Progress indicators - Visual feedback during execution
Files are downloaded to a project directory named
sandbox-{id} in your current working directory, preserving the full directory structure.Environment Variables
Pass additional environment variables to the sandbox:ANTHROPIC_API_KEY(required)E2B_API_KEY(required)- Custom environment variables you set
Monitoring Sandbox Execution
The E2B launcher provides real-time feedback:Troubleshooting E2B
WebSocket Connection Failed- Network/firewall blocking WebSocket connections
- Corporate proxy restrictions
- Temporary E2B service issues
- Try from a different network
- Check firewall/proxy settings
- Wait a few minutes and retry
Docker Local Sandboxes
Run Claude Code in isolated Docker containers on your local machine.Prerequisites
1
Install Docker
Download from docker.com
2
Start Docker Daemon
Ensure Docker Desktop is running or start the daemon:
3
Set API Key
Running Docker Sandboxes
Docker Architecture
The Docker launcher (docker-launcher.js) orchestrates:
- Image build - Creates
claude-sandboximage with Node.js and dependencies - Volume mounting - Maps
./output/for file persistence - Environment injection - Passes
ANTHROPIC_API_KEYsecurely - Container execution - Runs
execute.jswith your prompt - Cleanup - Removes container with
--rmflag
Output Directory
All files are saved to./output/ in your current directory:
The Docker container runs with
--rm flag, so it’s automatically removed after execution. Only files in the mounted output/ directory persist.Custom Docker Configuration
Modify the Dockerfile incomponents/sandbox/docker/Dockerfile:
Cloudflare Workers Sandbox
Run Claude Code in serverless edge environments.Setup Cloudflare Sandbox
.claude/sandbox/cloudflare/:
Deploying to Cloudflare
1
Install Wrangler
2
Authenticate
3
Deploy Worker
Local Development
http://localhost:8787
Sandbox Comparison
Security Considerations
Sandbox Isolation
All sandbox providers offer:- Process isolation - Code runs in separate processes
- Filesystem isolation - Limited access to host files
- Network isolation - Controlled outbound connections
- Resource limits - CPU, memory, and time constraints
API Key Security
API keys are passed securely:Advanced Usage
Batch Sandbox Execution
Run multiple prompts in sequence:Custom Sandbox Templates
Create your own E2B templates:Monitoring Long-Running Tasks
The E2B monitor provides progress updates:Examples
Safe Code Experimentation
Multi-Component Project
Testing Framework
Next Steps
Session Sharing
Share your Claude Code sessions with team members
Remote Access
Access Claude Code dashboards remotely