不能在 ubuntu Docker 容器上使用 systemd

问题

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

设置

我正在从 ubuntu:16.04ubuntu:16.10图像运行 Docker 容器。

测试

如果我执行:

16,04容器中的 systemctl status ssh

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

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

如果我做的 which systemctl system ctl 是在 16.04容器中找到的,而不是在 16.10容器中找到的。

我发现了 /lib/systemd的存在。

我已经尝试安装系统:

apt-get install systemd libpam-systemd systemd-ui

然后 which systemctl16.10中找到 system ctl

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

问题

如何在 Ubuntu Docker 映像中激活 systemd 和 systemctl?

为什么 system 在 Ubuntu Docker 的容器中不活跃?

我没有找到任何关于 Ubuntu/Ubuntu Docker 镜像的文档,只有关于从 Upstartsystemd的 Ubuntu 转换的信息。是否有任何文件给出了充分的解释?

69138 次浏览

This is by design. Docker should be running a process in the foreground in your container and it will be spawned as PID 1 within the container's pid namespace. Docker is designed for process isolation, not for OS virtualization, so there are no other OS processes and daemons running inside the container (like systemd, cron, syslog, etc), only your entrypoint or command you run.

If they included systemd commands, you'd find a lot of things not working since your entrypoint replaces init. Systemd also makes use to cgroups which docker restricts inside of containers since the ability to change cgroups could allow a process to escape the container's isolation. Without systemd running as init inside your container, there's no daemon to process your start and stop commands.