Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

How to use Docker for Drupal Development

Parent Feed: 
drupal docker

At the end of 2014, one of the OSTraining team choose Docker as their favorite innovation of the year.

Why is Docker so great? Often developers discover code that’s working on a local machine may not work as expected in other environments.

Also a lot of time is often wasted in cases of new developer joining a team and configuring developers environments to work perfectly for the application you need to run.

For solving these issues, the virtualisation to match exact OS is a solution. There are many solution which tries to solve problem via Virtual Machine, but they eats up the RAM of local developer and also add extra maintenance burden on DevOps team. 

Think of Virtual Environment where you can write the code for your Operating System and share on all environments. Yes that’s what docker based environment provide.  The idea of writing this article is to make you familiar on how to write your own DockerFile for Drupal Environment. This article assume that you have Ubuntu on your computer. If you have Mac then you can either use Boot2Loader or Vagrant. 

What services you need for Drupal?

  • HTTP Server with PHP: We can either use Apache with PHP or Nginx with PHP. Here we will demonstrating building the docker using Apache with PHP. Drupal Docker can also have services like ssh (for drush alias to work) and some important utilities like vim
  • SQL Server: Choose your favourite SQL Service (MySQL or PostGRESQL or SQLite). Here we shall be demonstrating using MySQL Docker. Idea of using separate docker for SQL service is to have a freedom to choose internal  SQL Service on other Docker or a external SQL Services like Amazon RDS without affecting your Drupal environment. 

Choosing Base Image of Docker?

There a plenty of options you can use for building a docker, like Ubuntu, CentOS, Alpine etc  Here we will be using  standard Ubuntu base image for Dockers i.e. Ubuntu Phusion which is a minimal Ubuntu base image modified for Docker-friendliness.

Read more details on github for building Drupal Docker. 

  • Create a directory with name say dev-docker-repo, anywhere on your computer.
  • Download Drupal to /home/user-name/share/drupal 

Your directory structure should look like this:

  • dev-docker-repo
    • Dockerfile
    • apache-2-run.sh
    • apache2.conf

Let’s see the content of each file, first we will be creating the Dockerfile. Comments in Dockerfile starts with pound character (#).

Drupal Dockerfile Sample

#Base Image
 FROM phusion/baseimage
 #Maintainer
 MAINTAINER Ankit Babbar <babbar.ankit@gmail.com>; 
#locale settings for Ubuntu
 RUN locale-gen en_US.UTF-8
 ENV LANG       en_US.UTF-8
 ENV LC_ALL     en_US.UTF-8 
#Install Necessary Software
 RUN apt-get update -y
 RUN DEBIAN_FRONTEND="noninteractive" apt-get install -y vim curl wget
 RUN add-apt-repository -y ppa:ondrej/php5
 RUN apt-get update -y 
#Install Apache and PHP 
 RUN DEBIAN_FRONTEND="noninteractive" apt-get install -y --force-yes php5-cli  \
  php5-mysql  php5-curl php5-gd php5-mcrypt apache2 drush libapache2-mod-php5
 RUN php5enmod mcrypt
 RUN a2enmod rewrite 
#Use baseimage-docker's init system.
 CMD ["/sbin/my_init"] 
#apache2 Environment Variables, and config settings
 ENV APACHE_RUN_USER www-data
 ENV APACHE_RUN_GROUP www-data
 ENV APACHE_LOG_DIR /var/log/apache2
 ENV APACHE_LOCK_DIR /var/lock/apache2
 #ENV APACHE_PID_FILE=/var/run/apache2.pid 
#runit service files
 COPY ./apache2-run.sh /etc/service/http/run 
#runit service permissions
 RUN chmod +x /etc/service/http/run 
EXPOSE 80
 ## /apache2 
#apt-cleanup
 RUN apt-get clean
 RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

apache2-run.sh

This script will run apache2 as a background process

#!/bin/bash /usr/sbin/apache2 -D FOREGROUND

Create Drupal Docker Image

This script will run apache2 as a background process

$ cd dev-docker-repo $ docker build . ----- ----- Successfully built {docker_image_id} 

After build command a docker image id like 6762f304c834 will be generated for Ubuntu with all required services. Here we are using MySQL Community Docker Image for simplicity. Also we assume that you have a Drupal is downloaded at /home/user-name/share/drupal.

$ docker run --name mysql-db -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.6 $ docker run --link mysql-db:mysql-db -d -p 49980:80 -v /home/user-name/share/drupal:/var/www/html {docker_image_id} 

(You can possibly think of writing your mysql Dockerfile if need be so).

Hit your browser with http://localhost:49980/ and check you get default Drupal installation file In Database Settings Choose Database URL as mysql-db, User root, Password my-secret-pw.

This completes your Vanilla Drupal running inside a docker. You can run the same docker now in all environments.

Further you can create a Drush Alias. Drush Alias is nice way to work with Drupal inside your docker. To create a drush alias you will have to enable ssh and expose port 22. Then write a Drush Alias (assuming 49922 being the port map of ssh docker), crush alias file ~/.drush/docker.aliases.drushrc.php

Drush Alias of Drupal inside Docker

$aliases['drupal’'] = array( 'root' => '/var/www/html', 'remote-user' => 'root', 'remote-host' => 'localhost', 'ssh-options' => '-p 49922', );

Type these commands on your base OS

 $ drush cc drush $ drush sa #see alias below @docker @docker.drupal ……. $ drush @docker.drupal cc all or $ drush @docker.drupal {drush_command} 

Further ideas

There is a sample Vagrantfile in the Phusion Github Repo. You can possibly think of running Ubuntu Docker on CoreOS. You can also think of Continuous Integration using CircleCI.


About the author

Ankit has been a Drupal developer for more than 4 years and also has extensive experience with cloud services from Digital Ocean, AWS, Rackspace, Google Compute Engine.


Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web