NanoClaw - 容器级安全隔离
约 1057 字大约 4 分钟
2026-03-07
容器级安全隔离的 AI 助手,采用 Docker-in-Docker (DinD) 架构实现物理隔离,每次代码执行在独立容器中运行。
核心特性
安全隔离
- Docker-in-Docker (DinD) 架构
- 每次代码执行在独立容器中
- 容器生命周期仅几秒,执行完即销毁
- 完全的进程和文件系统隔离
快速执行
- 容器预热机制
- 镜像缓存优化
- 并行执行支持
- 资源限制可配置
灵活配置
- 支持多种编程语言环境
- 自定义 Docker 镜像
- 资源配额管理
- 网络隔离策略
企业级
- 基于 Anthropic Agent SDK
- 审计日志完整
- 合规性支持
- 多租户隔离
快速开始
前置要求
- Docker 20.10+
- Docker Compose 2.0+
- Python 3.9+
安装
# 克隆仓库
git clone https://github.com/nanoclaw/nanoclaw.git
cd nanoclaw
# 安装依赖
pip install -r requirements.txt
# 配置环境变量
cp .env.example .env
# 编辑 .env 文件,设置 ANTHROPIC_API_KEY启动
# 启动 Docker-in-Docker 守护进程
docker-compose up -d
# 运行 NanoClaw
python main.py配置
编辑 config.yaml:
security:
# 容器资源限制
cpu_limit: 1.0
memory_limit: 512m
timeout: 30s
# 网络隔离
network_mode: none # 或 bridge
# 文件系统
read_only: true
tmpfs_size: 100m
execution:
# 支持的语言环境
languages:
- python:3.11-slim
- node:18-alpine
- golang:1.21-alpine
# 容器预热
preload_images: true
pool_size: 3
llm:
provider: anthropic
api_key: ${ANTHROPIC_API_KEY}
model: claude-3-5-sonnet-20241022技术架构
核心技术栈
- 语言: Python 3.9+
- 容器: Docker Engine API
- SDK: Anthropic Agent SDK
- 异步: asyncio
架构设计
┌─────────────────────────────────────┐
│ NanoClaw Core │
│ ┌──────────────────────────────┐ │
│ │ Anthropic Agent SDK │ │
│ └──────────────────────────────┘ │
│ ↓ │
│ ┌──────────────────────────────┐ │
│ │ Execution Manager │ │
│ └──────────────────────────────┘ │
│ ↓ │
│ ┌──────────────────────────────┐ │
│ │ Docker API Client │ │
│ └──────────────────────────────┘ │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ Docker-in-Docker Daemon │
│ ┌────────┐ ┌────────┐ ┌────────┐│
│ │ 容器 1 │ │ 容器 2 │ │ 容器 3 ││
│ └────────┘ └────────┘ └────────┘│
└─────────────────────────────────────┘安全机制
- 进程隔离: 每个任务在独立容器中执行
- 文件系统隔离: 只读根文件系统 + tmpfs
- 网络隔离: 可配置无网络或受限网络
- 资源限制: CPU、内存、磁盘 IO 限制
- 时间限制: 执行超时自动终止
- 审计日志: 完整的执行记录
使用示例
基本使用
from nanoclaw import NanoClaw
# 初始化
agent = NanoClaw(config_path="config.yaml")
# 执行任务
result = agent.execute(
prompt="编写一个 Python 脚本计算斐波那契数列",
language="python",
timeout=30
)
print(result)自定义容器镜像
# 注册自定义镜像
agent.register_image(
name="custom-python",
dockerfile="""
FROM python:3.11-slim
RUN pip install numpy pandas
""",
languages=["python"]
)
# 使用自定义镜像
result = agent.execute(
prompt="使用 pandas 分析数据",
image="custom-python"
)批量执行
import asyncio
async def batch_execute():
tasks = [
agent.execute_async("任务 1"),
agent.execute_async("任务 2"),
agent.execute_async("任务 3"),
]
results = await asyncio.gather(*tasks)
return results
results = asyncio.run(batch_execute())安全最佳实践
1. 最小权限原则
security:
# 只读文件系统
read_only: true
# 禁用特权模式
privileged: false
# 删除所有 capabilities
cap_drop:
- ALL2. 网络隔离
security:
# 完全禁用网络
network_mode: none
# 或使用受限网络
network_mode: bridge
allowed_hosts:
- api.anthropic.com3. 资源限制
security:
# CPU 限制
cpu_limit: 0.5
cpu_shares: 512
# 内存限制
memory_limit: 256m
memory_swap: 256m
# 磁盘 IO 限制
blkio_weight: 5004. 审计日志
# 启用审计日志
agent = NanoClaw(
config_path="config.yaml",
audit_log=True,
audit_path="/var/log/nanoclaw"
)
# 日志包含:
# - 执行时间
# - 输入输出
# - 资源使用
# - 容器 ID
# - 退出状态性能指标
| 指标 | 数值 |
|---|---|
| 容器启动时间 | < 500ms |
| 容器销毁时间 | < 100ms |
| 内存开销 | 基础镜像 + 50MB |
| 并发执行 | 10+ 容器 |
| 镜像缓存命中率 | > 95% |
适用场景
推荐使用
- 处理不可信代码的场景
- 代码审计和安全测试
- 自动化运维执行未知脚本
- 企业生产环境的安全保障
- 多租户 SaaS 平台
- 在线代码执行平台
不推荐使用
- 对性能要求极高的场景
- 需要持久化状态的场景
- 资源非常受限的环境
相关链接
- GitHub: nanoclaw/nanoclaw
- 文档: 使用文档
- 安全指南: Security Guide
- 问题反馈: Issues
贡献
欢迎贡献代码、报告安全问题或提出建议!
# 克隆仓库
git clone https://github.com/nanoclaw/nanoclaw.git
# 安装开发依赖
cd nanoclaw
pip install -r requirements-dev.txt
# 运行测试
pytest tests/
# 安全测试
python scripts/security_test.py许可证
Apache License 2.0 - 详见 LICENSE