【问题标题】:Docker AutoStartup service NGINXDocker 自动启动服务 NGINX
【发布时间】:2018-04-24 10:31:35
【问题描述】:

我想在 Docker 容器中自动启动服务 NGINX,

我尝试在 DOCKERFILE FILE 中添加此代码,但服务没有自动启动。

RUN service nginx start
CMD service nginx start

【问题讨论】:

    标签: docker nginx service dockerfile


    【解决方案1】:

    问题是该服务/守护程序将在后台运行。

    阅读以下文章了解更多信息 How to Run a Daemon Service in the Foreground

    快速解决方法是将 CMD 或 ENTRYPOINT 替换为 您必须弄清楚启动命令才能在前台运行。

    (Note: Daemon Service = Binary + Configuration + Initscript.)
    
    To run the process in the foreground, we just need these two ingredients:
    
    Binary: bash -x /etc/init.d/XXX start.
    Here, -x instructs shell to display verbose output. Should be easy to figure out binary path. Another trick is _”ps -ef | grep $PROCESS_PATTERN”_.
    Configuration: Typically speaking, we can dig out from console output of bash -x. Sometimes, it’s just too long to read. Figure out the process ID and the list environment from cat /proc/$pid/environ.
    A lot of decent services refuse to run as root because of security concerns. Here is how to impersonate other users with root.
    

    所以在你的情况下可执行命令应该是

    CMD ["bash", "-x", "/etc/init.d/nginx","start"]
    

    或相应地调整它。
    如果您需要更多帮助,请在 cmets 部分告诉我。

    【讨论】: