Docker: A primer for PHP Developers
In theory, a Docker container should run exactly one process (no more and no less than one process).
In reality, we may want to run php-pfm and nginx on the same container since we need both in order for our website to properly be served.
There is no benefit in having php-fpm and nginx run in two separate containers.
We achieve this by using supervisord, a process management daemon that allows us to monitor and control processes with Linux.
We configure supervisord in three steps:
[supervisord]
nodaemon=true
[program:nginx]
command=nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:php-fpm]
command=php-fpm7.2
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
FROM ubuntu:18.04
LABEL maintainer="Kâmi Barut-Wanayo"
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install -y gnupg tzdata \
&& echo "UTC" > /etc/timezone \
&& dpkg-reconfigure -f noninteractive tzdata
RUN apt-get update \
&& apt-get install -y curl zip unzip git supervisor sqlite3 \
nginx php7.2-fpm php7.2-cli \
php7.2-pgsql php7.2-sqlite3 php7.2-gd \
php7.2-curl php7.2-memcached \
php7.2-imap php7.2-mysql php7.2-mbstring \
php7.2-xml php7.2-zip php7.2-bcmath php7.2-soap \
php7.2-intl php7.2-readline php7.2-xdebug \
php-msgpack php-igbinary \
&& php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
&& mkdir /run/php \
&& apt-get -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& echo "daemon off;" >> /etc/nginx/nginx.conf
ADD default /etc/nginx/sites-available/default
ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["supervisord"]
Step 3: We rebuild our container image using the same command we used in section 6/13 (Nginx configuration)
$ docker build -t kbarut/app:latest -f docker/app/Dockerfile docker/app