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

标签 docker centos7

文件

# 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"]

启动文件
#!/bin/bash
systemctl restart autofs

现在,我可以毫无问题地运行以下命令
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
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init

看起来 autofs 从未从“docker run”CMD 启动
$docker run --rm -itd --privileged --name mycentos mycentos
0c977e677897fc9a42bd3a4efe6742fbb14ed888a010cfee94c604436729db2d
$ docker exec -it mycentos /bin/bash
[root@0c977e677897 /]# systemctl status autofs
\u25cf autofs.service - Automounts filesystems on demand
   Loaded: loaded (/usr/lib/systemd/system/autofs.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

最佳答案

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

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

启动文件

 #!/bin/bash
/usr/bin/systemctl restart autofs &
exec /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 容器中运行命令

enter image description here

enter image description here

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

https://github.com/whyistheinternetbroken/docker-centos7-nfs-client-autofs/blob/master/Dockerfile

关于Docker:在 CMD 的 shell 脚本中运行 init 和 systemctl 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49850067/

相关文章:

docker - 无法删除具有依赖子图像的 docker 图像

python3.5 --version 命令未找到 CentOS 7

redis - 无法在 Centos 7 上启动 redis 服务器

mysql - docker,无法启动从官方 docker 镜像 mysql 构建的 docker mysql 容器

docker - 带Traefik和让我们加密的矩阵(突触)家庭服务器

docker - 不带 “docker pull”的散装容器图片提取

php - 如何在 Centos 7 上手动安装 PHP-Zts

nginx - Dockerizing nginx 和 Flask

linux - Docker 运行,设备上没有剩余空间

linux - FIWARE物联网代理: install from RPMs