sandbox%3Adocker-sandbox-setup by aaronbassett/agent-foundry
npx skills add https://github.com/aaronbassett/agent-foundry --skill sandbox:docker-sandbox-setup配置 Docker 容器以在隔离、安全的环境中运行 Claude Code,防止 AI 代理修改主机系统。
此技能专门提供用于创建沙盒化开发环境的 Docker 配置知识。重点在于实现 Claude Code 隔离所需的最小 Docker 设置,同时保持完整的开发能力,而非提供通用的 Docker 专业知识。
沙盒使用 Docker 在 Claude Code 和主机系统之间创建完全隔离:
默认推荐:Ubuntu 24.04 LTS
FROM ubuntu:24.04
选择 Ubuntu 24.04 的原因:
替代镜像:
debian:bookworm - 最小化,镜像尺寸更小ubuntu:25.04 - 最新功能,支持周期较短广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
为了使用预装了常用工具的自定义基础镜像来加速沙盒创建:
何时使用自定义基础镜像:
创建方法:
# my-sandbox-base.dockerfile
FROM ubuntu:24.04
# 安装常用工具
RUN apt-get update && apt-get install -y \
curl wget git build-essential \
ripgrep fd-find jq python3 python3-pip \
&& rm -rf /var/lib/apt/lists/*
# 安装 Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
# 通过 nvm 安装 Node.js
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
构建并打标签:
docker build -f my-sandbox-base.dockerfile -t my-sandbox-base:latest .
在 Sandbox.toml 中使用:
[sandbox]
base_image = "my-sandbox-base:latest"
挂载工作空间和持久化目录:
docker run \
-v $(pwd)/workspace:/workspace \
-v $(pwd)/.docker-cache/cargo:/root/.cargo \
-v $(pwd)/.docker-cache/npm:/root/.npm \
-v $(pwd)/.docker-cache/pip:/root/.cache/pip \
-v $(pwd)/.docker-cache/claude:/root/.claude \
sandbox-container
关键挂载点:
/workspace - 项目源代码(可从主机编辑)/root/.cargo - Rust 包缓存/root/.npm - Node.js 包缓存/root/.cache/pip - Python 包缓存/root/.claude - Claude Code 配置和缓存优点:
为 Web 开发转发端口:
docker run -p 3000-3999:3000-3999 sandbox-container
端口范围选择理由:
自定义端口: 从 package.json 脚本或用户指定中检测:
# 仅特定端口
docker run -p 3000:3000 -p 8080:8080 sandbox-container
传递必需的环境变量:
docker run \
--env-file .env \
-e GITHUB_TOKEN="${GITHUB_TOKEN}" \
sandbox-container
始终传递:
GITHUB_TOKEN - 用于 gh CLI 操作.env 中指定的自定义 API 密钥切勿传递:
FROM ubuntu:24.04
# 为 apt 设置非交互式前端
ENV DEBIAN_FRONTEND=noninteractive
# 安装基础包
RUN apt-get update && apt-get install -y \
curl wget git build-essential \
ca-certificates gnupg \
&& rm -rf /var/lib/apt/lists/*
# 设置工作目录
WORKDIR /workspace
# 安装语言(Rust、Python、Node.js)
# ... 语言特定设置 ...
# 安装 CLI 工具
# ... 工具安装 ...
# 安装 Claude Code
RUN curl -fsSL https://claude.ai/install.sh | bash
# 配置 shell
# ... shell 设置 ...
CMD ["/bin/bash"]
按变更频率排序 Dockerfile 命令:
良好的排序:
# 很少更改 - 缓存效果好
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y curl git
# 偶尔更改 - 中等缓存命中率
RUN install_rust_stable
# 频繁更改 - 通过卷挂载,不放入镜像
# (不要将源代码 COPY 到镜像中)
为何重要:
沙盒容器通常不需要多阶段构建,因为:
对于沙盒,跳过多阶段构建。
#!/bin/bash
# up.sh
docker build -t sandbox-${PROJECT_NAME} .
docker run -d \
--name sandbox-${PROJECT_NAME} \
-v $(pwd)/workspace:/workspace \
-v $(pwd)/.docker-cache/cargo:/root/.cargo \
-v $(pwd)/.docker-cache/npm:/root/.npm \
-v $(pwd)/.docker-cache/pip:/root/.cache/pip \
-v $(pwd)/.docker-cache/claude:/root/.claude \
-p 3000-3999:3000-3999 \
--env-file .env \
sandbox-${PROJECT_NAME}
echo "沙盒正在启动..."
# 等待并检查容器是否在运行
sleep 2
if docker ps | grep -q "sandbox-${PROJECT_NAME}"; then
echo "✅ 沙盒正在运行!"
echo "使用以下命令访问:./sandbox/shell.sh"
else
echo "❌ 启动沙盒失败。"
echo "检查日志:docker logs sandbox-${PROJECT_NAME}"
exit 1
fi
#!/bin/bash
# shell.sh
docker exec -it sandbox-${PROJECT_NAME} /bin/zsh
#!/bin/bash
# run.sh
docker exec sandbox-${PROJECT_NAME} "$@"
#!/bin/bash
# stop.sh
docker stop sandbox-${PROJECT_NAME}
docker rm sandbox-${PROJECT_NAME}
添加视觉指示器以表明你处于容器中:
# 在 .zshrc 中
if [ -f /.dockerenv ]; then
export IN_CONTAINER=true
# Starship 将显示容器指示器
fi
Starship 配置能识别此变量:
[container]
disabled = false
symbol = "🐳 "
style = "bold red"
使用 .dockerignore 排除不必要的文件:
# .dockerignore
.git
node_modules/
.docker-cache/
*.log
.env.local
优点:
为长时间运行的服务添加健康检查:
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:3000/ || exit 1
谨慎使用 - 仅用于需要它的服务。
容器默认以 root 身份运行。在工作空间中创建的文件将归 root 所有。
解决方案 1:匹配主机 UID/GID
ARG USER_ID=1000
ARG GROUP_ID=1000
RUN groupadd -g ${GROUP_ID} developer && \
useradd -u ${USER_ID} -g developer developer
USER developer
解决方案 2:在停止时修复权限
# 在 stop.sh 中
docker exec sandbox-${PROJECT_NAME} chown -R $(id -u):$(id -g) /workspace
docker stop sandbox-${PROJECT_NAME}
为简单起见,选择解决方案 2。
容器默认是网络隔离的。只有暴露的端口可访问。
不要暴露:
切勿将密钥构建到镜像中:
# ❌ 错误做法
RUN git clone https://user:password@github.com/repo.git
# ✅ 正确做法
RUN git clone https://github.com/repo.git
# (需要在环境中提供 GITHUB_TOKEN)
改用 .env 文件和 --env-file 标志。
用于详细故障排除和优化:
references/troubleshooting.md - 常见的 Docker 问题及解决方案references/optimization.md - 性能和开发体验改进完整的工作示例:
examples/Dockerfile.template - 包含所有功能的完整 Dockerfile 模板examples/docker-compose.yml - 使用 Docker Compose 的替代方案与 language-environment-config:
与 sandbox-config-management:
默认基础镜像: ubuntu:24.04
必需的卷挂载:
/workspace - 源代码/root/.cargo - Rust 缓存/root/.npm - Node 缓存/root/.cache/pip - Python 缓存/root/.claude - Claude Code 配置常用端口范围: -p 3000-3999:3000-3999
环境变量: --env-file .env + -e GITHUB_TOKEN
Dockerfile 顺序: 基础 → 语言 → 工具 → 配置
对于开发容器,跳过多阶段构建
每周安装次数
1
仓库
GitHub 星标数
1
首次出现
1 天前
安装于
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
Configure Docker containers to run Claude Code in isolated, safe environments that prevent AI agents from modifying the host system.
This skill provides Docker configuration knowledge specifically for creating sandboxed development environments. Focus on the minimum Docker setup needed for Claude Code isolation while maintaining full development capabilities, not general Docker expertise.
Sandboxes use Docker to create complete isolation between Claude Code and the host system:
Default recommendation : Ubuntu 24.04 LTS
FROM ubuntu:24.04
Why Ubuntu 24.04:
Alternative images:
debian:bookworm - Minimal, smaller image sizeubuntu:25.04 - Latest features, shorter supportFor faster sandbox creation with common tools pre-installed, build a custom base image:
When to use custom base images:
How to create:
# my-sandbox-base.dockerfile
FROM ubuntu:24.04
# Install common tools
RUN apt-get update && apt-get install -y \
curl wget git build-essential \
ripgrep fd-find jq python3 python3-pip \
&& rm -rf /var/lib/apt/lists/*
# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
# Install Node.js via nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
Build and tag:
docker build -f my-sandbox-base.dockerfile -t my-sandbox-base:latest .
Use in Sandbox.toml:
[sandbox]
base_image = "my-sandbox-base:latest"
Mount the workspace and persistence directories:
docker run \
-v $(pwd)/workspace:/workspace \
-v $(pwd)/.docker-cache/cargo:/root/.cargo \
-v $(pwd)/.docker-cache/npm:/root/.npm \
-v $(pwd)/.docker-cache/pip:/root/.cache/pip \
-v $(pwd)/.docker-cache/claude:/root/.claude \
sandbox-container
Critical mounts:
/workspace - Project source code (editable from host)/root/.cargo - Rust package cache/root/.npm - Node.js package cache/root/.cache/pip - Python package cache/root/.claude - Claude Code config and cacheBenefits:
Forward ports for web development:
docker run -p 3000-3999:3000-3999 sandbox-container
Port range rationale:
Custom ports: Detect from package.json scripts or user specification:
# Specific ports only
docker run -p 3000:3000 -p 8080:8080 sandbox-container
Pass through essential environment variables:
docker run \
--env-file .env \
-e GITHUB_TOKEN="${GITHUB_TOKEN}" \
sandbox-container
Always pass through:
GITHUB_TOKEN - For gh CLI operations.envNever pass through:
FROM ubuntu:24.04
# Set non-interactive frontend for apt
ENV DEBIAN_FRONTEND=noninteractive
# Install base packages
RUN apt-get update && apt-get install -y \
curl wget git build-essential \
ca-certificates gnupg \
&& rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /workspace
# Install languages (Rust, Python, Node.js)
# ... language-specific setup ...
# Install CLI tools
# ... tool installation ...
# Install Claude Code
RUN curl -fsSL https://claude.ai/install.sh | bash
# Configure shell
# ... shell setup ...
CMD ["/bin/bash"]
Order Dockerfile commands by change frequency:
Good ordering:
# Rarely changes - cached well
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y curl git
# Occasionally changes - medium cache hit rate
RUN install_rust_stable
# Frequently changes - volume mounted, not in image
# (Don't COPY source code into image)
Why this matters:
Multi-stage builds are typically NOT needed for sandbox containers because:
Skip multi-stage builds for sandboxes.
#!/bin/bash
# up.sh
docker build -t sandbox-${PROJECT_NAME} .
docker run -d \
--name sandbox-${PROJECT_NAME} \
-v $(pwd)/workspace:/workspace \
-v $(pwd)/.docker-cache/cargo:/root/.cargo \
-v $(pwd)/.docker-cache/npm:/root/.npm \
-v $(pwd)/.docker-cache/pip:/root/.cache/pip \
-v $(pwd)/.docker-cache/claude:/root/.claude \
-p 3000-3999:3000-3999 \
--env-file .env \
sandbox-${PROJECT_NAME}
echo "Sandbox is starting..."
# Wait and check if the container is running
sleep 2
if docker ps | grep -q "sandbox-${PROJECT_NAME}"; then
echo "✅ Sandbox is running!"
echo "Access it with: ./sandbox/shell.sh"
else
echo "❌ Failed to start sandbox."
echo "Check logs: docker logs sandbox-${PROJECT_NAME}"
exit 1
fi
#!/bin/bash
# shell.sh
docker exec -it sandbox-${PROJECT_NAME} /bin/zsh
#!/bin/bash
# run.sh
docker exec sandbox-${PROJECT_NAME} "$@"
#!/bin/bash
# stop.sh
docker stop sandbox-${PROJECT_NAME}
docker rm sandbox-${PROJECT_NAME}
Add visual indicators that you're in a container:
# In .zshrc
if [ -f /.dockerenv ]; then
export IN_CONTAINER=true
# Starship will show container indicator
fi
Starship config recognizes this:
[container]
disabled = false
symbol = "🐳 "
style = "bold red"
Use .dockerignore to exclude unnecessary files:
# .dockerignore
.git
node_modules/
.docker-cache/
*.log
.env.local
Benefits:
Add health checks for long-running services:
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:3000/ || exit 1
Use sparingly - only for services that need it.
Container runs as root by default. Files created in workspace will be root-owned.
Solution 1: Match host UID/GID
ARG USER_ID=1000
ARG GROUP_ID=1000
RUN groupadd -g ${GROUP_ID} developer && \
useradd -u ${USER_ID} -g developer developer
USER developer
Solution 2: Fix permissions on stop
# In stop.sh
docker exec sandbox-${PROJECT_NAME} chown -R $(id -u):$(id -g) /workspace
docker stop sandbox-${PROJECT_NAME}
Choose Solution 2 for simplicity.
Containers are network-isolated by default. Only exposed ports are accessible.
Don't expose:
Never build secrets into the image:
# ❌ BAD
RUN git clone https://user:password@github.com/repo.git
# ✅ GOOD
RUN git clone https://github.com/repo.git
# (Requires GITHUB_TOKEN in environment)
Use .env files and --env-file flag instead.
For detailed troubleshooting and optimization:
references/troubleshooting.md - Common Docker issues and solutionsreferences/optimization.md - Performance and DevX improvementsComplete working examples:
examples/Dockerfile.template - Full Dockerfile template with all featuresexamples/docker-compose.yml - Alternative using Docker ComposeWith language-environment-config:
With sandbox-config-management:
Default base image: ubuntu:24.04
Required volume mounts:
/workspace - Source code/root/.cargo - Rust cache/root/.npm - Node cache/root/.cache/pip - Python cache/root/.claude - Claude Code configCommon port range: -p 3000-3999:3000-3999
Environment: --env-file .env + -e GITHUB_TOKEN
Order Dockerfile: Base → Languages → Tools → Config
Skip multi-stage builds for dev containers
Weekly Installs
1
Repository
GitHub Stars
1
First Seen
1 day ago
Installed on
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
Azure RBAC 权限管理工具:查找最小角色、创建自定义角色与自动化分配
117,000 周安装