在docker里使用systemctl
一、问题
启动一个docker容器:
- snippet.bash
[yz@test-4 ~]$ sudo docker run --privileged=true --name ff_1 -itd centos:build8 /bin/bash ff02aed2db53……
在docker里无法使用systemctl:
- snippet.bash
[yz@test-4 ~]$ sudo docker attach ff02aed2db53 [yz@ff02aed2db53 ~]$ [yz@ff02aed2db53 ~]$ sudo systemctl start cgconfig.service Failed to get D-Bus connection: Operation not permitted
二、解决方法
docker run命令作两项修改:
- /bin/bash → /usr/sbin/init
- 增加 -u root 参数
- snippet.bash
[yz@test-4 ~]$ sudo docker run --privileged=true -u root --name ff_2 -itd centos:build8 /usr/sbin/init ea6f89d980…… [yz@test-4 ~]$ sudo docker exec -it ff_2 /bin/bash [root@ea6f89d9804d ~]# systemctl start cgconfig.service [root@ea6f89d9804d ~]# systemctl status cgconfig.service ● cgconfig.service - Control Group configuration service Loaded: loaded (/usr/lib/systemd/system/cgconfig.service; disabled; vendor preset: disabled) Active: active (exited) since Thu 2020-10-28 01:13:44 UTC; 2s ago Process: 577 ExecStart=/usr/sbin/cgconfigparser -l /etc/cgconfig.conf -L /etc/cgconfig.d -s 1664 (code=exited, status=0/SUCCESS) Main PID: 577 (code=exited, status=0/SUCCESS) Oct 28 01:13:44 ea6f89d9804d systemd[1]: Starting Control Group configuration service... Oct 28 01:13:44 ea6f89d9804d systemd[1]: Started Control Group configuration service.
如果希望加入docker时使用指定用户而不是root用户,则:
- snippet.bash
[yz@test-4 ~]$ sudo docker exec -u yz -it ff_2 /bin/bash [yz@ea6f89d9804d ~]$
打赏作者以资鼓励: