Multiple versions of AMP in One Host

Ridwan Shariffdeen
Docker Captain
Published in
3 min readJan 12, 2017

--

Working on multiple projects on different environments

When you are working on multiple projects with tight deadlines it becomes a pain in the neck to change your host environment to match the project requirements. Not only the hassle but the risk of ruining your almost optimum development environment which worked fine since the beginning of time, now needs to be altered. Working on multiple machines or virtual machines can solve the problem but the time for context switch could be a concern. So why not use docker container technology to solve this problem of environment isolation.

Few days back one of my friends tried Docker and was interested to use docker to orchestrate his development environment using Docker. The problem he was facing and actually a common issue for most linux developers is software change management. The scenario goes like this, he was doing well with LAMP setup of PHP 5.5 and MySQL 5.7 until he got a project which required him to use MySQL 5.5, which he thought should be a piece of cake and started to downgrade the MySQL version. He followed few tutorials here and there and was able to successfully downgrade to MySQL 5.5 but only to find out later that it was not working as expected. Spending hours to troubleshoot and figure out what went wrong, despite the fact there is a deadline to meet in the project he was frustrated and running out of safe options. This is where Docker came to help (obviously not a smooth solution but gets the problem solved).

Here is how you can easily set up different versions of software and use them at the same time without conflicting the resources of one another. Following docker-compose file is a simple setup to use two PHP versions and MySQL versions on the same host and complimentary tool phpmyadmin to make it feel like home. Let’s assume you are running Ubuntu Xenial which by default is packaged with PHP7 and MySQL 5.7. Now we want to use PHP 5.5 and 5.6 with MySQL 5.5 and 5.6 versions all at the same time without compromising the host setup.

version: '2'
services:
web55:
image: php:5.5-apache
ports:
- "7070:443"
volumes:
- ./web55/html:/var/www/html
- ./web55/php5/apache2/php.ini:/etc/php5/apache2/php.ini
- ./web55/php5/cli/php.ini:/etc/php5/cli/php.ini
- ./web55/apache_logs:/var/log/apache2
- /etc/localtime:/etc/localtime
links:
- db55
- db56
restart: always
container_name: dev_web_55

web56:
image: php:5.6-apache
ports:
- "7071:443"
volumes:
- ./web56/html:/var/www/html
- ./web56/php5/apache2.php.ini:/etc/php/5.6/apache2/php.ini
- ./web56/php5/cli/php.ini:/etc/php/5.6/cli/php.ini
- ./web56/apache_logs:/var/log/apache2
- /etc/localtime:/etc/localtime
links:
- db55
- db56
restart: always
container_name: dev_web_56

db55:
image: mysql:5.5
expose:
- "3306"
volumes:
- ./db55/config:/etc/mysql
- ./db55/mysql_logs:/var/log
- /etc/localtime:/etc/localtime
environment:
MYSQL_ROOT_PASSWORD: 1234
restart: always
container_name: dev_mysql_55
db56:
image: mysql:5.6
expose:
- "3306"
volumes:
- ./db56/config:/etc/mysql
- ./db56/mysql_logs:/var/log
- /etc/localtime:/etc/localtime
environment:
MYSQL_ROOT_PASSWORD: 1234
restart: always
container_name: dev_mysql_56
phpmyadmin:
image: phpmyadmin/phpmyadmin
volumes:
- /etc/localtime:/etc/localtime
links:
- db55
- db56
ports:
- "9090:80"
environment:
PMA_HOSTS: db55,db56
restart: always
container_name: dev_phpmyadmin

This docker-compose configuration file consists of 5 containers namely dev_web_55, dev_web_56, dev_mysql_55, dev_mysql_56 and dev_phpmyadmin. Note the naming convention I used to depict the version of the software. I have also linked each web container to both MySQL containers and same for phpmyadmin. This way you can have multiple combinations for different projects. If you are not familiar with the notations please see my article on Docker terminology and docker-compose usage. You can also refer the official Docker documentation which is self-explanatory and gives you handful of examples to understand.

Now all you have to do is correctly volume the settings you want inside the container. I have linked the html folder and few php settings so I can change these settings on run-time just as you do for your host setup. You can also volume the Apache settings and any other director/file as you prefer.

Once everything is setup, you can use PHP 5.5 by accessing with the port 7070 and PHP 5.6 with the port 7071. Within the application you can connect to different MySQL hosts using the host name db55 and db56. I have also included a multi host phpmyadmin which you can access using the port 9090 and allows you to login into both db55 and db56 containers. Hope this enlighten you to try out Docker to deploy multi version tools. Happy development!!

--

--