PHP development environment with Docker Compose

I’m a professional PHP/Drupal developer and some people have asked me about my development stack. Here’s how you can use Docker to create a PHP development environment on your Mac. Say bye to MAMP!

First you’ll need a Mac laptop. While this is possible on Linux or Windows, I haven’t tried those so your mileage may vary (a lot).

We will be setting up an example development website with PHP and MySQL, using Docker Compose and PHPFarm. This will allow you to test with different PHP versions.

Install Docker and Docker Compose

Install Docker. You can get the free edition called Docker Community Edition (CE).

Install Docker Compose. This will allow you to create multiple containers, containing PHP and MySQL, and connect them into an application.

Create folder structure

I’ve created a folder structure which I find especially useful for development. You can clone it from using

~$ git clone ~/docker-php-dev

Configure Docker Compose

You need to create configuration files for Docker Compose to set up your example website.

Go into the cloned folder docker-php-dev/docker , and copy the file default.docker-compose.yml into docker-compose.yml.

~$ cd ~/docker-php-dev/docker
~/docker-php-dev/docker$ cp default.docker-compose.yml docker-compose.yml

docker-compose.yml contains the default configuration for a simple PHP example site. You can modify it later.

This is all the configuration you need (for now).

Run Docker Compose containers

It’s time to start running Docker!

Make sure you have Docker running. On your Mac, click on Docker in your Applications folder.

Then go to your terminal, and in the docker folder, run:

~/docker-php-dev/docker$ docker-compose up -d
Creating network "docker_default" with the default driver
Creating docker_web_1 ...
Creating docker_mysql_1 ...
Creating docker_web_1
Creating docker_mysql_1 ... done

If you now run docker ps, you’ll see what actually happened:

~/docker-php-dev/docker$ docker ps
CONTAINER ID  IMAGE                     COMMAND                 CREATED             STATUS             PORTS                                                               NAMES
fdd6c8068daf  mysql:5.7                 "docker-entrypoint..."  About a minute ago  Up About a minute>3306/tcp                                              docker_mysql_1
7edd09b729a4  eugenesia/phpfarm:jessie  "/bin/bash /"     About a minute ago  Up About a minute>8051-8056/tcp,>8070-8071/tcp  docker_web_1

Your two containers docker_web_1 (with PHP and Apache) and docker_mysql_1 (with MySQL database server) are now running and connected with each other.

They are accessible through ports 8051-8056, and 8070-8071. These correspond to the different PHP versions provided by PHPFarm.

Go to your web browser and browse to localhost:8056/example.php to see the example page. Voila!

Because we are using PHPFarm, a setup which runs multiple versions of PHP, you can access the page using different ports, corresponding to the different PHP versions:

Shutting down

To shutdown your containers, do this:

~/docker-php-dev/docker$ docker-compose down
Stopping docker_web_1 ... done
Stopping docker_mysql_1 ... done
Removing docker_web_1 ... done
Removing docker_mysql_1 ... done
Removing network docker_default

Customise project name

You may have noticed that your project name has been set to docker, resulting in your containers being named docker_*. Wouldn’t it be nicer if they were named myproj_*, especially if you were running containers for several different projects at the same time?

To fix this, shutdown your containers using the docker-compose down command as above.

Then go to the docker/ folder and copy the default.env file to .env, to set the environment variables for Docker Compose:

~/docker-php-dev/docker$ cp default.env .env

Change your project name to whatever you like in the .env file:

# docker-php-env/docker/.env
# Docker-compose environment variables.
# Rename this file .env and customise to your liking.
# Reference:

# Set the project name otherwise it will default to folder name of
# docker-compose.yml

Bring your containers up again using the docker-compose up -d command as before. This time, they should be named using your project name as a prefix.

What’s next?

Developing a full-fledged PHP site is beyond the scope of this tutorial. But here are a few pointers to get you started:

  • Your docroot and code files are at docker-php-env/data/php/
  • To SSH into the container, do
    $ # docker_web_1 is your container name shown in "docker ps"
    $ docker exec -it docker_web_1 bash
  • To customise the PHPFarm Docker image, see the repository

Leave a Reply

Your email address will not be published. Required fields are marked *