【问题标题】:How to startup up services on a Docker image?如何在 Docker 映像上启动服务?
【发布时间】:2015-11-20 20:01:03
【问题描述】:

我已经为在 FPM + Nginx 上运行 PHP7 的图像创建了一个 Dockerfile,但我无法让图像正确启动服务。

这到底应该怎么做?

这是我目前正在做的部分:

CMD service php7-fpm start; \
    service php7-fpm status; \
    service nginx start; \
    service nginx status

【问题讨论】:

    标签: php nginx docker dockerfile php-7


    【解决方案1】:

    您需要一个进程管理器,例如 supervisor、s6、daemontools 或任何其他

    例如查看主管的文档

    https://docs.docker.com/articles/using_supervisord/

    【讨论】:

    • 使用其他包(例如主管)代替内置 System V / upstart 作业(这是 Nginx/PHP 的常见用例)有什么优势?另外,CMD 一开始就没有工作有什么原因吗?
    • 容器和镜像已经没有了。另一个答案解决了这个问题并解释了它。
    【解决方案2】:

    当管理多个服务时,您需要使用 supervisord。

    在您的 Dockerfile 中。

    Additional commands...
    RUN apt-get update && apt-get install -y supervisor
    ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
    Additional commands...
    CMD ["/bin/supervisord", "-c /etc/supervisor/conf.d/supervisord.conf"]
    

    supervisord.conf

    [supervisord]
    nodaemon=true
    
    [program:php7]
    command=<command to start php7>
    autostart=true
    autorestart=true
    <repeat for additional services>...
    

    这将确保容器在一切启动后不会退出。请记住,Docker应该是一个单进程环境,该进程在前台运行,而不是作为服务在后台运行。

    【讨论】:

    • 嘘。似乎主管确实是答案。但是为什么CMD service nginx start 不工作?
    • 如上所述,它不起作用,因为 Docker 期望应用程序在前台运行。当您将应用程序作为服务启动时,它会在后台启动并退出容器。因此,如果您希望容器在您去docker run -td mycontainer 时保持运行,您需要确保服务在前台运行而不是作为服务运行。此外,如果您只想运行 nginx 而没有其他内容,您可以去:CMD ["nginx", "-g", "daemon off;"],再次注意到该进程不是守护进程而是在前台运行。
    • 知道了!我认为“docker应该是一个单一的进程环境”你的意思是它是一个约定,而不是一个技术约束。
    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2017-11-18
    • 2018-03-25
    • 2018-08-23
    • 2018-06-22
    相关资源
    最近更新 更多