docker容器內使用systemctl報錯


問題:

在docker容器中使用systemctl命令報錯

[root@707a5892dae2 /]# systemctl start nginx
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

原因:

默認情況下,在第一步執行的是/bin/bash,而docker中的bug,無法使用systemctl
所以使用/usr/sbin/init同時添加--privileged=true參數就能夠使用systemctl了,但覆蓋了默認的/bin/bash,因此我們想進入容器就不能使用docker attach了;而只能使用docker exec -it <dockername> /bin/bash

解決辦法:

運行容器時添加--privileged=true,修改/bin/bash/usr/sbin/init;

[root@919 ~]# docker run -itd --name nginxv2 --privileged=true centos /usr/sbin/init 
# --privileged: 指定容器是否是特權容器。

驗證:

[root@919 ~]# docker exec -it nginxv2 /bin/bash
[root@491ab139e4fd /]# systemctl start nginx
[root@491ab139e4fd /]# ps -ef |grep nginx
root         187       1  0 02:32 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx        188     187  0 02:32 ?        00:00:00 nginx: worker process


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM