【问题标题】:Running nginx on Alpine在 Alpine 上运行 nginx
【发布时间】:2019-07-09 15:29:16
【问题描述】:

我想在容器启动时运行 nginx 和 php-fpm,但我似乎不能这样做。这是我的Dockerfile

FROM php:7-fpm-alpine

EXPOSE 9080 8000
EXPOSE 9088 80

WORKDIR /var/www
COPY . .

RUN apk add nginx composer php7-fpm && \
    composer install --no-progress && \
    mkdir -p /etc/nginx /etc/nginx/sites-available /etc/nginx/sites-enabled /run/nginx && \
    ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf && \
    cp nginx.conf /etc/nginx/conf.d/default.conf

CMD ["nginx", "-g", "daemon off;"]

容器启动并运行,但是当我运行 ps aux 时,nginx 无处可见,直到我运行 nginx 命令(配置正常,nginx -t 返回正常,并且通过打开的容器运行它确实启动了服务)。

我试图链接RUN php-fpm7 && nginx,但这无济于事。

还使用像ENTRYPOINT ["nginx"] 这样的入口点对我没有任何帮助。

如何确保这些进程在创建容器时正在运行?

【问题讨论】:

  • 这是 docker 最佳实践的一部分——一个容器就是一个进程。因此,通常您应该为fpm 创建一个容器,为nginx 创建另一个容器。这是doc article about
  • 我建议检查 lslio/nginx-php-fpm 或 richarvey/nginx-php-fpm 的 Dockerfile。它按照您的预期运行。那里的 Dockerfile 可以帮助你找出你的问题。

标签: php docker nginx dockerfile


【解决方案1】:

在同一个容器中运行 2 个进程不是 docker 最佳实践,但我认为它是适合您特定用例的正确方法。幸运的是docker has a solution for you.

使用管理工具 - supervisord

supervisord 专为协调多个进程而设计,我认为它比 shell 脚本更好,因为它为您提供了管理和日志记录功能。创建一个supervisord.conf:

[supervisord]
nodaemon=true
logfile=/tmp/supervisord.log
childlogdir=/tmp
pidfile = /tmp/supervisord.pid

[program:php-fpm]
command=php-fpm7 -F
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

[program:nginx]
command=nginx -g 'daemon off;'
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

然后安装并复制到您的Dockerfile:

RUN apk add supervisor
COPY ./supervisord.conf /etc/

现在你的入口点应该是:

ENTRYPOINT /usr/bin/supervisord -c /etc/supervisord.conf

【讨论】:

  • 这应该是我读到的许多其他问题的答案,这些问题误导了失败的 entrypoint.sh
【解决方案2】:

您可以添加一个脚本并在您的CMD 中使用它:

脚本:

#!/bin/bash
service nginx start
php-fpm7

将脚本添加到您的Dockerfile:

COPY /PATH/TO/script.sh /path/in/container/script.sh
RUN chmod +x /path/in/container/script.sh
CMD ["/path/in/container/script.sh"]

【讨论】:

  • 我实际上使用了入口点ENTRYPOINT ["sh", "/start.sh"],但基本上就是这样。谢谢
  • @Norgul 你能分享你的“项目”吗?
  • @projetmbc 我不能,它是专有的 :)
  • @Norgul 我该死... :-)
  • @projetmbc 我认为您可能唯一担心的是我放弃了这种方法。我使用 nginxinc/nginx-unprivileged:1.20-alpine 图像而不是库存的 Alpine 图像。
【解决方案3】:

两个答案都很棒,但正如@Efrat supervisorsd 所述,更适合这种情况。

我讨厌在 Docker 构建过程中复制东西的一件事,Dockerfile 应该独立于复制我相信的东西。应该只有 Dockerfile 才能构建 Docker 映像,而不是需要复制它们的其他内容。只是扩展@Efrat 答案。在这里,您可以使用 Dockerfile 中的所有配置。

FROM php:7-fpm-alpine
WORKDIR /var/www
RUN apk add nginx composer php7-fpm supervisor && \
    mkdir -p /etc/nginx /etc/nginx/sites-available /etc/nginx/sites-enabled /run/nginx && \
    ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf && \
    # cp nginx.conf /etc/nginx/conf.d/default.conf && \
    mkdir -p /etc/supervisord.d/

#supervisord basic config file    
    RUN echo  $'[supervisord] \n\
[unix_http_server] \n\
file = /tmp/supervisor.sock \n\
chmod = 0777 \n\
chown= nobody:nogroup \n\
[supervisord] \n\
logfile = /tmp/supervisord.log \n\
logfile_maxbytes = 50MB \n\
logfile_backups=10 \n\
loglevel = info \n\ 
pidfile = /tmp/supervisord.pid \n\
nodaemon = true \n\
umask = 022 \n\
identifier = supervisor \n\
[supervisorctl] \n\
serverurl = unix:///tmp/supervisor.sock \n\
[rpcinterface:supervisor] \n\
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface \n\
[include] \n\
files = /etc/supervisord.d/*.conf' >> /etc/supervisord.conf 

# nginx supervisord Config
    RUN echo $'[supervisord] \n\
nodaemon=true \n\
[program:nginx] \n\
command= /usr/sbin/nginx -g \'daemon off;\' \n\
stdout_logfile=/dev/fd/1 \n\
stdout_logfile_maxbytes=0MB \n\
stderr_logfile_maxbytes = 0 \n\
stderr_logfile=/dev/fd/2 \n\
redirect_stderr=true \n\
autorestart=false \n\
startretries=0 \n\
exitcodes=0 ' >> /etc/supervisord.d/nginx.conf
# php-fpm7
RUN echo $'[supervisord] \n\
nodaemon=true \n\
[program:php-fpm] \n\
command= /usr/sbin/php-fpm7  -F \n\
stdout_logfile=/dev/fd/1 \n\
stdout_logfile_maxbytes=0MB \n\
stderr_logfile_maxbytes = 0 \n\
stderr_logfile=/dev/fd/2 \n\
redirect_stderr=true \n\
autorestart=false \n\
startretries=0 \n\
exitcodes=0 ' >> /etc/supervisord.d/php-fpm.conf

EXPOSE 9080 8000 9088 80

ENTRYPOINT ["supervisord", "--nodaemon", "--configuration", "/etc/supervisord.conf"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多