Files
agent/docker-compose.yml
T
kicer 3e389747c8 加固:executor 根文件系统只读,tmpfs 提供可写点
- agent-executor 增加 read_only: true,AI 命令无法往镜像层(/usr、/root 等)写文件做持久化/篡改。

- tmpfs 配 /tmp、/var/tmp、/run。/run 必须有:隔离 wrapper 要在 /run/agent-exec 建私有 anchor/rundir,只读下 mkdir 报 EROFS 会中断每条命令(已实测:Linux + Docker 29 重建后 ReadonlyRootfs=true,session 隔离与技能执行均正常,touch 根目录报 Read-only file system、ls /workspace/tasks 仅见本会话)。

- ARCHITECTURE §6 同步。
2026-09-11 21:11:42 +08:00

93 lines
4.1 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
services:
agent-core:
build: ./app
container_name: agent-core
restart: unless-stopped
expose:
- "8080" # 仅内网暴露,由 nginx-proxy 对外
volumes:
- ./tasks:/workspace/tasks # 持久化:任务目录
- ./skills:/workspace/skills # 技能目录:所有任务/会话共享同一份技能
- ./data:/workspace/data # 持久化:数据目录
- ./config.yml:/app/config.yml # 允许设置页写配置(写前服务端会校验)
- ./app/server:/app/server
environment:
- PORT=8080
- AGENT_CONFIG=/app/config.yml
- VIRTUAL_HOST=agent.ai.foresh.com
- VIRTUAL_PORT=8080
- CERT_NAME=default
# 加入executor独立服务
- EXECUTOR_URL=http://agent-executor:9000
# 两侧设成同一个值即启用调用鉴权(内网互访,可选):
- EXECUTOR_TOKEN=b2676884778e373dcd7244c29aadcb82
networks:
- my-app-network
- agent-internal
agent-executor:
build: ./executor
container_name: agent-executor
restart: unless-stopped
expose:
- "9000"
volumes:
# 核心变更:将宿主机 tasks 挂载到非标准路径 /mnt/host-tasks
# 目的:wrapper 启动时先 unmount 此路径,确保未经 wrapper 的进程无法访问其他 session 数据
- ./tasks:/mnt/host-tasks # 持久化:任务目录
- ./skills:/workspace/skills:ro # 技能目录:所有任务/会话共享同一份技能
environment:
- PORT=9000
# 与 agent-core 侧设成同一个值即启用调用鉴权(可选):
- EXECUTOR_TOKEN=b2676884778e373dcd7244c29aadcb82
# 日志等级:debug|info|warn|error|none(默认 info);
# debug 会把与 core 的完整交互打印出来(请求、/exec 请求体、每条 SSE 帧、JSON 响应):
- EXECUTOR_LOG_LEVEL=info
# 防止 Python 在 skills 目录生成 __pycache__
- PYTHONDONTWRITEBYTECODE=1
# 定义隔离基路径,server.js 需读取此变量以执行 umount 和 bind mount
- EXECUTOR_ISOLATION_BASE=/mnt/host-tasks
# 资源限制:兜住 AI 失控命令(烧 CPU / 吃内存 / fork 炸弹 / 写盘撞满宿主机磁盘)。cgroup/ulimit 随 fork 继承。
# 这些是性能/容量护栏,不限制可调用的 syscall;执行能力由下方 cap_add/security_opt 控制。
cpus: "2.0" # 容器整体最多用 2 核(超出限流不杀进程),按宿主机核数调整
mem_limit: 2g # 内存失控 → 容器被 OOM kill(restart 策略自动拉起,代价是该容器内所有在执行命令随容器重启丢失)
pids_limit: 512 # 进程+线程总数上限,防 fork 炸弹耗尽宿主机进程表
ulimits:
# 单文件最大 2G(字节,RLIMIT_FSIZE):dd/cat /dev/urandom 写巨型文件超限即被内核 SIGXFSZ 杀掉,
# 对 bind 卷上的写入同样生效(内核层限制,与文件系统无关)。
# 只限单文件不限总占用量,“大量文件堆满磁盘”需宿主机侧配额(xfs project quota / 独立分区)兜底。
fsize:
soft: 2147483648
hard: 2147483648
# 安全加固:显式丢弃所有能力,仅保留必要的 SYS_ADMIN
cap_drop:
- ALL
cap_add:
- SYS_ADMIN
security_opt:
# 禁止提权(SUID/SGID 失效),防止通过二进制漏洞获取更高权限
- no-new-privileges:true
# 实测 Docker 29 (Ubuntu) 需 unconfined 才能使用 mount/unshare 系统调用
- apparmor=unconfined
# 根文件系统只读:AI 命令无法往镜像层(/usr、/root 等)写文件做持久化/篡改;
# 合法写盘另走 tmpfs 与 bind 卷(handleExec 已把 HOME/TMPDIR 指向 /tmp)。
read_only: true
tmpfs:
- /tmp
- /var/tmp
# /run 必须可写:隔离 wrapper 要在 /run/agent-exec 下建私有 anchor/rundir,
# 否则 read_only 下 mkdir 报 EROFS,每条命令直接中断。
- /run
networks:
- agent-internal
networks:
my-app-network:
external: true
agent-internal:
driver: bridge