In the Docker in Development mini-series, we used a container for Nginx, PHP, Redis and MySQL.
For this application, we're going to combine the Nginx and PHP container into one. I discuss this a bit in the video:
- PHP is weird in that Nginx and PHP processes need access to .php files (diff from other languages)
- I like packing containers as source of truth for each release of the app - we can tag a container with a release/commit sha and build it with that specific code inside, rather than using volume sharing
- So I tend to put Nginx + PHP together for PHP apps in production, which means we:
- Are not sharing code via volume mounts (making production life harder when using multiple servers)
- Are not
COPY
ing orADD
ing code into more than one container image used in a project
Directory Structure
To start using Docker in the shippingdocker.com repository, we'll create a new docker
directory with this structure:
docker
- app
- Dockerfile
- other files
- node
- Dockerfile
- build (shell script)
App Container:
This installs Nginx, PHP and Supervisord. Supervisord is used to run both Nginx and PHP at the same time, and is the process Docker starts when spinning up a container from the image we are building here.
Note that we're symlinking Nginx log files to stdout and stderr. This lets supervisord spit out the Nginx logs in a way that Docker can use via it's logging mechanisms.
We also set Nginx to run in the foreground by setting daemon off;
in /etc/nginx/nginx.conf
.
Note: An update not in the video:
Near the top of the following
Dockerfile
, I had to add the installation of the packagelocales
in order to set the locale correctly. A newer version of the base Ubuntu image had this package uninstalled by default.
FROM ubuntu:16.04
MAINTAINER Chris Fidao
RUN apt-get update \
&& apt-get install -y locales \
&& locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
RUN apt-get update \
&& apt-get install -y nginx curl zip unzip git software-properties-common supervisor sqlite3 \
&& add-apt-repository -y ppa:ondrej/php \
&& apt-get update \
&& apt-get install -y php7.0-fpm php7.0-cli php7.0-mcrypt php7.0-gd php7.0-mysql \
php7.0-pgsql php7.0-imap php-memcached php7.0-mbstring php7.0-xml php7.0-curl \
php7.0-sqlite3 php7.0-xdebug \
&& php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
&& mkdir /run/php \
&& apt-get remove -y --purge software-properties-common \
&& apt-get -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& echo "daemon off;" >> /etc/nginx/nginx.conf
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log
COPY default /etc/nginx/sites-available/default
COPY php-fpm.conf /etc/php/7.0/fpm/php-fpm.conf
EXPOSE 80
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["/usr/bin/supervisord"]
File default
nginx config:
This is a pretty standard PHP configuration for Nginx. Note that it's using the local unix
socket file instead of a TCP network address.
Since PHP-FPM and Nginx will be within the same container, we can use the slightly faster unix socket for communication between Nginx and PHP-FPM.
server {
listen 80 default_server;
root /var/www/html/public;
index index.html index.htm index.php;
server_name _;
charset utf-8;
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
error_page 404 /index.php;
location ~ /\.ht {
deny all;
}
}
File php-fpm.conf
PHP config:
We need to make two changes to the default php-fom.conf
file:
daemonize = no
- Run PHP-FPM in the foreground, so Supervisord can supervise iterror_log = /proc/self/fd/2
- Send php-fpm error log output to stderr
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;
; All relative paths in this configuration file are relative to PHP's install
; prefix (/usr). This prefix can be dynamically changed by using the
; '-p' argument from the command line.
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = /run/php/php7.0-fpm.pid
; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written
; in a local file.
; Note: the default prefix is /var
; Default Value: log/php-fpm.log
error_log = /proc/self/fd/2
; syslog_facility is used to specify what type of program is logging the
; message. This lets syslogd specify that messages from different facilities
; will be handled differently.
; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON)
; Default Value: daemon
;syslog.facility = daemon
; syslog_ident is prepended to every message. If you have multiple FPM
; instances running on the same server, you can change the default value
; which must suit common needs.
; Default Value: php-fpm
;syslog.ident = php-fpm
; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
;log_level = notice
; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
;emergency_restart_threshold = 0
; Interval of time used by emergency_restart_interval to determine when
; a graceful restart will be initiated. This can be useful to work around
; accidental corruptions in an accelerator's shared memory.
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;emergency_restart_interval = 0
; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;process_control_timeout = 0
; The maximum number of processes FPM will fork. This has been design to control
; the global number of processes when using dynamic PM within a lot of pools.
; Use it with caution.
; Note: A value of 0 indicates no limit
; Default Value: 0
; process.max = 128
; Specify the nice(2) priority to apply to the master process (only if set)
; The value can vary from -19 (highest priority) to 20 (lower priority)
; Note: - It will only work if the FPM master process is launched as root
; - The pool process will inherit the master process priority
; unless it specified otherwise
; Default Value: no set
; process.priority = -19
; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
daemonize = no
; Set open file descriptor rlimit for the master process.
; Default Value: system defined value
;rlimit_files = 1024
; Set max core size rlimit for the master process.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
;rlimit_core = 0
; Specify the event mechanism FPM will use. The following is available:
; - select (any POSIX os)
; - poll (any POSIX os)
; - epoll (linux >= 2.5.44)
; - kqueue (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)
; - /dev/poll (Solaris >= 7)
; - port (Solaris >= 10)
; Default Value: not set (auto detection)
;events.mechanism = epoll
; When FPM is build with systemd integration, specify the interval,
; in second, between health report notification to systemd.
; Set to 0 to disable.
; Available Units: s(econds), m(inutes), h(ours)
; Default Unit: seconds
; Default value: 10
;systemd_interval = 10
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;
; Multiple pools of child processes may be started with different listening
; ports and different management options. The name of the pool will be
; used in logs and stats. There is no limitation on the number of pools which
; FPM can handle. Your system will tell you anyway :)
; Include one or more files. If glob(3) exists, it is used to include a bunch of
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
; - the global prefix if it's been set (-p argument)
; - /usr otherwise
include=/etc/php/7.0/fpm/pool.d/*.conf
File supervisord.conf
:
This sets supervisord to run in the foreground (required by Docker to run it).
We then run Nginx and PHP-FPM, making sure to set log files to be coming from stderr/stdout (and ensuring Supervisord doesn't attempt to "rotate" log files for these streams of text).
[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.0
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
Node Container:
The official Node container is great on it's own, but NPM sometimes needs git
to retrieve packages during a run of npm install
.
So, we also build our own NodeJS container.
FROM node:7.2
RUN apt-get install -y git
Build Them:
Here's a script to build the two images. Note we just tag them latest
for now:
#!/usr/bin/env bash
docker build -f app/Dockerfile -t shippingdocker.com/app:latest ./app
docker build -f node/Dockerfile -t shippingdocker.com/node:latest ./node
Test images:
We can test the images by building them and then using them to run some commands:
chmod +x build
./build
cd ../
# Run our app container. It works!
# But, check this in browser @ localhost - we'll see an error! (no vendor dir yet)
docker run --rm -it -p 80:80 \
-v $(pwd):/var/www/html \
shippingdocker.com/app
# Check that our nodejs container works
docker run --rm -it shippingdocker.com/node node -v