centos7 docker容器报 Failed to get D-Bus connection 错误

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Trend_H/article/details/80770899

在官方的centos7的docker容器里面不能用service启动服务

[root@5e9c0d13330f /]# service nginx start
bash: service: command not found

使用 systemctl 又开始报错

[root@ab2ceae04ce8 /]# systemctl start nginx
Failed to get D-Bus connection: Operation not permitted

报这个错的原因是dbus-daemon没能启动。systemctl并不是不能使用。

方法一

官方对此有解释 github centos官方镜像repo README.md的解释,
上面说了具体的解决方案,

Dockerfile for systemd base image

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/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

This Dockerfile deletes a number of unit files which might cause issues. From here, you are ready to build your base image.

$ docker build --rm -t local/c7-systemd .

然后使用这个做好的镜像为基础,再开发你的镜像。

方法二

将CMD或者entrypoint设置为/usr/sbin/init即可。docker容器会自动将dbus等服务启动起来。如下:

docker run --privileged -it centos:7 /usr/sbin/init

然后再通过exec进到容器内执行你的操作

docker exec -it 容器id /bin/bash

青冥有晓月

猜你喜欢

转载自blog.csdn.net/Trend_H/article/details/80770899