【问题标题】:Docker: running a command after init on centos7 container with systemdDocker:使用systemd在centos7容器上初始化后运行命令
【发布时间】:2018-10-09 22:12:53
【问题描述】:

根据https://hub.docker.com/_/centos/ Centos7 容器必须使用 init 进程运行才能使 systemd 正常工作

Dockerfile

FROM centos:7
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"]

我希望能够在我调用“docker run”时自动运行脚本,所以我将 CMD 行更改为

COPY startup.sh /usr/local/bin/startup.sh
CMD ["/usr/sbin/init", "/usr/local/bin/startup.sh"]

当我 docker exec'd 进入容器时,我看到了进程

root         1     0  0 18:20 ?        00:00:00 /usr/sbin/init /usr/local/bin/startup.sh

尽管上面一行显示了 startup.sh 和 init,但脚本 starup.sh 本身从未执行过,因为该脚本应该启动几个守护进程,而我没有看到它们在“docker run”之后运行。
当我手动运行脚本时(不是作为 CMD 行的一部分,而是在容器外壳中实际执行它),我会看到守护进程正在运行

如何自动调用 startup.sh?我不能简单地只替换 CMD 行 startup.sh,因为这样 systemd 会中断(我需要在 init 运行后调用脚本)

我尝试将 /usr/sbin/init 放在 startup.sh 中

即启动.sh

daemon &
daemon2 &
exec /usr/sbin/init

但守护进程无法运行,因为 init 没有运行

如果你放了

/usr/sbin/init &
daemon &
daemon2 

这不起作用,因为 init 的 PID 不是 1

【问题讨论】:

    标签: docker centos7


    【解决方案1】:

    如果你确实运行服务,你只需要一个 systemd-init 东西。否则,您可以只运行一个 shell 脚本并将 daemon 和 daemon2 放在后台。

    如果您真的要“systemctl start”某些东西,那么您可以避免 cgroup-mapping 和 systemd-init start ... 只需使用 docker-systemctl-replacement 脚本。如果您愿意,它也可以作为 PID 1 运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2022-06-29
      • 2015-12-12
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多