How I develop in PHP with CoreOS and Docker
Notice!!
Since Docker and all tools provided are moving fast, you shouldn’t be reading this post. We’ve written a new post about PHP development and Docker at Yappa. You can read it here: http://tech.yappa.be/docker-php-development
I’ve been using the Vagrant provisioned-with-Ansible-setup for a while now. But for the last month(s) I’ve been playing around with things like: Docker, boot2docker, CoreOS, etcd, .. I managed to setup a fast and easy way to develop my PHP applications. Symfony2 is my preferred weapon of choice, so I’ll explain how I’m developing a Symfony2 app.
The software stack
Most of these applications are installed with an automated Mac installation/configuration manager I’ve created a while ago. MacPlan, built on top of Ansible.
You can also download and install these applications manually or use Homebrew and Homebrew Cask to install them.
Vagrant and CoreOS
I’m working on Mac, so I need a virtual machine to run Docker server. I used to run boot2docker as a Docker host, but I recently switched to CoreOs because it feels faster. I didn’t benchmarked it yet.
Configure coreos-vagrant
Edit config.rb
, uncomment and edit the following lines.
Once these things are done, you can start the CoreOS Vagrant box.
Vagrant will ask for sudo permission to change the /etc/hosts file on your Mac.
You can test the docker daemon (already) by running:
Configure Docker client on Mac
The Docker client on your Mac don’t know where to look for Docker server (yet).
The easiest way is to add to following line at the bottom of ~/.bash_profile
.
Or update your dotfiles in MacPlan or personal dotfiles management repository.
Open a new terminal or export the variable in your current terminal.
At this moment, Docker client will have access to Docker server on your virtual Machine with CoreOS.
No containers running yet.
Docker compose
I’m always starting with (almost) the same docker-compose.yml
file.
This files describes the docker services I want to use.
In short the following services are used:
- A data container
- An Nginx webserver on port 80
- PHP-FPM listening in port 9000
- A MySQL server
- Mailcatcher
Data Container
I’m using this to get some volumes shared accros different containers (PHP, Nginx, ..) without file permission issues. Also the vendor folder will be symlinked to a volume on the docker containers, this speeds up the io.
Nginx
A standard webserver setup with a minimal amount of setup. I’m using a pre-built image, yappabe/docker-nginx.
PHP-FPM
A PHP-FPM instance waiting on a TCP socket, port 9000 to process the PHP files. Also a pre-built image, yappabe/docker-php. It’s possible to use an older versions of PHP, by using a tag on the docker image.
The following tags are possible:
- 5.6
- 5.4
- 5.3
- default will be the latest built: 5.6
MySQL
A normal Mysql server, listening on port 3306. You can define the default credentials and pre-create a database. You can also leave these settings empty for random credentials and no database on creation.
Run the container
Starting form my default setup you can download the docker-compose.yml
file and run the containers.
Open a new terminal window.
Docker will start pulling the images, but this will only be done once. After that, you shoud see an output log of the started containers.
Open a new terminal window to check of all containers are running.
Install vendors
Now we need to install our vendors.
Fill in the correct credentials for MySQL when asked. Fill in “mysql” as database host as localhost will not work. This is because of the Docker Compose magic, it links all the containers together and updates their hosts file accordingly.
Clearing the Symfony2 Cache is also easy.
Now you can visit your Symfony2 application at http://172.17.8.101
, and start developing.
Since app_dev.php
has an internal check on localhost, you can’t visit the page.
An easy fix is to remove the following lines from app_dev.php
.
Extra: Improve performance
There are some quick tips to improve the speed of a Symfony2 application while developing.
- Use
/dev/shm
as your cache and logs location - Finetune the nfs mount command in the coreos-vagrant Vagrantfile
Next?
I’ve also written something about automagic discover docker containers with dnsdock and CoreOs.
Setting up a DNS service discovery with SkyDNS or DNSDock
Thanks for reading
Feel free to leave a comment if you have remarks or like this post