【问题标题】:How can I start services like cron and supervisor when build and up a docker container?在构建和构建 docker 容器时,如何启动 cron 和 supervisor 等服务?
【发布时间】:2019-01-09 09:12:44
【问题描述】:

我在构建容器后尝试启动 cron 和 supervisor 等服务,但服务没有启动,我需要手动执行容器内的命令。

我的意图是一旦容器启动,这些服务就已经在运行了。

我创建了一个适用于 Caddy Web Server 启动的 shell 脚本,但不适用于其他服务。

我的 init-services.sh

#!/bin/sh

/usr/bin/caddy --conf /etc/Caddyfile --log stdout
service supervisor start
service cron start

我的 Dockerfile 片段

RUN apt-get update && apt-get install --no-install-recommends -y \
    wget \
    nano \
    git \
    unzip \
    iputils-ping \
    gnupg \
    supervisor \
    cron

COPY .docker/scripts/init-services.sh /usr/bin/init-services

RUN chmod +x /usr/bin/init-services

CMD ["/usr/bin/init-services"]

【问题讨论】:

  • 最佳实践是重新设计您的系统,让每个容器拥有一项服务。我还会尽量避免安装一些对您正在运行的服务无用的工具(nanogitping)并将其限制为一项服务你正在尝试运行。

标签: docker ubuntu docker-compose supervisord


【解决方案1】:

您可以通过在运行命令 (CMD) 中以前台模式运行您的主管来实现此目的。

首先创建带有所需服务的 supervisord.conf。您还需要在nodaemon 模式下启动所有服务,这是前台模式。例如,如果您要启动 apache 服务器,则需要在 supervisord.conf 中设置 -D 标志,如下所示;
command=/bin/bash -c "/usr/local/bin/gosu root /sbin/httpd -D FOREGROUND"


您需要在 Dockerfile 中复制此 supervisord.conf 文件,如下所示;
COPY supervisord.conf /etc/supervisord.d/supervisord.conf


之后,您可以在 init-services.sh 文件中以前台模式启动 supervisord 服务,如下所示;
/usr/bin/supervisord -c /etc/supervisord.d/supervisord.conf -n

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多