在docker容器中启动服务失败,错误为:获取D-Bus连接失败:与服务管理器没有连接

时间:2023-01-19 09:26:58

I installed docker image and built a image successfully.

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

When I ssh to the container and run the command service xxx start, an error popped:

当我ssh到容器并运行命令服务xxx start时,出现了一个错误:

service nginfra start

服务nginfra开始

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

重定向到/bin/systemctl启动nginfra。服务/sbin/service:第79行:/bin/systemctl:没有这样的文件或目录

Actually, fakesystemd is installed in the container instead of systemd.

实际上,fakesystemd安装在容器中,而不是systemd。

So I removed fakesystemd and installed systemd with the command: yum swap -- remove fakesystemd -- install systemd systemd-libs

所以我删除了fakesystemd,并安装了systemd,命令是:yum swap——删除fakesystemd——安装systemd system -libs

But I still can't start the service:

但我还是不能开始服务:

service nginfra start

服务nginfra开始

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

重定向到/bin/systemctl启动nginfra。服务无法获得D-Bus连接:与服务管理器没有连接。

Does anyone ever meet and solved this issue?

有人见过并解决过这个问题吗?

2 个解决方案

#1


5  

I've managed to fix this issue in a CentOS:7 Docker container. I've followed mainly the Guide on CentOS Docker image project.

我已经在CentOS:7 Docker容器中解决了这个问题。我主要遵循CentOS Docker映像项目指南。

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"]

Now, build the image, and run it using at least the following arguments to docker run command: -v /run -v /sys/fs/cgroup:/sys/fs/cgroup:ro

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

Then main point is that /usr/sbin/init must be the first process inside the Docker container.

然后重点是/usr/sbin/init必须是Docker容器中的第一个进程。

So if you want to use a custom script that executes some commands before running /usr/sbin/init, launch it at the end of your script using exec /usr/sbin/init (in a bash script).

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

Here is an example:

这是一个例子:

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

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

And here is the content of 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

You could have System is booting up. See pam_nologin(8) if your using the PAM system, in that case, delete /usr/lib/tmpfiles.d/systemd-nologin.conf in your Dockerfile because it creates the file /var/run/nologin which generates this specific error.

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

#2


4  

This is known issue with systemd-based OSes inside Docker containers.

这是Docker容器中基于系统的操作系统的问题。

Short answer: as well as replacing fakesystemd with systemd you need to attach /sys/fs/cgroup as a read-only volume into the container, build the image and then run it in "privileged" mode.

简短的回答:除了用systemd替换fakesystemd之外,还需要将/sys/fs/cgroup作为只读卷附加到容器中,构建映像,然后以“特权”模式运行它。

This is the best guide I've found for this. It uses Centos as the example, but should work with any systemd-based OS.

这是我找到的最好的指南。它使用Centos作为示例,但是应该与任何基于系统的操作系统一起工作。

#1


5  

I've managed to fix this issue in a CentOS:7 Docker container. I've followed mainly the Guide on CentOS Docker image project.

我已经在CentOS:7 Docker容器中解决了这个问题。我主要遵循CentOS Docker映像项目指南。

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"]

Now, build the image, and run it using at least the following arguments to docker run command: -v /run -v /sys/fs/cgroup:/sys/fs/cgroup:ro

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

Then main point is that /usr/sbin/init must be the first process inside the Docker container.

然后重点是/usr/sbin/init必须是Docker容器中的第一个进程。

So if you want to use a custom script that executes some commands before running /usr/sbin/init, launch it at the end of your script using exec /usr/sbin/init (in a bash script).

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

Here is an example:

这是一个例子:

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

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

And here is the content of 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

You could have System is booting up. See pam_nologin(8) if your using the PAM system, in that case, delete /usr/lib/tmpfiles.d/systemd-nologin.conf in your Dockerfile because it creates the file /var/run/nologin which generates this specific error.

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

#2


4  

This is known issue with systemd-based OSes inside Docker containers.

这是Docker容器中基于系统的操作系统的问题。

Short answer: as well as replacing fakesystemd with systemd you need to attach /sys/fs/cgroup as a read-only volume into the container, build the image and then run it in "privileged" mode.

简短的回答:除了用systemd替换fakesystemd之外,还需要将/sys/fs/cgroup作为只读卷附加到容器中,构建映像,然后以“特权”模式运行它。

This is the best guide I've found for this. It uses Centos as the example, but should work with any systemd-based OS.

这是我找到的最好的指南。它使用Centos作为示例,但是应该与任何基于系统的操作系统一起工作。