无法在 ubuntu docker 容器上使用 systemd

Not able to use systemd on ubuntu docker container

问题

似乎 systemd 在 Ubuntu Docker 容器中未激活或不可用。

设置

我是来自 ubuntu:16.04ubuntu:16.10 图像的 运行 Docker 个容器。

测试

如果我执行:

systemctl status ssh16,04 容器中

结果是错误的Failed to connect to bus: No such file or directory

16.10 容器中,错误是:bash: systemctl: command not found

如果我这样做 which systemctl systemctl 在 16.04 容器中找到,但在 16.10 容器中找不到。

我发现 /lib/systemd 存在。

我尝试安装 systemd:

apt-get install systemd libpam-systemd systemd-ui

然后which systemctl16.10

中找到systemctl

但是systemctl status ssh仍然报错Failed to connect to bus: No such file or directory

问题

如何激活 systemd 和 systemctl 以便在 Ubuntu Docker 图像中使用?

为什么 systemd 在 Ubuntu Docker 容器中不活动? systemd 不是用于实例化容器吗?

我没有找到关于 Ubuntu / Ubuntu Docker 图像的任何文档,只有关于 Ubuntu 从 Upstart 转换的信息至 systemd。有没有文档给出完整的解释?

这是设计使然。 Docker 应该 运行 在您的容器的前台运行一个进程,它将在容器的 pid 命名空间中作为 PID 1 产生。 Docker 是为进程隔离而设计的,而不是为 OS 虚拟化而设计的,因此容器内没有其他 OS 进程和守护进程 运行ning(如 systemd、cron、syslog、等),只有你的入口点或命令你 运行.

如果它们包含 systemd 命令,您会发现很多东西都不起作用,因为您的入口点替换了 init。 Systemd 还利用 docker 限制容器内部的 cgroup,因为更改 cgroup 的能力可能允许进程逃避容器的隔离。如果没有 systemd 运行ning 作为容器内的初始化,就没有守护进程来处理您的启动和停止命令。