Docker容器中使用systemctl报错Failed to get D-Bus connection: Operation not permitted
在Docker容器中执行systemctl
命令时,无论是启动nginx, 或者是vsftpd等服务,都会报如下错误。
1 | systemctl status vsftpd |
解决方法
- 在启动容器时,需要加上
--privileged
参数来添加权限。 - 不能使用默认的bash,而需要执行
/usr/sbin/init
执行命令:
1 | docker run -dit --privileged <image_id> /usr/sbin/init |
以下我们以安装启动vsftpd
服务来例子来讲解。
启动centos的容器
1 | docker run -dit --privileged 3816db78c729 /usr/sbin/init |
进入刚才启动的容器
1 | docker exec -it d9c3cf1ad7a bash |
yum安装vsftpd
服务
1 | yum install -y vsftpd |
启动vsftpd
服务
1 | systemctl start vsftpd |
查看vsftpd
服务的状态
1 | systemctl status vsftpd |