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
Get E2B API Key
Sign up at e2b.dev and get your API key from the dashboard.
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
Install Docker
Download from docker.com
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
Local Development
http://localhost:8787
Sandbox Comparison
| Feature | E2B | Docker | Cloudflare |
|---|---|---|---|
| Environment | Cloud | Local | Edge |
| Setup Time | Instant | 2-5 min (first build) | Moderate |
| Isolation | Complete | Complete | Sandboxed |
| File Access | Downloaded | Mounted volume | Limited |
| Internet | ✅ Yes | ✅ Yes | ✅ Yes |
| Cleanup | Automatic | Automatic (container) | Manual |
| Cost | Pay per use | Free (local resources) | Free tier available |
| Best For | Quick experiments | Offline work | Serverless APIs |
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