Docker:在CMD的shell脚本中运行init和systemctl时出错

Docker:在CMD的shell脚本中运行init和systemctl时出错,docker,centos7,Docker,Centos7,Dockerfile # Use Centos7 or RHEL7 base image FROM centos:7 # This steps are needed so that systemd works within container #ENV container docker RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \ systemd-tmpfiles-setup.servi

Dockerfile

# Use Centos7 or RHEL7 base image
FROM centos:7

# This steps are needed so that systemd works within container
#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/*;

COPY startup.sh /usr/local/bin/startup.sh
CMD ["/usr/sbin/init"]
 FROM centos:7

# This steps are needed so that systemd works within container
#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/*;
RUN yum install -y autofs
COPY startup.sh /usr/local/bin/startup.sh
RUN chmod +x /usr/local/bin/startup.sh
RUN systemctl enable autofs
CMD ["/usr/local/bin/startup.sh"]
startup.sh

#!/bin/bash
systemctl restart autofs
#!/bin/bash
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init
 #!/bin/bash
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init
现在,我可以毫无问题地运行以下内容

docker build -t mycentos
docker run -d --privileged --name mycentos mycentos
docker exec -it  mycentos /bin/bash
./usr/local/bin/startup.sh
我希望startup.sh能够自动运行,而无需进入容器并手动运行

所以,我只是将/usr/sbin/init滚动到startup.sh中,并更改了Dockerfile CMD

CMD ["/usr/local/bin/startup.sh"]

startup.sh现在是

#!/bin/bash
/usr/sbin/init
systemctl restart autofs
我发现了错误

Couldn't find an alternative telinit implementation to spawn.
Failed to get D-Bus connection: Operation not permitted
知道如何启动startup.sh吗

PS我记得在/usr/sbin/init未运行时看到了相同的错误消息,我尝试运行systemctl

编辑 我改变了startup.sh

#!/bin/bash
systemctl restart autofs
#!/bin/bash
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init
 #!/bin/bash
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init
看起来autofs从未从“docker run”命令启动过


由于您没有安装autofs,第二件事是您没有在docker文件中启用它。您需要先在Dockerfile中启用它

这是您的Dockerfile,稍作修改,启动脚本与更新后的脚本相同

startup.sh

#!/bin/bash
systemctl restart autofs
#!/bin/bash
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init
 #!/bin/bash
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init
Dockerfile

# Use Centos7 or RHEL7 base image
FROM centos:7

# This steps are needed so that systemd works within container
#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/*;

COPY startup.sh /usr/local/bin/startup.sh
CMD ["/usr/sbin/init"]
 FROM centos:7

# This steps are needed so that systemd works within container
#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/*;
RUN yum install -y autofs
COPY startup.sh /usr/local/bin/startup.sh
RUN chmod +x /usr/local/bin/startup.sh
RUN systemctl enable autofs
CMD ["/usr/local/bin/startup.sh"]
注意:这有时还需要为systemctl装载cgroup

-v /sys/fs/cgroup:/sys/fs/cgroup:ro 
在docker容器中运行该命令

有关更多信息,您可以查看此GitHub


那么,为什么我可以在容器的IDE中运行“systemctl restart autofs”,而不首先调用systemctl enable autofs?systemctl status命令不仅提供有关服务/进程是否正在运行(活动)的信息,还提供是否在机器启动时启用该服务/进程的信息。这本身就需要多个程序(chkconfig等)进行查询。
/usr/bin/systemctl restart autofs&
只是给了我一个错误
无法连接到总线:主机已关闭