Docker Alpine 上的容器 Linux 3.7:奇怪的 pid 1 在容器的 pid 命名空间中不可见

Docker container on Alpine Linux 3.7: Strange pid 1 not visible within the container's pid namespace

我目前正在跟踪我们在 Alpine Linux 3.7 主机上使用 dockerd 17.10.0-ce 时遇到的奇怪问题。对于此主机上的所有容器,似乎 Docker 图像的 entrypoint/command 启动的进程树在容器本身内不可见。相比之下,在 Ubuntu 主机上,同一图像的进程树将显示为 PID 1。

这是一个例子。

运行 一个显式已知的容器 entrypoint/command:

% docker run -d --name testcontainer --rm busybox /bin/sh -c 'sleep 1000000'

验证进程是否被 dockerd 正确识别:

% docker top testcontainer
PID                 USER                TIME                COMMAND
6729                root                0:00                /bin/sh -c sleep 1000000
6750                root                0:00                sleep 1000000

现在,在该容器内启动一个 shell 并检查进程列表:

% docker exec -t -i testcontainer /bin/sh
/ # ps -ef
PID   USER     TIME   COMMAND
    6 root       0:00 /bin/sh
   12 root       0:00 ps -ef

可以看出,我们的入口点命令 (/bin/sh -c 'sleep 1000000') 在容器本身内部是不可见的。即使 运行 top 也会产生相同的结果。

这里有我遗漏的东西吗?在具有相同 docker 引擎版本的 Ubuntu 主机上,结果如我所料。这可能与 Alpine 的强化内核有关,导致容器 PID space 的分离方式出现问题吗?

任何有助于调查领域的帮助。

-b

这个问题似乎与 Alpine 内核实现的 grsecurity 模块有关。在这种特定情况下,GRKERNSEC_CHROOT_FINDTASK 内核设置用于限制进程在 chroot 环境之外可以执行的操作。这是由 kernel.grsecurity.chroot_findtask sysctl 变量控制的。

来自 grsecurity 文档:

kernel.grsecurity.chroot_findtask

If you say Y here, processes inside a chroot will not be able to kill, send signals with fcntl, ptrace, capget, getpgid, setpgid, getsid, or view any process outside of the chroot. If the sysctl option is enabled, a sysctl option with name "chroot_findtask" is created.

我现在找到的唯一解决方法是禁用此标志以及 chroot_deny_mknodchroot_deny_chmod 标志,以获得与非 grsecurity 内核相同的行为。

kernel.grsecurity.chroot_deny_mknod=0
kernel.grsecurity.chroot_deny_chmod=0
kernel.grsecurity.chroot_findtask=0

当然这不太理想,因为它会绕过并禁用系统的安全功能,但对于开发环境来说可能是一个有效的解决方法。