Failed to get D-Bus connection: Operation not permitted , docker安装centos7之坑

当使用docker安装centos:7镜像或centos:latest后,若是执行systemctl时,会出现如下问题git

[root@68903c5fbdeb /]# systemctl stop firewalld.service
Failed to get D-Bus connection: Operation not permitted

这是docker中centos7的bug,官网上也提到了这个问题,Docker的官方CentOS镜像中没有提供systemd服务,并给出了 解决办法,虽然复杂了一点,仍是能够处理的。github

一、建立systemd的base imagedocker

新建一个文件 Dockerfile,这是构建docker镜像时默认读取的文件名称,也能够使用其它文件名,而后在构建时使用 -f 指定文件名便可centos

vi Dockerfile

输入如下内容bash

FROM centos:latest
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"]

执行建立镜像命令(注意:记得命令最后要加个空格和点,不然会报错)微信

docker build --rm -t centos:c7-systemd .

二、基于该镜像建立http服务的镜像ssh

从新编辑Dockerfile文件socket

vi Dockerfile

删除原来的内容,输入如下内容大数据

FROM centos:c7-systemd
RUN yum -y install httpd; yum clean all; systemctl enable httpd.service
EXPOSE 80
CMD ["/usr/sbin/init"]

构建docker镜像(注意:记得命令最后要加个空格和点,不然会报错)ui

docker build --rm -t centos:c7-systemd-httpd .

三、使用新的镜像来建立centos7容器

记得要加上 --privileged,官方给的示例没有这个参数,执行时会报错,加上以后就不会了

docker run --privileged -it --name mycentos -d -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 80:80 centos:c7-systemd-httpd

四、进入docker容器执行systemctl

docker exec -it mycentos bin/bash

执行

systemctl start sshd.service

虽然如今没有提示 Failed to get D-Bus connection: Operation not permitted 问题,但却提示

[root@21481fb2cefc /]# systemctl start sshd.service
Failed to get D-Bus connection: No such file or directory

而查了systemctl的服务路径

ls /usr/lib/systemd/system | grep sshd.service

是存在 sshd.service 服务的,但执行systemctl却说是找不到文件,很奇怪

执行 systemctl enable sshd.service 也不行

[root@21481fb2cefc /]# systemctl enable sshd.service
Failed to get D-Bus connection: No such file or directory

这个问题还暂时找不到解决办法,后面继续研究

 

欢迎关注本人的微信公众号“大数据与人工智能Lab”(BigdataAILab),获取更多资讯

相关文章
相关标签/搜索