Đang tải…
Đang tải…
npx claude-code-templates@latest --sandbox docker/READMEExecute Claude Code in isolated Docker containers with AI-powered code generation using the Claude Agent SDK.
Ensure Docker is installed and running on your system:
# Check Docker installation
docker --version
# Verify Docker daemon is running
docker ps
If Docker is not installed, visit: https://docs.docker.com/get-docker/
Set your Anthropic API key:
# Set as environment variable
export ANTHROPIC_API_KEY=sk-ant-your-api-key-here
# Or pass directly when using the CLI
npx claude-code-templates@latest --sandbox docker \
--agent development/frontend-developer \
--prompt "Create a React component" \
--anthropic-api-key sk-ant-your-key
# Basic execution
npx claude-code-templates@latest --sandbox docker \
--prompt "Write a function to calculate factorial"
# With specific agent
npx claude-code-templates@latest --sandbox docker \
--agent development/python-developer \
--prompt "Create a data validation script"
# With multiple components
npx claude-code-templates@latest --sandbox docker \
--agent development/fullstack-developer \
--command development/setup-testing \
--prompt "Set up a complete testing environment"
This sandbox combines two powerful technologies:
User Prompt → Docker Launcher → Container Build → Execute Script → Claude Agent SDK → Output Files
docker/
├── docker-launcher.js # Node.js launcher that orchestrates Docker
├── Dockerfile # Container definition with Claude Agent SDK
├── execute.js # Script that runs inside container
├── package.json # Dependencies (Claude Agent SDK)
└── README.md # This file
output/ directorynpx claude-code-templates@latest --sandbox docker \
--prompt "Create a REST API server with Express.js"
npx claude-code-templates@latest --sandbox docker \
--agent security/security-auditor \
--prompt "Audit this codebase for security vulnerabilities"
npx claude-code-templates@latest --sandbox docker \
--agent development/frontend-developer \
--command testing/setup-testing \
--setting performance/performance-optimization \
--prompt "Create a React app with testing setup"
# 1. Generate initial code
npx claude-code-templates@latest --sandbox docker \
--agent development/fullstack-developer \
--prompt "Create a blog API with authentication"
# 2. Check output
ls -la output/
# 3. Iterate on generated code
npx claude-code-templates@latest --sandbox docker \
--prompt "Add pagination to the blog API"
Required:
ANTHROPIC_API_KEY - Your Anthropic API keyOptional:
DOCKER_BUILDKIT=1 - Enable BuildKit for faster buildsThe Docker image (claude-sandbox) includes:
@anthropic-ai/claude-agent-sdk installed globally/app/output (mounted as volume)Edit Dockerfile to customize:
# Add additional system dependencies
RUN apk --no-cache add postgresql-client redis
# Install additional global npm packages
RUN npm install -g typescript tsx
# Set custom environment variables
ENV CUSTOM_VAR=value
cd .claude/sandbox/docker
docker build -t claude-sandbox .
docker run --rm \
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
-v $(pwd)/output:/output \
claude-sandbox \
node /app/execute.js "Your prompt here" ""
# Remove built image
docker rmi claude-sandbox
# Remove all stopped containers
docker container prune
# Remove dangling images
docker image prune
Error: Docker is not installed
Solution:
# Install Docker from official site
# macOS: https://docs.docker.com/desktop/install/mac-install/
# Linux: https://docs.docker.com/engine/install/
# Windows: https://docs.docker.com/desktop/install/windows-install/
Error: Docker daemon is not running
Solution:
# macOS/Windows: Start Docker Desktop application
# Linux: sudo systemctl start docker
Error: ANTHROPIC_API_KEY environment variable is required
Solution:
export ANTHROPIC_API_KEY=sk-ant-your-key-here
Error: Failed to build Docker image
Solution:
# Check Docker logs
docker logs <container-id>
# Rebuild from scratch
docker build --no-cache -t claude-sandbox .
# Check disk space
docker system df
Error: Permission denied when accessing output files
Solution:
# Check output directory permissions
ls -la output/
# Fix permissions (if needed)
sudo chown -R $USER:$USER output/
Error: Container failed with code 1
Solution:
# Run container interactively for debugging
docker run -it --rm \
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
claude-sandbox \
/bin/bash
# Check container logs
docker logs <container-id>
DOCKER_BUILDKIT=1)sandboxuser (UID 10001)Docker:
Anthropic API:
Example costs for 100 executions:
| Feature | Docker | E2B | Cloudflare |
|---|---|---|---|
| Execution Location | 🏠 Local | ☁️ Cloud | 🌍 Edge |
| Setup Complexity | Medium | Easy | Easy |
| Internet Required | Setup only | Yes | Yes |
| Cost | Free | Paid | Paid |
| Privacy | Full control | Third-party | Third-party |
| Offline Support | Yes | No | No |
| Best For | Local dev, privacy, offline | Full stack projects | Serverless, global APIs |
docker/
├── docker-launcher.js # Orchestrates container lifecycle
│ ├── checkDockerInstalled()
│ ├── checkDockerRunning()
│ ├── buildDockerImage()
│ └── runDockerContainer()
├── Dockerfile # Container definition
│ ├── Base image (Node 22 Alpine)
│ ├── System dependencies
│ ├── Claude Agent SDK
│ └── Security (non-root user)
├── execute.js # Execution script (runs in container)
│ ├── installComponents()
│ ├── executeQuery()
│ └── copyGeneratedFiles()
├── package.json # NPM dependencies
└── README.md # Documentation
# Build image
npm run build
# Clean image
npm run clean
Add custom tools to the Dockerfile:
# Install Python packages
RUN pip install --no-cache-dir pandas numpy matplotlib
# Install Node.js packages globally
RUN npm install -g typescript eslint prettier
# Add custom scripts
COPY scripts/ /app/scripts/
Create a custom Dockerfile for specialized environments:
FROM node:22-alpine
# Install database clients
RUN apk add --no-cache postgresql-client mysql-client
# Install development tools
RUN apk add --no-cache vim nano tmux
# Install Claude Agent SDK
RUN npm install -g @anthropic-ai/claude-agent-sdk
# ... rest of configuration
Optimize image size with multi-stage builds:
# Build stage
FROM node:22-alpine AS builder
WORKDIR /build
COPY package*.json ./
RUN npm ci --only=production
# Runtime stage
FROM node:22-alpine
COPY --from=builder /build/node_modules ./node_modules
# ... rest of configuration
Mount additional volumes for persistent data:
docker run --rm \
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
-v $(pwd)/output:/output \
-v $(pwd)/cache:/cache \
claude-sandbox
MIT License - See LICENSE file for details
For issues and questions:
docker --version && docker psecho $ANTHROPIC_API_KEYdocker logs <container-id>ls -la output/Built with ❤️ using Docker, Node.js, and Claude Agent SDK