The simplest Dockerized Drupal Imaginable.

Unless you've been living under a rock recently I'm sure you've heard all the buzz about Docker. If not read on and I'll explain all about how it can help your Drupal development life be that much easier.

The preconception that people have about Docker is that it's going to just be a stand in for their VMs. It's not. A virtual machine is usually this giant monolith with all the requred dependencies either on the same machine or cluster. For example if we look at the excellent Laravel Homestead vagrant box https://laravel.com/docs/4.2/homestead you can see that all the dependencies run inside that.

Production sites need to work a little differently. It's already quite common and established for a scaling well designed application that individual services and dependencies are separate from each other. This allows the administrator to scale, debug and isolate individual resources. Docker works exactly like that.

Docker is just like building an application with LEGO®.

Ingredients for a basic Drupal Site:

  • Apache with mod-php (You could complicate this with nginx and php-fpm but not today!)
  • MySQL

Because most of the leg work has been done for us, once you have the tooling set up you'll be able to fire up fresh environments in seconds. The best part? It's going to be an almost carbon copy of your production server. This brings us one step closer to the holy grail of having no more "Works on my machine".

What makes Docker so fast is that services are composed of cached images which can derive from each other. For example the official Drupal docker image is derrived from the official PHP image which in turn is derrived from the official Ubuntu Image. This makes it very quick to run and spin up a fresh one.

Docker Installation

If you're on Linux just follow the instructions here https://docs.docker.com/engine/installation/linux/ubuntulinux/ you're a linux user you don't need babysitting! 

Now, for the kids on OSX and Windows:

You'll need a tool called Docker Toolbox. This makes it super simple to just get started. It'll install VirtualBox on your machine(if you dont' already have it) and will set up the tools that you need to get started. Grab it from https://www.docker.com/products/docker-toolbox 

Docker Toolbox will install

  • The Docker Virtual Machine - Alas Docker on runs in linux. So this gives you a tiny VM which you'll never need to touch (often).
  • Docker Compose - This is the tool which allows you to run multiple containers at once.
  • Docker Quickstart Terminal - This sets all the environment variables and settings you'll need in your terminal
  • Docker Client - Just the client which will run on your host operating system

Scaffolding our Drupal Infrastructure

As I said before we need MySQL and Apache for Drupal to function (and we're going to take a quick shortcut).

Open up the Docker Quickstart Terminal (Don't use your normal terminal, the environment variables need to be set and VM gets created on first run) 

Create a directory that we can work from:

mkdir drupal_docker
cd drupal_docker

Now create a file called docker-compose.yml

Inside that file we're going to need this little bit of yml code:

drupal:
  image: drupal:8
  ports:
    - 80
  links:
    - mysql
mysql:
  image: mysql:5.6
  environment:
    - MYSQL_RANDOM_ROOT_PASSWORD=true
    - MYSQL_DATABASE=drupal
    - MYSQL_USER=drupal
    - MYSQL_PASSWORD=drupal

(for a real sites don't put your passwords here - use your build process or env variables)

Next run

docker-compose up -d

This will grab all the images required and start up the server and throw you back to the bash prompt.

If you're on OS X or Windows then you'll need one extra step.

On OSX and Linux get the IP address of the Docker VM 

docker-machine ip default

Then we need to grab the port that docker has automatically assigned to the container. To get that easily just run

docker-compose port drupal 80

That'll produce something like:

0.0.0.0:32802

Navigate to http://DOCKER_IP:DOCKER_PORT and you should see the friendly Drupal 8 installation page! Easy eh? Any questions let me know.

Credit to the people that made the official Drupal docker image. It's fantastic.

Add new comment