加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在docker容器中启动服务失败,错误:无法获得D-Bus连接:没有与服

发布时间:2020-12-16 03:43:31 所属栏目:安全 来源:网络整理
导读:我安装了docker image并成功构建了一个图像. 当我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

我安装了docker image并成功构建了一个图像.

当我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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读