在Alpine上运行Nginx

时间:2019-07-09 06:06:41

标签: php docker nginx dockerfile

我想在容器启动时运行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 -t返回正常,并通过运行打开容器会启动服务)。

我尝试将RUN php-fpm7 && nginx链接起来,但是什么也没做。

也使用ENTRYPOINT ["nginx"]之类的入口点对我没有任何帮助。

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

3 个答案:

答案 0 :(得分:3)

在同一容器中运行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

答案 1 :(得分:1)

您可以添加脚本并在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"]

答案 2 :(得分:0)

两个答案都很棒,但是正如@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"]