TechSummary 2025-09-17
· 閱讀時間約 12 分鐘
如何使用 Cerebras 和 Docker Compose 建構安全的 AI 編程代理 🔐
Source: https://www.docker.com/blog/cerebras-docker-compose-secure-ai-coding-agents/
- 本文深入探討如何利用 Cerebras AI 推理 API、Docker Compose、ADK-Python 和 MCP 伺服器,建構可攜、安全且完全容器化的 AI 編程代理環境。
- 入門設定:首先透過
git clone
取得範例程式碼,並設定CEREBRAS_API_KEY
於.env
檔案中,然後執行docker compose up
啟動系統,代理介面可在localhost:8000
存取。git clone https://github.com/dockersamples/docker-cerebras-demo && cd docker-cerebras-demo
cp .env-sample .env
# 編輯 .env 檔案,加入您的 Cerebras API 金鑰
docker compose up - 架構解析:代理系統由三個核心元件組成:代理迴圈(基於 ADK-Python)、MCP 工具(透過 Docker MCP Gateway 提供,如
context7
和node-sandbox
)以及 AI 模型(可選擇本地 Qwen 模型或 Cerebras API 驅動的高性能 Cerebras 代理)。 - 建構自訂沙箱作為 MCP 伺服器:文中展示如何建構一個安全的程式碼執行沙箱。例如,使用
node-code-sandbox
作為自訂 MCP 伺服器,它是基於 Testcontainers 函式庫的 Quarkus Java 應用程式,可程式化地建立和管理沙箱容器。 - 沙箱安全性:在沙箱容器中禁用網路 (
.withNetworkMode("none")
) 是關鍵安全措施,防止代理程式碼外洩資料。例如:可在沙箱內執行命令或寫入檔案:GenericContainer sandboxContainer = new GenericContainer<>("mcr.microsoft.com/devcontainers/javascript-node:20")
.withNetworkMode("none") // disable network!!
.withWorkingDirectory("/workspace")
.withCommand("sleep", "infinity");
sandboxContainer.start();// 在沙箱內執行命令
sandbox.execInContainer(command);
// 將檔案寫入沙箱
sandbox.copyFileToContainer(Transferable.of(contents.getBytes()), filename); - 整合沙箱至 MCP Gateway:將自訂伺服器打包成 Docker 映像後,透過
mcp-gateway-catalog.yaml
檔案整合至 MCP Gateway,並在docker-compose.yml
中啟用。此設定確保沙箱容器在代理請求時被啟動,並在 Compose 停止時由 Testcontainers 自動清理。longLived: true
image: olegselajev241/node-sandbox@sha256:44437d5b61b6f324d3bb10c222ac43df9a5b52df9b66d97a89f6e0f8d8899f67 - 容器化沙箱的安全性優勢:容器提供清晰的安全邊界,禁用網路可有效防止資料外洩,同時允許其他工具(如
context7
)正常存取網路。