Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running nginx on Alpine

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?

like image 450
Norgul Avatar asked Dec 11 '22 02:12

Norgul


1 Answers

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
like image 140
Efrat Levitan Avatar answered Dec 26 '22 18:12

Efrat Levitan