Linux Docker systemctl 命令无法使用的解决方案

615 阅读1分钟

据说在 Linux Docker \text{Linux Docker} Linux Docker 中无法使用 systemd(systemctl) \text{systemd(systemctl)} systemd(systemctl) 相关命令的原因是 0 0 0 号进程不是 init \text{init} init,而是其他例如 /bin/bash \text{/bin/bash} /bin/bash,所以导致缺少相关文件无法运行。

具体的解决方案是通过 init \text{init} init 在后台运行一个 docker \text{docker} docker 容器,然后再通过 exec \text{exec} exec 的方式进入到容器中,不过需要注意的是不同的系统 init \text{init} init 程序的路径不同。

例如 CentOS 7 \text{CentOS 7} CentOS 7,

docker run -tid --name test_1 --privileged=true centos:latest /usr/sbin/init
docker exec -it test_1 /bin/bash

再例如 Ubuntu 16.04 \text{Ubuntu 16.04} Ubuntu 16.04,

docker run -tid --name test_2 --privileged=true ubuntu:16.04 /sbin/init
docker exec -it test_2 /bin/bash

需要注意的是,第一个命令除了要通过 init \text{init} init 后台启动 docker \text{docker} docker 容器以外, –privileged=true \text{--privileged=true} –privileged=true 是必须的。