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"]
startup.sh
#!/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
EDIT 我已经改变了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)
答案 0 :(得分:3)
因为你没有安装autofs,所以你没有在docker文件中启用它。你首先在Dockerfile中启用它。
这是您的Dockerfile,其位修改和启动脚本与更新后的相同。
startup.sh
#!/bin/bash
/usr/bin/systemctl restart autofs &
exec /usr/sbin/init
Dockerfile
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。
https://github.com/whyistheinternetbroken/docker-centos7-nfs-client-autofs/blob/master/Dockerfile