Tuesday, 23 February 2016

Create a drupal development environment using docker.

Currently at work i am using docker to streamline and speedup of development and deployment to testing and production, I am using symfony currently and our app is dockerised, i wanted to do the same for drupal based apps.

I searched for a official drupal docker repo and i did find this. https://www.drupal.org/node/2538074

Unfortunately there was very terse explanation but pretty much worked, i found a few hiccups along the way so wanted to share what i did.

Install docker for your environment. Lots of details are available at https://docs.docker.com/ explaing docker and installing docker for your environment.

The docker repo holds the drupal container at https://hub.docker.com/_/drupal/, following the instructions this is how to create the drupal container:

In order to use my code base we need to mount the volume with the -v directive as below, we do this in one command:

docker run -v /$HOME/work/drupal8:/var/www/html -p 8080:80 --name mydrupal -d drupal

this will create a drupal container using drupal code in your home folder, this brought up a new site however soon i had a problem where

Because i was using my local folders mapped to /var/www/html i need to ensure the sites/default/files are writable, normally you should be careful what permissions you should set, more information is on drupal site. for now 777 will do, so i did a chmod 777 $HOME/work/drupal8/sites/default and also created a settings.php as instructed. Also chmod 777 $HOME/work/drupal8/sites/default/files.

Now comes the database part, at this point i created another container with mysql, fortunately a official mysql repo is also available on hub.docker.com

docker run --name mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3307:3306 -d mysql:5.7

giving the command above download the mysql 5.7 image, set the root password, expose the mysql port in the container externally, now our drupal app can use this container. normally you would store the data outside the mysql container, the repo page of mysql on the docker hub discusses that, for demonstration purpose it is just fine to have out data inside.

i also connected to this database container using sequelPro and created a new database to use and named it drupal8, then continued with the installation.

Unfortunately then drupal died, with the following error:

I can again gave permissions, but ended again with another permission issue. Thank God i am using docker...so i dropped the drupal container and decided to mount modules and themes folder seperately, and use drupal core inside the container.

So first i drop the container as below:

docker rm -f mydrupal

so my modified docker run now becomes:

docker run -v /$HOME/work/drupal8/modules:/var/www/html/modules -v /$HOME/work/drupal8/themes:/var/www/html/themes -p 8080:80 --name mydrupal -d drupal

Notice the use of -v to use external folders for modules & themes otherwise use the drupal codebase inside the drupal container.

now reinstalling again. I had to drop the database and recreate a blank database and finish the install.

Somethings to remember, us the docker machine IP address, which you get by running docker-machine env default, use this IP address in the database config as well as the browser to look at your new drupal site!

All Done!

1 comment: