SoFunction
Updated on 2025-03-10

Detailed explanation of the solution to the use of systemctl startup service in docker

docker version:

[root@localhost gae_proxy]# docker version
Client:
 Version:     1.10.3
 API version:   1.22
 Package version: docker-common-1.10.3-46..10.x86_64
 Go version:   go1.6.3
 Git commit:   d381c64-unsupported
 Built:      Thu Aug 4 13:21:17 2016
 OS/Arch:     linux/amd64

Server:
 Version:     1.10.3
 API version:   1.22
 Package version: docker-common-1.10.3-46..10.x86_64
 Go version:   go1.6.3
 Git commit:   d381c64-unsupported
 Built:      Thu Aug 4 13:21:17 2016
 OS/Arch:     linux/amd64

Mirror version

[root@b2ca5610d3da /]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core) 

When starting the service through systemctl in docker, the error message is always Failed to get D-Bus connection: Operation not allowed.

[root@25d27693a24d ~]# systemctl start sshd
Failed to get D-Bus connection: Operation not permitted

The solution is to run /usr/sbin/init when docker run. for example:

[root@localhost /]# docker run -tid --name hadoopbase centos/hadoopbase:v001 /usr/sbin/init

In this way, the running container can use systemctl to start the service. Some people say that the problem of errors reported through systemctl is solved in CentOS7.2, but I still encountered such a problem in actual operation.

Another solution is to execute the command /usr/sbin/init through CMD when generating the image file through Dockerfile, namely: CMD [ "/usr/sbin/init"];

refer to:/docker/docker/issues/7459
          /docker/docker/issues/2296

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.