Error in Centos7 Docker container Failed to get D-Bus connection: Operation not permitted
About 1 min
The reason for the error:
Execute the command to start nginx in the running docker container
[root@node132 ~]# docker run -it nginx-1 /bin/bash
[root@03e74fb601c1 /]# systemctl start nginx
Failed to get D-Bus connection: Operation not permitted
If you want to use systemctl to manage services, you need to add the parameter --privileged to increase the rights, and you can’t use the default bash, replace it with init, the command is as follows
[root@node132 ~]# docker run -d -it --privileged nginx-1 /usr/sbin/init
362306ddc6f03919fcf22854da065d46c5231e8c140e636656c1bbbe497f9b62
[root@node132 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
362306ddc6f0 nginx-1 "/usr/sbin/init" 2 seconds ago Up 1 second
Run it in background mode and then enter the container to operate
[root@node132 ~]# docker exec -it 3623 /bin/bash
[root@362306ddc6f0 /]# systemctl start nginx
[root@362306ddc6f0 /]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2019-02-27 11:43:04 UTC; 6s ago
Process: 4221 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 4220 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 4219 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 4222 (nginx)
CGroup: /docker/362306ddc6f03919fcf22854da065d46c5231e8c140e636656c1bbbe497f9b62/system.slice/nginx.service
├─4222 nginx: master process /usr/sbin/nginx
├─4223 nginx: worker process
├─4224 nginx: worker process
├─4225 nginx: worker process
└─4226 nginx: worker process
‣ 4222 nginx: master process /usr/sbin/nginx
Feb 27 11:43:04 362306ddc6f0 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 27 11:43:04 362306ddc6f0 nginx[4220]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 27 11:43:04 362306ddc6f0 nginx[4220]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 27 11:43:04 362306ddc6f0 systemd[1]: Started The nginx HTTP and reverse proxy server.
So far the problem is solved