dpowhyh70416
2019-07-09 06:06
浏览 826
已采纳

在Alpine上运行nginx

I would like to run nginx and php-fpm on container start, however I can't seem to do that. Here is my 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;"]

Container comes up and running, however when I run ps aux nginx is nowhere to be seen until I run nginx command (configuration is okay, nginx -t returns okay, and running it through open container does start the service).

I've tried to chain RUN php-fpm7 && nginx but that does nothing.

Also using entrypoint like ENTRYPOINT ["nginx"] did nothing for me.

How can I make sure those processes are running upon creating the container?

图片转代码服务由CSDN问答提供 功能建议

我想在容器启动时运行nginx和php-fpm,但我似乎无法做到这一点。 这是我的 Dockerfile

  FROM php:7-fpm-alpine 
 
EXPOSE 9080 8000 
EXPOSE 9088 80 
 
 
NIDDIR / var  / www 
COPY。  。
 
RUN apk添加nginx作曲家php7-fpm&&  \
作曲家安装--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& amp ;& amp;  \
 cp nginx.conf /etc/nginx/conf.d/default.conf

CMD [“nginx”,“ -  g”,“daemon off;”] 
   \  n 
 

容器启动并运行,但是当我运行 ps aux 时,无法看到nginx,直到我运行 nginx 命令(配置没问题, nginx -t 返回正常,通过open container运行它会启动服务)。

我试图链接 RUN php-fpm7&& nginx 但是什么也没做。

同样使用像 ENTRYPOINT [“nginx”] 这样的入口点对我没有任何作用。

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglinyi4313 2019-07-09 06:48
    已采纳

    you can add a script and use it in your CMD:

    script :

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

    add the script to your 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"]
    
    已采纳该答案
    打赏 评论
  • doujiao3998 2019-07-09 06:53

    running 2 processes in the same container is not a docker best practice, but i assume it is the correct approach for your specific use-case. luckily docker has a solution for you.:

    use a management tool - supervisord.

    supervisord is designed for orchestrating multiple processes, and i consider it better than a shell script because its offering you the management and logging abilities. create a 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
    

    then install & copy it to your Dockerfile:

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

    now your entrypoint should be:

    ENTRYPOINT /usr/bin/supervisord -c /etc/supervisord.conf
    
    打赏 评论
  • dongzou9047 2019-07-09 07:29

    Both answers are awesome, but As mentioned by @Efrat supervisorsd is more suitable for such cases.

    One thing that I hate about copying thing during Docker build process, Dockerfile should be independent for copying thing I believe. It should be Dockerfile only that will build the Docker image, not other stuff they need to be copied. Just Extending @Efrat answer. Here you go with all configuration inside 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] 
    \
    [unix_http_server] 
    \
    file = /tmp/supervisor.sock 
    \
    chmod = 0777 
    \
    chown= nobody:nogroup 
    \
    [supervisord] 
    \
    logfile = /tmp/supervisord.log 
    \
    logfile_maxbytes = 50MB 
    \
    logfile_backups=10 
    \
    loglevel = info 
    \ 
    pidfile = /tmp/supervisord.pid 
    \
    nodaemon = true 
    \
    umask = 022 
    \
    identifier = supervisor 
    \
    [supervisorctl] 
    \
    serverurl = unix:///tmp/supervisor.sock 
    \
    [rpcinterface:supervisor] 
    \
    supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 
    \
    [include] 
    \
    files = /etc/supervisord.d/*.conf' >> /etc/supervisord.conf 
    
    # nginx supervisord Config
        RUN echo $'[supervisord] 
    \
    nodaemon=true 
    \
    [program:nginx] 
    \
    command= /usr/sbin/nginx -g \'daemon off;\' 
    \
    stdout_logfile=/dev/fd/1 
    \
    stdout_logfile_maxbytes=0MB 
    \
    stderr_logfile_maxbytes = 0 
    \
    stderr_logfile=/dev/fd/2 
    \
    redirect_stderr=true 
    \
    autorestart=false 
    \
    startretries=0 
    \
    exitcodes=0 ' >> /etc/supervisord.d/nginx.conf
    # php-fpm7
    RUN echo $'[supervisord] 
    \
    nodaemon=true 
    \
    [program:php-fpm] 
    \
    command= /usr/sbin/php-fpm7  -F 
    \
    stdout_logfile=/dev/fd/1 
    \
    stdout_logfile_maxbytes=0MB 
    \
    stderr_logfile_maxbytes = 0 
    \
    stderr_logfile=/dev/fd/2 
    \
    redirect_stderr=true 
    \
    autorestart=false 
    \
    startretries=0 
    \
    exitcodes=0 ' >> /etc/supervisord.d/php-fpm.conf
    
    EXPOSE 9080 8000 9088 80
    
    ENTRYPOINT ["supervisord", "--nodaemon", "--configuration", "/etc/supervisord.conf"]
    
    打赏 评论

相关推荐 更多相似问题