Dockerサンドボックス構築ガイド / Docker Sandboxing Guide
- 日本語
- English
- 简体中文
🐳 AIを「安全な檻」の中で走らせる
AIエージェントにプログラムの生成と実行を任せるとき、最も安全な構成は Docker(コンテナ)内で隔離して動かすことです。コンテナ内で何が起きても、あなたのホストPC(Macなど)本体に影響はありません。
なぜDockerが最強の安全策なのか?
graph LR;
subgraph Host[あなたのPC 🖥]
OS[macOS / Linux]
Files[大事なファイル ❤️]
end
subgraph Container[Docker コンテナ 📦]
Agent[AIエージェント 🤖]
Sandbox[隔離されたファイル]
Agent -->|自由に読み書き| Sandbox
end
Container -.->|壁:アクセス不可| Files
Host --> Container
ドッカーの考え方: AIがどんなに暴走しても(rm -rf / を叩こうが、おかしなプログラムを走らせようが)、「コンテナの壁」に阻まれ、あなたのホストは無傷。コンテナを消して作り直せば元通りです。
Step 1: Dockerのインストール
Dockerがまだない場合は Docker Desktop をインストールしてください。
Step 2: Dockerfileの作成
ワークスペースのルートに以下の内容で Dockerfile を作成します。
FROM node:20-slim
# 作業ディレクトリの設定
WORKDIR /app
# OpenClawのインストール
RUN npm install -g openclaw
# ワークスペースのコピー
COPY . /app/workspace
# ポートの公開(必要に応じて)
EXPOSE 3000
# 起動コマンド
CMD ["openclaw", "agent", "--agent", "main"]
Step 3: コンテナのビルドと起動
# イメージのビルド
docker build -t my-openclaw-agent .
# コンテナの起動(APIキーは環境変数で安全に渡す)
docker run -d \
--name openclaw-agent \
-e CEREBRAS_API_KEY="csk-your-key-here" \
-e DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/..." \
--restart unless-stopped \
my-openclaw-agent
Step 4: ログの確認
# リアルタイムログの表示
docker logs -f openclaw-agent
# コンテナの状態確認
docker ps
運用のコツ
| テクニック | コマンド | 説明 |
|---|---|---|
| コンテナの停止 | docker stop openclaw-agent | 一時停止 |
| コンテナの再起動 | docker restart openclaw-agent | プロセス更新 |
| コンテナの削除 | docker rm -f openclaw-agent | 完全に破棄 |
| ファイルの取り出し | docker cp openclaw-agent:/app/workspace/report.md . | 成果物の回収 |
- APIキーはファイルに書き込まず、
-eオプションで渡すのが鉄則です。 --restart unless-stoppedをつけておくと、PC再起動時も自動復帰します。- 重要な成果物は
docker cpコマンドで定期的にホストに取り出しておきましょう。
🐳 Running AI Inside a "Safe Cage"
When letting an AI agent write and execute code, the safest architecture is to isolate it inside a Docker container. Whatever happens inside the container, your host PC remains unaffected.
Why Docker is the Ultimate Safety Net
graph LR;
subgraph Host[Your PC 🖥]
OS[macOS / Linux]
Files[Precious Files ❤️]
end
subgraph Container[Docker Container 📦]
Agent[AI Agent 🤖]
Sandbox[Isolated Files]
Agent -->|Free read/write| Sandbox
end
Container -.->|Wall: No Access| Files
Host --> Container
The Docker mindset: No matter how reckless the AI gets (rm -rf /, running weird scripts), the "container wall" blocks it from touching your host. Just destroy and recreate the container to reset.
Step 1: Install Docker
If not installed, get Docker Desktop.
Step 2: Create a Dockerfile
Create a Dockerfile at the workspace root:
FROM node:20-slim
WORKDIR /app
RUN npm install -g openclaw
COPY . /app/workspace
EXPOSE 3000
CMD ["openclaw", "agent", "--agent", "main"]
Step 3: Build & Run
# Build the image
docker build -t my-openclaw-agent .
# Start the container (pass API keys securely via env vars)
docker run -d \
--name openclaw-agent \
-e CEREBRAS_API_KEY="csk-your-key-here" \
-e DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/..." \
--restart unless-stopped \
my-openclaw-agent
Step 4: Check Logs
# Real-time logs
docker logs -f openclaw-agent
# Container status
docker ps
Operational Tips
| Technique | Command | Purpose |
|---|---|---|
| Stop container | docker stop openclaw-agent | Pause |
| Restart container | docker restart openclaw-agent | Process refresh |
| Remove container | docker rm -f openclaw-agent | Full teardown |
| Extract files | docker cp openclaw-agent:/app/workspace/report.md . | Retrieve deliverables |
- Always pass API keys via
-eflags, never hardcode them in files. --restart unless-stoppedensures auto-recovery after PC reboots.- Periodically extract critical deliverables from the container to your host with
docker cp.
🐳 让 AI 在"安全的笼子"里运行
当您让 AI 代理编写并执行代码时,最安全的架构是将其隔离在 Docker 容器内运行。无论容器内发生什么,您的宿主 PC 都不会受到影响。
为什么 Docker 是终极安全方案?
graph LR;
subgraph Host[您的电脑 🖥]
OS[macOS / Linux]
Files[珍贵的文件 ❤️]
end
subgraph Container[Docker 容器 📦]
Agent[AI 代理 🤖]
Sandbox[被隔离的文件]
Agent -->|自由读写| Sandbox
end
Container -.->|隔离墙:禁止访问| Files
Host --> Container
Docker 的理念:无论 AI 有多失控(执行 rm -rf /、运行可疑脚本),"容器之墙"都会将其拦截,确保宿主系统毫发无伤。销毁容器后重新创建即可恢复如初。
第一步:安装 Docker
如果尚未安装,请从 Docker Desktop 下载安装。
第二步:创建 Dockerfile
在工作空间根目录中创建以下内容的 Dockerfile:
FROM node:20-slim
WORKDIR /app
RUN npm install -g openclaw
COPY . /app/workspace
EXPOSE 3000
CMD ["openclaw", "agent", "--agent", "main"]
第三步:构建并运行
# 构建镜像
docker build -t my-openclaw-agent .
# 启动容器(通过环境变量安全传递 API 密钥)
docker run -d \
--name openclaw-agent \
-e CEREBRAS_API_KEY="csk-your-key-here" \
-e DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/..." \
--restart unless-stopped \
my-openclaw-agent
第四步:查看日志
# 实时日志
docker logs -f openclaw-agent
# 容器状态查看
docker ps
运维技巧
| 操作 | 命令 | 用途 |
|---|---|---|
| 停止容器 | docker stop openclaw-agent | 暂停运行 |
| 重启容器 | docker restart openclaw-agent | 刷新进程 |
| 删除容器 | docker rm -f openclaw-agent | 完全销毁 |
| 提取文件 | docker cp openclaw-agent:/app/workspace/report.md . | 回收交付物 |
- 务必通过
-e参数传递 API 密钥,绝不要直接写在文件中。 - 添加
--restart unless-stopped可确保 PC 重启后容器自动恢复运行。 - 定期使用
docker cp命令将关键交付物从容器中提取到宿主机。