How to use Docker for Drupal Development

drupal docker

At the end of 2014, one of OSTraining team member chose 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 trying to configure all the developers environments to work perfectly for the application you need to run.

Perhaps the best way to solve these issues is with virtualisation. There are many different solutions that try to solve problem with Virtual Machine, but they often eats up the RAM of local developer and also add an extra maintenance burden on DevOps team. Docker is the most reliable virtualisation solution that we’ve found.

I’m going to explain how to use Docker with Drupal. We’ll see how to write your own DockerFile for Drupal Environment.

I’m going to assume that you have Ubuntu on your computer. If you have Mac then you can either use Boot2Loader or Vagrant.

What services do you need for Drupal and Docker?

  • HTTP Server with PHP: We can either use Apache with PHP or Nginx with PHP. I’m going to demonstrate building the Docker using Apache with PHP. A 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). I’m going to be using a MySQL docker. The idea behind using a separate docker for SQL is so that you have a freedom to choose an internal SQL service or an 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, such as Ubuntu, CentOS and Alpine. I’m going to be using a standard Ubuntu base image for dockers: Ubuntu Phusion which is a minimal Ubuntu base image modified for Docker-friendliness.

Read more details on Github about 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’ll be creating the Dockerfile. Comments in Dockerfile starts with a 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 the Drupal Docker Image

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

After the 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 Drupal 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}
  • Hit your browser with http://localhost:49980/ and check you get default Drupal installation file.
  • In “Database Settings”, choose
    • Database URL: 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.

Drush Alias of Drupal inside Docker

You can also 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

$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.

Author

0 0 votes
Article Rating
Subscribe
Notify of
guest

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Firoz
Firoz
8 years ago

Great tutorial !!!

Wib
Wib
8 years ago

Some of your code blocks need new lines…

purewm
purewm
8 years ago

You called your apache run file ‘[url=http://apache-2-run.sh]apache-2-run.sh[/url]’ at first but your Dockerfile is then looking for ‘[url=http://apache2-run.sh]apache2-run.sh[/url]’ (one less hyphen). Might want to edit that in your instructions. Cheers.

purewm
purewm
8 years ago

Is apache2.conf meant to remain empty?

Jipoll
Jipoll
8 years ago

Check out this open-source initiative [url=https://github.com/Wodby/docker4drupal]https://github.com/Wodby/do…[/url] You can use it to spin up local environment for Drupal with Docker native app (1.12) on win/mac/linux

5
0
Would love your thoughts, please comment.x
()
x