当前位置: 首页>>技术教程>>正文


systemctl无法连接到总线 – docker ubuntu:16.04容器

, , , ,

问题描述

我正在尝试在ubuntu:16.04 docker容器中使用systemctl命令。我正在运行以下命令……

systemctl status ssh

但是我收到了错误……

Failed to connect to bus: No such file or directory

为什么这不起作用?这是否与在Docker容器中运行的Ubuntu有关?如何让systemctl正常工作?

最佳解决方法

我假设你用类似的东西开始你的docker容器

docker run -t -i ubuntu:16.04 /bin/bash

现在的问题是你的init进程PID 1是/bin/bash,而不是systemd。使用ps aux确认。

除此之外,您将缺少dbus,这将是沟通的方式。这是您的错误消息来自的地方。但是由于PID 1没有系统化,因此无法安装dbus。

最好的方式是re-think你计划使用docker的方式。不要依赖systemd作为进程管理器,而是让docker容器在前台运行所需的应用程序。

次佳解决方法

其他人报告了类似的问题。启动终端并输入:

$ env

你看到这样的环境变量吗?

XDG_RUNTIME_DIR=/run/user/`id -u`

其中id -u用反引号括起来而不是单引号。对于普通用户,此变量通常被重新解释为1000,对于超级用户(sudo),该变量被重新解释为0

如果环境变量XDG_RUNTIME_DIR不存在,则需要创建它。完整的讨论在launchpad systemd answers中。

第三种解决方法

试试这个:

docker run -ti -d --privileged=true images_docker  "/sbin/init"

要么

docker run -ti -d --privileged=true images_docker

将是相同的结果。

在这里,我从doc of Docker获得:

By default, Docker containers are “unprivileged” and cannot, for example, run a Docker daemon inside a Docker container. This is because by default a container is not allowed to access any devices, but a “privileged” container is given access to all devices (see the documentation on cgroups devices).

When the operator executes docker run –privileged, Docker will enable access to all devices on the host as well as set some configuration in AppArmor or SELinux to allow the container nearly all the same access to the host as processes running outside containers on the host. Additional information about running with –privileged is available on the Docker Blog.

第四种方法

如果您在Windows的Linux子系统(WSL)中收到此错误,我发现它是因为Docker不受支持。这是由于缺乏cgroup和其他先决条件。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/2962.html,未经允许,请勿转载。