I wanted to spin up an existing Drupal site with Docker for quick debugging and ran into a few issues.

The docker-compose.yml file has three services: web, db, and memcached. The memcached service is straight forward:

  memcached:
    container_name: drupal_memcached
    image: memcached:1.6.6
    ports:
        - "11211:11211"

For the database, one of the errors thrown was Error while sending QUERY packet, which was fixed with command: --max_allowed_packet=10000M. The database snapshot was placed in .docker/db-backups and mapped it to /docker-entrypoint-initdb.d/ so that it is loaded when the container is initialized.

  db:
    container_name: drupal_db
    image: mysql:5.7
    command: --max_allowed_packet=10000M
    environment:
      MYSQL_DATABASE: drupal
      MYSQL_USER: drupal
      MYSQL_PASSWORD: drupal
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - .docker/db-backups:/docker-entrypoint-initdb.d/
      - .docker/mysql:/var/lib/mysql/
    ports:
      - "3306:3306"
    restart: always

The quickest way to connect the Drupal with the database was to pass the database service name as well as the credentials as environment variables:

  web:
    container_name: drupal_web
    build: .
    depends_on:
      - db
    environment:
      DRUPAL_DATABASE_HOST: db
      DRUPAL_DATABASE_PORT: 3306
      DRUPAL_DATABASE_NAME: drupal
      DRUPAL_DATABASE_USERNAME: drupal
      DRUPAL_DATABASE_PASSWORD: drupal
      MEMCACHED_HOST: memcached
    ports:
      - "8081:80"
    volumes:
      - .:/app
    restart: always

In the settings.local.php, the db and memcached info can be referenced as follows:

$databases['default']['default'] = [
  'host' => getenv('DRUPAL_DATABASE_HOST'),
  'database' => getenv('DRUPAL_DATABASE_NAME'),
  'username' => getenv('DRUPAL_DATABASE_USERNAME'),
  'password' => getenv('DRUPAL_DATABASE_PASSWORD'),
];

// Memcache.
$memcached_host = getenv('MEMCACHED_HOST');
$settings['memcache']['servers'] = ["$memcached_host:11211" => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['memcache']['key_prefix'] = 'drupal_local';
$settings['cache']['default'] = 'cache.backend.memcache';

Even with memcached, though, the site is very slow. I read this might be due to an overhead associated with the file system on Mac (since we are binding the local files to the container). There are some solutions out there, but that’s for another day.

Resources