man riding a bycicle transporting some boxes

PHP development environment with Docker

In this post I will share a development environment for PHP projects using Docker. The goal is that you see Docker as an alternative to Vagrant as development environment.

We will use a repository with a LAMP project in Docker -> php-docker

Introduction

Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same way, regardless of its environment.

Docker uses a client-server architecture. The Docker client communicates with Docker Engine (server), which makes the job of creating, running and distributing the containers. The client can connect to the server on the same system, or on a remote system. When you install Docker on a Linux distribution, you are installing a client and a server on the same machine, so when using the client, it connects to the local server by default.

Docker only works on Linux systems, because it uses the operating system containers technology. But it can be used on Mac and Windows systems using a virtual machine. Docker provides the boot2docker image file, containing the configuration to run Docker Engine, for most common virtualization systems.

In this tutorial we will use Docker Machine and Docker Compose, two useful projects that are part of the ecosystem of Docker. We explain them briefly:

Docker Machine is a tool that helps us create, configure and manage (virtual or physical) machines with Docker Engine. You can create and configure Docker Engine in different environments and suppliers, for example Virtualbox, AWS, VMWare, Digitalocean, OpenStack, … In this tutorial we will see how to use this tool on a Mac system with VirtualBox.

Docker Composer is a tool to define and run multi-container applications. All configuration is defined in the docker-composer.yml file, which is a yaml formatted file in which the different containers and their properties are specified.

Installation

If you use a Linux system, DockerClient and Server are installed on the same machine and you can use it directly. But if use Mac or Windows you need to install the Docker client and besides the server on a virtual machine. To simplify this, we can use Docker Machine and it will be much easier.

Let’s assume we have a Mac system and we will be setting up our Docker environment:

Install Docker Engine, Docker Machine and Docker Composer

The simplest way is to follow this guide (https://docs.docker.com/docker-for-mac/)

Once installed we can check out that everything is correct:

Install Virtualbox

Simply follow the installation process (https://www.virtualbox.org)

Docker settings

With Docker Machine we create a virtual machine with a Docker server ready to use.

We have just created a virtual machine in VirtualBox with boot2docker called “default”.

How do connect the client Docker?

With the DockerMachine env option, we can get the environment variables needed by the client to connect to our default server machine.

To get our client connected to the Docker Engine in the default virtual machine, simply run the eval command displayed as the last line of the previous output:

You must repeat last command each time you open a new shell or restart the computer.

If we test the client we can check how it connects to the server now:

Folder Synchronization

When Docker Machine creates a VirtualBox machine, it makes the host machine /Users folder shared to be accessible from the virtual machine.

To have better synchronization performance over the shared folder, we recommend using NFS Machine Tool Docker (https://github.com/adlogix/docker-machine-nfs). This tool allows you to configure shared folders with the NFS system wherever you need.

Clone the PHP-Docker example project

In this example I used five containers, one for each of the tools to be used:

  • Apache 2.4
  • PHP FPM 5.6
  • PHP CLI 5.6, Composer and Symfony installer
  • Mysql 5.6
  • PhpMyadmin

Directory schema project

  • apps: The folder where applications are located, each one in a subdirectory.
  • apache: Image definition with Apache 2.4
  • apache/conf: Apache settings
  • apache/logs: Apache log
  • apache/conf/vhosts: Apache virtual sites definition files.
  • php56/fpm: Image definition with PHP-FPM 5.6
  • php56/fpm/conf: PHP-FPM 5.6 settings
  • php56/cli: Image definition with PHP-CLI 5.6, Composer and Symfony installer
  • php56/cli/conf: PHP-CLI 5.6 settings

Start the development environment

Build images

Start containers

Stop containers

Stop virtual machine

With this few steps we would have the environment up and ready to develop 🙂

To access the web server and PhpMyAdmin, use ports 80 and 8080 respectively. The IP address is the virtual machine’s one and we can find it out with the command:

Tips and Tricks

To conclude this post I will drop a couple of tips that I find very useful.

How to run the PHP command console

This is a typical question when working with Docker. With the following example we will run the php -v command in a container based on the image php56-cli. The container mounts a volume in the current folder $ (pwd) in the path “/usr/local/apache2/htdocs”

To simplify its usage, the best way is to define aliases in ~/.bashrc:

We only miss reloading Bash for the changes to take effect in the terminal

Now we can run php, composer and Symfony commands:

Commands to delete images and containers

Delete all containers

Delete all images

That is all, now you can test all we have learnt in this article. You can find the code in the php-docker repository so you can use it.

I think Docker brings many advantages and you should give it a chance. I hope this post helps, bye 🙂