在 gVisor 内部和嵌套沙箱中使用 gvx 运行 Docker-in-Docker

快速解决方案:在无特权的 gVisor(runsc)沙箱中安全运行 docker:dind
云端 AI 智能体计算环境(例如 Google Agent Compute Engine 沙箱)、Kubernetes agent-sandbox 部署(RuntimeClass: gvisor)以及强化学习评估框架,会将不受信任的工作负载隔离在 gVisor(runsc) 容器中,以保护宿主机内核。当默认无特权的 runsc 容器内的工作负载尝试通过 apt-get 安装 docker.io 或 docker-ce、执行 service docker start 或 dockerd,并运行 docker build 时,即使其在容器内以 root(UID 0)身份运行,该操作仍会失败。
借助 gvx(rungvx),您可以安全地直接在无特权的 gVisor(runsc)容器内运行 docker:dind、docker build 和 docker compose(或将 gvx 嵌套在 gvx 中),无需削弱外层 RuntimeClass、暴露宿主机套接字或授予 --privileged 权限。
低开销、高令牌效率的 AI 智能体解决方案:在由 gVisor 支撑的云端智能体环境中,尝试安装
docker.io并手动绕过/etc/init.d/docker的ulimit错误、iptables-nft故障、iptables-legacy原始套接字拒绝、overlay2挂载错误以及docker build的runc/pivot_root崩溃,通常会在最终完全失败前消耗 20 次以上的工具调用轮次和数万个令牌。gvx(rungvx)是一种低开销、高令牌效率的即插即用解决方案:智能体只需在一个轮次中运行 3 条命令,即可启动一个可用的用户空间 Docker 守护进程,并立即执行docker build和docker compose,而无需修改项目文件。保持并强化外层沙箱隔离:在
runsc内运行rungvx不会绕过外层gVisor沙箱实施的任何限制。相反,gvx会创建第二个嵌套的用户空间 gVisorSentry内核,该内核具有其自身的hostlockdown(Landlock +PR_SET_NO_NEW_PRIVS+seccomp)、其自身的内存内netstack,以及单文件ext4lite卷。内层dockerd和runc容器受到双重沙箱保护:它们的系统调用、挂载操作和原始数据包都不会离开内层gvx用户空间内核。
1# 在现有的 gVisor (runsc) 容器或 Kubernetes Pod 内:
2# 从 GitHub Releases 安装静态链接的 rungvx 二进制文件,或通过 Go 1.27.4+ 安装:
3CGO_ENABLED=0 go install z.gfire.dev/gvx/cmd/rungvx@latest
4
5mkdir -p ~/.gvx
6rungvx run \
7 -p unix:~/.gvx/docker.sock:2375 \
8 -v gvx-docker:/var/lib/docker \
9 -e DOCKER_TLS_CERTDIR= \
10 docker:dind --tls=false &
11
12# 针对内层 gvx 套接字运行标准 Docker 构建和容器
13export DOCKER_HOST="unix://$HOME/.gvx/docker.sock"
14until docker info >/dev/null 2>&1; do sleep 0.5; done
15docker info --format '{{.Driver}} {{.OperatingSystem}}'
16# Output: overlayfs Alpine Linux ... (containerized)
17# 输出:overlayfs Alpine Linux ... (containerized)
18
19docker build -t nested-app:test .
20docker run --rm nested-app:test
在 gVisor(runsc)内直接安装 Docker 失败的五个原因
当用户或 AI 编程智能体在标准 runsc 容器内运行 apt-get install -y docker.io(或 docker-ce)并尝试启动 dockerd 时,会依次遇到五个技术障碍。
1. 初始化脚本(systemctl / service docker start 的 ulimit)和内核模块(modprobe)失败
运行 apt-get install -y docker.io(或 docker-ce)后,尝试通过 systemctl start docker 或 service docker start 启动守护进程会立即失败,因为 PID 1 不是 systemd,且 /etc/init.d/docker 试图将 ulimit -n 提高到沙箱的硬性 RLIMIT_NOFILE 上限之上。当直接调用 dockerd 时,初始化脚本会调用 modprobe,以针对 gVisor 的虚拟内核版本(4.4.0,或较新 gVisor 版本中的 4.19.0-gvisor)加载 overlay、bridge、br_netfilter、ip_tables 和 nf_nat 模块,而该版本并不存在 /lib/modules/<release> 目录:
1System has not been booted with systemd as init system (PID 1). Can't operate.
2Failed to connect to bus: Host is down
3/etc/init.d/docker: 62: ulimit: error setting limit (Operation not permitted)
4level=warning msg="Running modprobe bridge br_netfilter failed with message: modprobe: WARNING: Module bridge not found in directory /lib/modules/4.4.0\nmodprobe: WARNING: Module br_netfilter not found in directory /lib/modules/4.4.0\n, error: exit status 1"
5modprobe: FATAL: Module overlay not found in directory /lib/modules/4.4.0
6modprobe: FATAL: Module br_netfilter not found in directory /lib/modules/4.4.0
7modprobe: FATAL: Module ip_tables not found in directory /lib/modules/4.4.0
8modprobe: FATAL: Module nf_nat not found in directory /lib/modules/4.4.0
2. iptables-nft(nf_tables Netlink)、iptables-legacy 原始套接字拒绝以及 docker0 网桥失败
在 Ubuntu 22.04、Ubuntu 24.04 以及 Debian 11/12 上,apt-get install docker.io 会将 iptables-nft(由 nf_tables netlink 子系统支撑)选为默认的 /usr/sbin/iptables 替代项。此外,即使用户或智能体切换到 iptables-legacy(update-alternatives --set iptables /usr/sbin/iptables-legacy),默认的无特权 gVisor 沙箱(--net-raw=false)也会以 EPERM 拒绝 AF_INET, SOCK_RAW 套接字,导致 iptables-legacy 即使在以 UID 0 运行时也会因 Permission denied (you must be root) 而失败:
1failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to register "bridge" driver: failed to create NAT chain DOCKER: iptables failed: iptables --wait -t nat -N DOCKER: iptables v1.8.10 (nf_tables): Could not fetch rule set generation id: Invalid argument
2iptables v1.8.10 (legacy): can't initialize iptables table `nat': Permission denied (you must be root)
3Perhaps iptables or your kernel needs to be upgraded.
4iptables v1.8.10 (legacy): can't initialize iptables table 'nat': Table does not exist (do you need to insmod?)
5iptables: No chain/target/match by that name.
6Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i docker0 -j RETURN: iptables: No chain/target/match by that name.)
7Error initializing network controller: error obtaining controller instance: failed to create bridge "docker0": operation not permitted
3. gofer / 9p 根文件系统上的 overlay2 失败以及 vfs 磁盘耗尽
除非外层 runsc 管理员明确在 /var/lib/docker 上预先配置了专用的 tmpfs 或 filestore 挂载,否则容器根文件系统由 gVisor 的 gofer(9p/lisafs)或嵌套的 overlay 挂载支撑。在 /var/lib/docker 之上挂载 overlay2 会失败:
1level=error msg="[graphdriver] prior storage driver overlay2 failed: driver not supported"
2failed to mount overlay: invalid argument
3Error starting daemon: error initializing graphdriver: driver not supported: overlay2
当用户或智能体试图通过启动 dockerd --storage-driver=vfs 来绕过此错误时,Docker 会完全禁用写时复制(Copy-on-Write)层共享。Dockerfile 中的每条 RUN 指令都会对整个根文件系统执行完整的递归复制(dirCopy),使一个 500 MB 的镜像在 10 个构建步骤中放大为 5 GB 的写入量,最终导致沙箱因 no space left on device 或 I/O 超时而终止。
4. docker build(BuildKit)和嵌套 runc 失败(pivot_root、/proc、Cgroups 和 DNS)
即使通过 dockerd --iptables=false --ip6tables=false --bridge=none --storage-driver=vfs 强制启动 dockerd,当 BuildKit 或 runc 启动构建容器时,尝试运行 docker build -t app . 或 docker run 仍会立即失败:
1ERROR: failed to solve: process "/bin/sh -c ..." did not complete successfully:
2docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process:
3error during container init: error jailing process inside rootfs: pivot_root .: invalid argument
4error during container init: error mounting "cgroup" to rootfs at "/sys/fs/cgroup": mount cgroup:/sys/fs/cgroup/cpuset (via /proc/self/fd/6), flags: 0xf, data: cpuset: operation not permitted
5cgroups: cgroup mountpoint does not exist: unknown
6runc create failed: systemd not running on this host: cannot use systemd cgroup manager
7error during container init: error mounting "proc" to rootfs at "/proc": mount proc:/proc (via /proc/self/fd/6), flags: 0xe: operation not permitted
8error during container init: error loading seccomp filter into kernel: operation not permitted
9Temporary failure resolving 'deb.debian.org'
10Temporary failure resolving 'archive.ubuntu.com'
5. 嵌套 runsc 因 SECCOMP_FILTER_FLAG_NEW_LISTENER(EBUSY)失败
如果用户尝试在 runsc 内使用 systrap 平台运行另一个 runsc 实例,初始化会立即中止:
1creating subprocess: sysmsg: failed to initialize thread: failed to install seccomp notify rules: device or resource busy
Linux 内核和 gVisor Sentry 都只允许每个任务层级中至多一个 SECCOMP_FILTER_FLAG_NEW_LISTENER(SECCOMP_RET_USER_NOTIF)监听器。由于外层沙箱已占用该监听器槽位,内层 seccomp(2) 调用会返回 EBUSY(device or resource busy)。
gvx 如何解决 gVisor 内的嵌套执行
gvx 在其自身的用户空间内核和平台层中解决了上述全部五个障碍,使 rungvx 能够在外层 gVisor(runsc)容器内运行,甚至可以递归地在另一个 gvx 容器中运行(gvx 中的 gvx)。

1. 从 EBUSY 自动回退到 _RUN_SYSCALL_LOOP Futex 循环的 systrap
在 Linux systrap 中,每个 stub 子进程运行两类线程:
- 客户机执行线程(
sysmsg):通过SECCOMP_RET_TRAP(SIGSYS)以及二进制修补的jmp rel32跳板(syshandler)捕获客户机系统调用,并通过共享内存和SYS_FUTEX与 Sentry 交换寄存器状态。 - stub
syscallThread(syscall_thread.go):代表 Sentry 在 stub 子进程内执行地址空间系统调用(mmap、munmap、clone)。
在上游 gVisor 中,syscallThread 在 stub_amd64.s 和 stub_arm64.s 中支持两种唤醒模式:
- Seccomp 用户通知循环(
_RUN_SECCOMP_LOOP = 6,当t.seccompNotify != nil时):在syscallThread.init()期间,t.installSeccompNotify()安装一个SECCOMP_RET_USER_NOTIF监听器(seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_NEW_LISTENER, stubSyscallRules)),使 Sentry 能够通过ioctl(SECCOMP_IOCTL_NOTIF_SEND)和ioctl(SECCOMP_IOCTL_NOTIF_RECV)分发请求。 - Futex 系统调用循环(
_RUN_SYSCALL_LOOP = 5,当t.seccompNotify == nil时):syscallThread运行syscall_loop(stub_amd64.s/stub_arm64.s),在共享的 2 页syscallSentryMessage(stub 中为PROT_READ)和syscallStubMessage(stub 中为PROT_READ | PROT_WRITE)区域上通过SYS_FUTEX(FUTEX_WAIT)休眠。
上游 gVisor 在 seccompNotifyIsSupported 为真时无条件调用 t.installSeccompNotify(),并且如果 seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_NEW_LISTENER, ...) 返回 EBUSY,则中止 stub 的创建。
在 gvx(pkg/sentry/platform/systrap/seccomp_notify_xplat.go 和 syscall_thread.go)中,syscallThread.init() 调用 t.installSeccompNotifyIfFree():
1// pkg/sentry/platform/systrap/seccomp_notify_xplat.go
2var seccompNotifyBusy atomic.Bool
3
4func (t *syscallThread) installSeccompNotifyIfFree() (*os.File, error) {
5 if seccompNotifyBusy.Load() {
6 return nil, nil
7 }
8 f, err := t.installSeccompNotify()
9 if errors.Is(err, unix.EBUSY) {
10 if seccompNotifyBusy.CompareAndSwap(false, true) {
11 log.Warningf("Another seccomp listener exists in the filter chain of the host process. " +
12 "The stub threads use the slower futex loop instead of seccomp-unotify.")
13 }
14 return nil, nil
15 }
16 return f, err
17}
当 installSeccompNotify() 因外层 gVisor(runsc)或外层 gvx 沙箱已占用进程过滤器链的单一监听器槽位而返回 unix.EBUSY 时:
installSeccompNotifyIfFree()设置seccompNotifyBusy(atomic.Bool),为该进程记录一次警告,并使该 stub 及所有后续 stub 保持t.seccompNotify == nil。- 在
syscallThread.detach()(syscall_thread_amd64.go和syscall_thread_arm64.go)中,当t.seccompNotify == nil时,在amd64上设置regs.Rbx = _RUN_SYSCALL_LOOP(5)(在arm64上为regs.Regs[9] = _RUN_SYSCALL_LOOP),而不是_RUN_SECCOMP_LOOP(6)。 - 在
syscallThread.syscall()(syscall_thread.go)中,内层 Sentry 仅通过共享内存和SYS_FUTEX分发 stub 系统调用:
1// pkg/sentry/platform/systrap/syscall_thread.go
2// 通知 syscall 线程有新的系统调用请求。
3atomic.AddUint32(&sentryMsg.state, 1)
4futexWakeUint32(&sentryMsg.state)
5
6// 等待回复。
7futexWaitWake(&sentryMsg.state, atomic.LoadUint32(&sentryMsg.state))
由于 SECCOMP_RET_TRAP 过滤器在客户机线程栈上可以任意深入,并且 syscallThread 在不请求第二个用户通知监听器的情况下回退到 _RUN_SYSCALL_LOOP,gvx 能够在 runsc 内干净地运行,并支持递归嵌套(rungvx 中的 rungvx)。此外,gvx 的 systrap 平台还能适应外层 runsc 的各种细微差异,包括固定映射顺序、零故障码、ARCH_SET_GS 清除外层补丁,以及在外层 Sentry 受密封的系统调用修补表(0x60000至0x65000)周围取消映射,从而避免 munmap/mseal 上出现 EPERM。
嵌套平台说明(
docs/cli.md):在最小化容器镜像中,始终使用静态链接的rungvx二进制文件(CGO_ENABLED=0)。rungvx中的rungvx可在linux/amd64和linux/arm64上运行(以及 macOShvf主机上运行,速度较慢)。在上游 Dockerrunsc(--platform=kvm和--platform=systrap)内,linux/amd64嵌套运行可以成功(如果您的外层runsc构建拒绝内层 Landlock/seccomp 堆叠,请传递-lockdown=false;外层runsc隔离仍然完全生效)。在linux/arm64上,上游runsc目前在信号帧中省略了fpsimd_context,导致内层 stub 退出码为137。
2. 单文件 ext4lite 卷上的原生 overlayfs
为避免在外层 gVisor 容器的 gofer(9p/lisafs)根文件系统之上挂载 overlayfs,-v gvx-docker:/var/lib/docker 使用 gvx 的内部 ext4lite 文件系统为 /var/lib/docker 提供支撑,该文件系统存储在单个普通文件(gvx-docker.ext4)中。
对外层 runsc 而言,gvx-docker.ext4 只是一个通过 pread64(2) 和 pwrite64(2) 访问的普通文件。在 gvx 内部,ext4lite 提供了一个带有 JBD2 日志的 ext4 文件系统,支持 trusted.overlay.* 和 security.capability 扩展属性、字符设备 whiteout 节点(0:0)、d_type 目录项以及 RENAME_EXCHANGE,使内层 dockerd 能够使用其原生的 overlayfs 存储驱动。
3. 带有 docker0 和 iptables-legacy 的自包含 netstack
在 gvx 内部,dockerd 不与外层 runsc 容器的网络接口或 netfilter 表交互。相反:
dockerd在gvx自身的用户空间netstack内创建其docker0网桥和veth对。- 所有
iptables-legacy规则(-t nat -N DOCKER、MASQUERADE、conntrack、addrtype)均针对gvx的内部 netfilter 实现执行。 - 嵌套的
runc容器在gvx的内存VFS2挂载表中挂载其/proc、/sys和/sys/fs/cgroup层级,从不调用外层runsc的mount(2)系统调用。
在 gVisor(或 gvx 中的 gvx)内验证嵌套 docker:dind
您可以使用以下命令验证两种场景(在 linux/amd64 上 rungvx 位于 runsc 内,或在 linux/amd64 和 linux/arm64 上 rungvx 位于 rungvx 内):
1# 在外层 gVisor (runsc) 容器或外层 rungvx 容器内:
2mkdir -p ~/.gvx
3rungvx run \
4 --backend=systrap \
5 -p unix:~/.gvx/docker.sock:2375 \
6 -v gvx-docker:/var/lib/docker \
7 -e DOCKER_TLS_CERTDIR= \
8 docker:dind --tls=false &
9DIND_PID=$!
10
11export DOCKER_HOST="unix://$HOME/.gvx/docker.sock"
12until docker info >/dev/null 2>&1; do sleep 0.5; done
13
14# 验证内层 Docker 守护进程使用的是原生 overlayfs(而非 vfs)
15docker info --format 'Storage Driver: {{.Driver}}'
16# Output: Storage Driver: overlayfs
17# 输出:Storage Driver: overlayfs
18
19# 运行多层构建并执行嵌套容器
20mkdir -p /tmp/nested-test && cd /tmp/nested-test
21cat <<'EOF' > Dockerfile
22FROM alpine:3.20
23RUN adduser -D -u 1001 appuser && mkdir -p /app && chown appuser:appuser /app
24USER appuser
25WORKDIR /app
26RUN echo "nested-build-ok" > status.txt
27CMD ["cat", "/app/status.txt"]
28EOF
29
30docker build -t nested-test:latest .
31docker run --rm nested-test:latest
32# Output: nested-build-ok
33# 输出:nested-build-ok
34
35kill -INT $DIND_PID
本系列的延伸阅读
- gvx:在一个无特权进程中运行 Linux 容器、Docker 和 Kubernetes:概述
gvx在 Windows(whpx、systrap)、macOS(hvf)和 Linux(systrap)上的运行情况。 - 无需 root 或用户命名空间运行 Docker、docker build 和 docker compose:当
root、sudo、unshare(CLONE_NEWUSER)、mount(2)和/dev/fuse被阻止时,gvx如何运行docker:dind。 - 使用 gvx 在 gVisor 和嵌套沙箱中运行 Docker-in-Docker(本文)
- 面向 AI 智能体强化学习 rollout 的快速隔离 Docker 环境(使用 gvx):如何使用
gvx和单文件ext4lite写时复制快照,以 100 毫秒的重置时间运行数百个并行的SWE-bench和 RL rollout 环境。