Using Drush in a docker Drupal development environment

Tech Notes

There are a number of ways to roll Drupal with Docker. We've been using phpdocker.io for a long time for various LAMP projects and had never looked at the Drupalised Docker images--mainly for consistency across projects. However the downside of going off campus is lack of a tuned database and difficulties using Drush.

We'd tried many approaches to this and none have worked consistently. Recently, however, we put in a bit more effort while updating an aging development stack for a long running project.

Firstly though we were striking issues with MariaDB running out of memory:

PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away:

This is a fairly common problem and fairly easy to fix: add resources to MariaDB's my.cnf file as per the note on Drupal.org.

Not so easy to do with Docker. Here's how to do it on top of the phpdocker.io docker-compose.yml file. Add the command containing Drupal's recommended defaults.

    mysql:
      image: mysql:5.5
      command:  --key-buffer-size=384M --max-allowed-packet=64M --table-open-cache=4096 --read-buffer-size=2M --read-rnd-buffer-size=64M --myisam-sort-buffer-size=64M --thread-cache-size=8 --query-cache-size=32M --innodb-buffer-pool-size=384M --innodb-additional-mem-pool-size=20M --innodb-log-buffer-size=64M --innodb-lock-wait-timeout=180

The next, more difficult, challenge was getting Drush working well. Often we could get drush status working, or drush download, or drush sql-cli, with a bit of alias hacking. However it was always a bit agricultural and required switching methods to perform different actions. 

The current solution, which seems to be working well at the moment, is using the drush/drush:7 image inside our docker-compose.yml file. Here's how it goes:

    drush:
        image: drush/drush:7
        container_name: drush
        volumes: - .:/application

Then kill and restart docker-compose and run drush via docker-compose:

$ docker-compose run drush --uri=http://webserver --root=/application/public sql-cli

Note that URI is made up of the docker-compose web server container name and root is the docker volumes alias and any additional path.

--uri=http://[webserver-container-name] --root=[volumes]/[root-path]