Dockerize Laravel 5 with php-fpm, nginx and mysql services

Let’s build basic stack of services for average php project. I bet once you get the point you will love this and moreover the deployment process will be ridiculously faster!

Pre-conditions
macOS Sierra
Docker CE Version 17.09.0-ce-mac35
docker-compose version 1.16.1, build 6d1ac21
docker-compose config version — 3

First of all I would say it’s a pity that there is no official docker-compose config for Laravel and related services. And there is only Vagrant-based (homestead) reference in official documentation.

Let’s start with the whole picture…

Before making a docker-compose or Dockerfile you have to follow docker philosophy one service per container.

Service: db

I used an official image of mysql from Docker Hub. And on the next step you have to come up with data storage. Actually there are 3 options : 
- Volumes
- Bind mounts
- tmpfs mounts

https://docs.docker.com/engine/admin/volumes/

As far as mysql container generates persistent state data the preferred choice is using Volumes. And also I decided to add slow-log config. Pretty useful setting for monitoring slow queries.

Service: php

For this service I used own Dockerfile forked from official php-fpm image just because I need a bunch of additional stuff like xDebug, composer, Laravel intaller etc. Nothing special at this point 🙂 . But take a note of : composer global require “hirak/prestissimo”

This plugin for parallel install makes installation tremendously faster!

Before
[188.8MB/206.54s] Memory usage: 188.76MB (peak: 218.72MB), time: 206.54s
After
[188.9MB/48.62s] Memory usage: 188.92MB (peak: 213.92MB), time: 48.62s

Service: nginx

At this point I was concerned about substitution on environment variables. I saw some people use Perl scripts for this but my choice is envsubst .

So that’s all for now. Happy hacking=)