Monday, 7 March 2016

Setting up a drupal development env using docker image on mac os.

I have been trying to try various ways in which i can make building and destroying development environments quickly and also make them as close to production as possible, i have tried using vagrant before till i was bitten by the docker bug.

There is an official docker image for drupal 8 and drupal 8 on the docker hub, i have tried using them before it wasn't a great wow moment to be honest.

Creating a drupal image using official drupal docker image form docker hub.

The docker image did not have drush or drupal console so it was a bit disappointing. I searched and came across https://hub.docker.com/r/iiiepe/nginx-drupal/ The guys have done a lot of work in using docker in their deployment workflow, so i wanted to give this a try.

I could have used docker hub to pull the iiiepe image, but i wanted to see what is inside and if i need to make changes i could.

I first created a folder in my docker subfolder, I normally keep docker images and my projects separate. I checked out the git repo https://github.com/iiiepe/docker-nginx-drupal

Obviously you need to have docker installed and configured properly, you can find a lot of tutorials for this on the internet, but basically you need to start your default docker machine i.e.

docker-machine start default

then, set the env like so

docker-machine env default

and type the command with eval() in the output of docker-machine env default command.

Now i am ready to create the docker images as below:

I went to the folder which has the dockerfile, then gave the following command

docker build -t drupalprogrammer/nginx-drupal .

This will pull appropriate images and begin to build images, in a few mins this will be all done and you can check the images by giving the docker images command


We will also need a mysql image for our drupal container to work with, we could have gone for the official mysql image however it doesnt work very well with os x because we want to keep the data outside the container and there is some work required to create scripts to allow this to happen.

There is instructions on the official mysql images page i.e. https://hub.docker.com/_/mysql/ i tried to use the instructions to load the data outside but could not get it to work. Luckily little googling led me to another image we can use.

run the following command:

docker run --name mysql-data -v /Users/nidash/data:/var/lib/mysql -p 3306:3306 -d dgraziotin/mysql

This will pull dgraziotin/mysql image and expose the 3306 port to use.

Now give the following command docker ps this shows the some-mysql container working, great job. 

The first time you create the container this will also create an additional user called admin with a random generated password, to find the password you need to look at the docker logs like so.

docker logs mysql-data

So this shows the password like below:

You can now connect to this MySQL Server using: mysql -uadmin -p47nnf4FweaKu -h -P Please remember to change the above password as soon as possible! MySQL user 'root' has no password but only allows local connections

Using a mysql client connect to the mysql server and change your password eg.

SET PASSWORD FOR 'admin'@'%' = PASSWORD('password');
Now we need to the drupal container to be up as well, so here is the command to use.

docker run -d -p 8000:80 -v /Users/nidash/work/drupal7/:/var/www -v /Users/nidash/work/drupal7/sites/all/default/files:/var/www/sites/default/files  drupalprogrammer/nginx-drupal

We need to map 2 volumes, /Users/nidash/work/drupal7 or where the codebase is to /var/www and because drupal is drupal and has files inside the code base, we need to map and mount this separately hence the other -v.

Complete the installation and that is it. Ensure the database host name is set as the IP the docker-machine env default command reports.



Tuesday, 1 March 2016

Creating a drupal 8 container from scratch.

I tried the official drupal container according to https://www.drupal.org/node/2538074

Was'nt happy with the result  so i thought build one from scratch.

What i want my drupal container to do?

1. Use my codebase which will be outside the container
2. Able to install drupal - correct file system permissions.
3. Clean urls should work
4. Drush & drupal console installed
5. Xdebug installed
6. Xhprof or any other profiler installed (optional)

Anything else?

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.

Pre-requisite:
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!

Sunday, 13 September 2015

Use Symfony2 to create template/skeleton code

Pre-requisite: A lamp environment, i will be using a vagrant lamp box (scotch-box) i created earlier.

To understand Symfony2 there is nothing better than getting ones hands dirty. We will try to use doctrine to build models, create views using twig and maybe use a dash of frontend magic using backbone or something similar.

For the test project I chose a sales bundle where we will make models for item, item type and sale.

Using symfony2 command line to create a model.

Because i have used vagrant scotch-box to provision my dev environment, i need to first make sure i do vagrant up to start the VM. then i do vagrant ssh this will take me to the ssh


locate where you have symfony installed, in my case it is /var/www/public/sandbox and give the following command:

php app/console generate:bundle

pick a name any name....well not actually, we need to be following coding standards set out in the symfony guide to ensure we name our bundle like this Bundle, so in my case i didn't have a vendor, so i chose a project name in this case it was Test\SandboxBundle, the bundle was named TestSandboxBundle and relevant files were created for me.



I just chose the defaults and had symfony create all it deemed good and wholesome...this will be additionally helpful if we peep under the hood as to what and where things are normally stored.

Now our bundle is created, we can create some models so in the symfony root in my case /var/www/public/sandbox i type the following command in order to generate the entities.

Entities we need: We are attempting to create a pos system, so we will have the following models:

ItemType - Types of item eg. Toys, Bakery, Drinks etc
  Id, Name (string:50) & Description (string: 250)

Item - The item, this will linked with item type
  Id, ItemTypeId (int), Name (string:50), Description (string:250), Price (descimal), Opening Stock (int)

Sale - the actual sale which will link to the item.
  Id, Date (date time), ItemId (int), Quantity(int), CustomerId(int)

Customer - the customer, to make things easier, we will create only 2 customers 1 pos & the other for WEB orders.
  Id(int), Name (string:50), Address (string:250), postcode (string:12)

So we generate these models one by one, the options i have chosen are as follows:

The Entity shortcut name
: TestSandboxBundle:

Configuration format for all (yml, xml, php, or annotation) [annotation]: annotation

Note that the primary key will be added automatically (named
id). When it asks do you want to generate an empty repository class say yes.

You can now start using the generated code!