docker - 在 docker 容器中启动服务失败,错误 : Failed to get D-Bus connection: No connection to service manager

标签 docker dbus systemd

我安装了docker镜像并成功构建了一个镜像。

当我 ssh 到容器并运行命令 service xxx start 时,弹出错误:

service nginfra start

Redirecting to /bin/systemctl start nginfra.service /sbin/service: line 79: /bin/systemctl: No such file or directory

其实容器中安装的是fakesystemd,而不是systemd

所以我删除了 fakesystemd 并使用以下命令安装了 systemd: yum swap -- 移除 fakesystemd -- 安装 systemd systemd-libs

但是我还是无法启动服务:

service nginfra start

Redirecting to /bin/systemctl start nginfra.service Failed to get D-Bus connection: No connection to service manager.

有没有人遇到并解决过这个问题?

最佳答案

我已经设法在 CentOS:7 Docker 容器中解决了这个问题。 我主要关注 the Guide on CentOS Docker image project .

FROM centos:7

ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;

# Install anything. The service you want to start must be a SystemD service.

CMD ["/usr/sbin/init"]

现在,构建镜像,并至少使用 docker run 命令的以下参数运行它:-v/run -v/sys/fs/cgroup:/sys/fs/cgroup:ro

那么重点是/usr/sbin/init必须是Docker容器内的第一个进程。

因此,如果您想使用在运行 /usr/sbin/init 之前执行某些命令的自定义脚本,请在脚本末尾使用 exec/usr/sbin/启动它init(在 bash 脚本中)。

这是一个例子:

ADD cmd.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/cmd.sh

CMD ["/usr/local/bin/cmd.sh"]

这里是cmd.sh的内容:

#!/bin/bash

# Do some stuffs

exec /usr/sbin/init # To correctly start D-Bus thanks to https://forums.docker.com/t/any-simple-and-safe-way-to-start-services-on-centos7-systemd/5695/8

您可能有 系统正在启动。如果您使用 PAM 系统,请参阅 pam_nologin(8),在这种情况下,删除 Dockerfile 中的 /usr/lib/tmpfiles.d/systemd-nologin.conf code>,因为它会创建文件 /var/run/nologin 来生成此特定错误。

关于docker - 在 docker 容器中启动服务失败,错误 : Failed to get D-Bus connection: No connection to service manager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28495341/

相关文章:

ubuntu - 如何将环境变量传递给 systemd 启动的服务

docker - 127当我运行此docker脚本时发生127

python - manage.py 不会在 Raspberry Pi 上的 Docker 中登录到 stdout/stderr

linux - DBus 配置通配符

django - 在 systemd 上使用 daphne 运行 django channel

linux - centos 7 : nginx Failed to read PID from file/run/nginx. pid:参数无效

docker - 如何使用Docker容器化InfluxDB?

django - 如何与Django一起使用Dokku?

python - 从 python 调用时,udisks FilesystemUnmount 似乎不存在

Android 到 Linux 蓝牙找不到所有 UUID