System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: 主机关闭
时间: 2023-12-15 19:32:18 浏览: 210
这个错误通常是由于Docker容器中的init系统不是systemd导致的。在Docker容器中,init系统通常是第一个进程,其进程ID为1。systemd是一种常见的init系统,但并不是所有的Docker镜像都默认使用systemd作为init系统。因此,当你尝试在容器中运行systemd命令时,就会出现“System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down”这个错误。
解决这个问题的方法是在Dockerfile中使用systemd作为init系统。具体步骤如下:
1.在Dockerfile中添加以下内容:
```dockerfile
# Use systemd as init system
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]
```
2.构建Docker镜像:
```shell
docker build -t myimage .
```
3.运行Docker容器:
```shell
docker run -it --privileged --name mycontainer myimage
```
在容器中运行systemd命令时,你应该不再会看到“System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down”这个错误了。
阅读全文
相关推荐














