Skip to main content

Sandbox Environments ของ Claude Code

การเปรียบเทียบ sandbox approaches, การเลือก approach และรายละเอียดแต่ละ sandbox type

ภาพรวม​

Sandbox environments ให้ Claude ทำงานใน isolated context เพื่อความปลอดภัย หรือเพื่อแยกงาน subagents ออกจากกัน

ประเภทของ Sandbox​

1. Git Worktrees (Isolation: worktree)​

สร้าง isolated copy ของ repository ใน git worktree แยก

วิธีการทำงาน:

# Claude Code สร้าง worktree โดยอัตโนมัติ
git worktree add /tmp/claude-worktree-xyz main

ข้อดี:

  • ไม่กระทบ main working directory
  • ใช้ git history และ tracking เดิม
  • เร็ว ไม่ต้อง clone ใหม่

ข้อเสีย:

  • ยังมี access ถึง filesystem เดิม
  • ไม่ isolate network

เหมาะสำหรับ:

  • การทดลองที่อาจกระทบ working files
  • Subagents ที่ต้องทำงานพร้อมกัน
  • Code review ใน branch แยก

2. Docker Containers​

รัน Claude Code ใน Docker container ที่แยกออกจากระบบ

การตั้งค่า:

# Dockerfile สำหรับ Claude Code sandbox
FROM node:20

RUN npm install -g @anthropic-ai/claude-code

WORKDIR /workspace
COPY . .

CMD ["claude", "-p", "ทำงานใน workspace นี้"]
# รัน sandbox
docker run -it \
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
-v $(pwd):/workspace \
my-claude-sandbox

ข้อดี:

  • Isolation ระดับ OS
  • กำหนด resources (CPU, memory) ได้
  • Network isolation ได้ถ้าต้องการ

ข้อเสีย:

  • ต้องมี Docker ติดตั้ง
  • ช้ากว่า worktrees
  • ต้อง map volumes

เหมาะสำหรับ:

  • Production environments
  • CI/CD pipelines
  • งานที่ต้องการ security เข้มงวด

3. Virtual Machines​

Isolation ระดับสูงสุดผ่าน VMs

ข้อดี:

  • Full OS isolation
  • Network isolation สมบูรณ์
  • Snapshot และ restore ได้

ข้อเสีย:

  • ช้าที่สุด
  • ใช้ resources มาก

เหมาะสำหรับ:

  • งานที่ต้องการ security สูงสุด
  • Testing environments

4. Subagents (Agent Isolation)​

Claude Code spawn subagents ที่ทำงานใน context แยก

การตั้งค่าใน agent definition:

---
name: security-reviewer
isolation: worktree
---

ตรวจสอบโค้ดนี้สำหรับ security issues...

ข้อดี:

  • Built-in support ใน Claude Code
  • ใช้งานง่าย
  • ไม่ต้องตั้งค่า infrastructure

เหมาะสำหรับ:

  • Code review
  • Parallel tasks
  • Specialized workflows

การเลือก Sandbox Approach​

ใช้ decision tree นี้:

ต้องการ isolation ประเภทไหน?
├── แค่ protect working files
│ └── ใช้ Git Worktrees
├── ต้องการ OS isolation
│ └── ใช้ Docker
├── ต้องการ full isolation
│ └── ใช้ VMs
└── ต้องการ parallel agents
└── ใช้ Subagents

การตั้งค่า Default Sandbox​

กำหนด default sandbox สำหรับ subagents ใน settings:

{
"subagent": {
"defaultIsolation": "worktree"
}
}

Worktree Lifecycle Hooks​

จัดการ worktrees ผ่าน hooks:

{
"hooks": {
"WorktreeCreate": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Setting up worktree environment...'"
}
]
}
],
"WorktreeRemove": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Cleaning up worktree...'"
}
]
}
]
}
}

ความปลอดภัย​

  • Worktrees ไม่ isolate filesystem access นอก repo
  • Docker containers isolate filesystem ด้วย volumes
  • ใช้ permissions.deny เพื่อจำกัด actions ใน sandbox
  • Managed settings บังคับ policies แม้ใน sandboxes

ดูสิ่งที่เกี่ยวข้อง​