Skip to main content

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:
The sandbox will:
  1. Create an isolated E2B cloud environment
  2. Install specified agents/commands/MCPs
  3. Execute your prompt with Claude Code
  4. Download all generated files to your local machine
  5. Automatically clean up the sandbox

How It Works

The E2B launcher (e2b-launcher.py) handles the entire lifecycle:
Key Features:
  • 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:
The sandbox environment inherits:
  • ANTHROPIC_API_KEY (required)
  • E2B_API_KEY (required)
  • Custom environment variables you set
E2B sandboxes have a 10-15 minute timeout. The launcher automatically extends this to 15 minutes for long-running operations.

Monitoring Sandbox Execution

The E2B launcher provides real-time feedback:

Troubleshooting E2B

WebSocket Connection Failed
This is usually caused by:
  • Network/firewall blocking WebSocket connections
  • Corporate proxy restrictions
  • Temporary E2B service issues
Solutions:
  • Try from a different network
  • Check firewall/proxy settings
  • Wait a few minutes and retry
No Files Generated If the sandbox completes but no files appear:

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

First run:
Subsequent runs:

Docker Architecture

The Docker launcher (docker-launcher.js) orchestrates:
  1. Image build - Creates claude-sandbox image with Node.js and dependencies
  2. Volume mounting - Maps ./output/ for file persistence
  3. Environment injection - Passes ANTHROPIC_API_KEY securely
  4. Container execution - Runs execute.js with your prompt
  5. Cleanup - Removes container with --rm flag

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 in components/sandbox/docker/Dockerfile:

Cloudflare Workers Sandbox

Run Claude Code in serverless edge environments.

Setup Cloudflare Sandbox

This installs the Cloudflare sandbox components to .claude/sandbox/cloudflare/:

Deploying to Cloudflare

1

Install Wrangler

2

Authenticate

3

Deploy Worker

Local Development

Access the worker at http://localhost:8787

Sandbox Comparison

Security Considerations

Never run untrusted code outside a sandbox. Always use sandbox execution when:
  • Testing code from unknown sources
  • Experimenting with system-level operations
  • Running potentially destructive commands
  • Working with sensitive data in isolated environments

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