Tag Archives: Failed to get D-Bus connection: Operation not permitted docker

Failed to get D-Bus connection: Operation not permitted [docker Install centos7]

When using docker to install centos:7 Mirror or centos:latest After that, if you execute systemctl, the following problems will occur

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

This is a bug of centos7 in docker, which is also mentioned on the official website. The official CentOS image of docker does not provide SYSTEMd service, and gives a solution. Although it is a little complicated, it can still be handled

1. Create the base image of SYSTEMd 2

Create a new file dockerfile, which is the default file name when building docker image. You can also use other file names, and then use – F to specify the file name when building

vi Dockerfile

Enter the following

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

Execute the create image command (Note: remember to add a space and a dot at the end of the command, otherwise an error will be reported)

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

2. Create an HTTP service image based on the image 2

Re edit dockerfile file

vi Dockerfile

Delete the original content and enter the following

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

Build docker image (Note: remember to add a space and a dot at the end of the command, otherwise an error will be reported)

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

3. Create centos7 container with new image

Remember to add — privileged. The official example does not have this parameter. An error will be reported during execution. After adding it, it will not

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

4. Enter the docker container to execute systemctl 2

docker exec -it mycentos bin/bash

Implementation

systemctl start sshd.service

Although there is no prompt for failed to get D-Bus connection: operation not permitted, it does

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

The service path of systemctl is checked

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

There is sshd.service service, but when you execute systemctl, you can’t find the file, which is very strange

You can’t execute systemctl enable sshd.service

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

There is no solution to this problem for the time being. We will continue to study it later