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 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 github.com/eugenesia/docker-php-dev using
~$ git clone [email protected]:eugenesia/docker-php-dev.git ~/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 0.0.0.0:3307->3306/tcp docker_mysql_1 7edd09b729a4 eugenesia/phpfarm:jessie "/bin/bash /run.sh" About a minute ago Up About a minute 0.0.0.0:8051-8056->8051-8056/tcp, 0.0.0.0:8070-8071->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:
- PHP 5.1: localhost:8051/example.php
- PHP 5.2: localhost:8052/example.php
- PHP 5.3: localhost:8053/example.php
- PHP 5.4: localhost:8054/example.php
- PHP 5.5: localhost:8055/example.php
- PHP 5.6: localhost:8056/example.php
- PHP 7.0: localhost:8070/example.php
- PHP 7.1: localhost:8071/example.php
- PHP 7.2 Beta: localhost:8072/example.php
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: https://docs.docker.com/compose/reference/envvars/ # Set the project name otherwise it will default to folder name of # docker-compose.yml COMPOSE_PROJECT_NAME=myproj
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.
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 github.com/eugenesia/docker-phpfarm