To experiment with the Pattern Lab Starter theme we began with a vanilla Drupal 8 installation, and then quickly spun up our local Docker development environment using Docker4Drupal. We then copied the Pattern Lab Starter code to a new custom/theme/patter_lab_starter
directory in our Drupal project.
Running the Phase 2 Pattern Lab Starter theme requires Node.js, the node package manager npm, PHP, and the PHP dependency manager Composer. Node and npm are required for managing the theme’s node dependencies (such as Gulp, Bower, etc.), while PHP and Composer are required by the theme to run and serve Pattern Lab.
While we could install these applications on the host machine, outside of the Docker image, that defeats the purpose of using Docker. One of the great advantages of virtualization, be it Docker or a full VM, is that you don’t have to rely on installing global dependencies on your local machine. One of the many benefits of this is that it ensures each team member is developing in the same environment.
Unfortunately, while Docker4Drupal provides public images for many applications (such as Nginx, PHP, MariaDB, Mailhog, Redis, Apache Solr, and Varnish), it does not provide images for running the applications required by the Pattern Lab Starter theme.
One of the nice features of Docker though is that it is relatively easy to create a new image that builds upon other images. This is done via a Dockerfile
which specifies the commands for creating the image.
To build an image with the applications required by our theme we created a Dockerfile
with the following contents:
FROM node:7.1
MAINTAINER Dan Murphy <dan@savaslabs.com>
RUN apt-get update && \
apt-get install -y php5-dev && \
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
# Directory required by Yeoman to run.
mkdir -p /root/.config/configstore \
# Clean up.
apt-get clean && \
rm -rf \
/root/.composer \
/tmp/* \
/usr/include/php \
/usr/lib/php5/build \
/var/lib/apt/lists/*
# Permissions required by Yeoman to run: https://github.com/keystonejs/keystone/issues/1566#issuecomment-217736880
RUN chmod g+rwx /root /root/.config /root/.config/configstore
EXPOSE 3001 3050
The commands in this Dockerfile
:
- Set the official Node 7 image as the base image. This base image includes Node and npm.
- Install PHP 5 and Composer.
- Make configuration changes necessary for running Yeoman, a popular Node scaffolding system used to create new component folders in Pattern Lab.
- Expose ports 3001 and 3050, which are necessary for serving the Pattern Lab style guide.
From this, Dockerfile
we built the image savaslabs/node-php-composer
and made it publicly available on DockerHub. Please check it out and use it to your delight!
One piece of advice I have for building images for local development is that while Alpine Linux based images may be much smaller in size, the bare-bones nature and lack of common packages brings with it some trade-offs that make it more difficult to build upon. For that reason, we based our image on the standard DebianJessie
Node image rather than the Alpine variant.
This is also why we didn’t just simply start from the wodby/drupal-php:7.0
image and install Node and npm on it. Unfortunately, the wodby/drupal-php
image is built from alpine:edge
which lacks many of the dependencies required to install Node and npm.
Now a Docker purist might critique this image and recommend only “one process per container”. This is a drawback of this approach, especially since Wodby already provides a PHP image with Composer installed. Ideally, we’d use that in conjunction with separate images that run Node and npm.
However, the theme’s setup makes that difficult. Essentially PHP scripts and Composer commands are baked into the theme’s npm scripts and gulp tasks, making it difficult to untangle them. For example, the npm start
command runs Gulp tasks that depend on PHP to generate and serve the Pattern Lab style guide.
Due to these constraints, and since this image is for local development, isn’t being used to deploy a production app, and encapsulates all of the applications required by the Pattern Lab Starter theme, we felt comfortable with this approach.