Skip to main content

Drupal 8 Development with Docker on OSX

This tool improves Drupal, makes our website perform better and also increases the speed of development.
Veli Akçakaya
Veli Akçakaya
5 min. read
osxte-docker-ile-drupal-8-gelistirme

The old way of developing a Drupal site used to be like this: you uploaded your drupal source code to Shared Hosting and then configured your apache server, then made changes via FTP, and then did an occasional MySQL database backup just to be sure. Those days are gone. In 2019, there are many tools and best practices that allow us to efficiently develop and maintain a Drupal site and scale across different teams as well as infrastructure. Developing Drupal with this tool allows our website to perform better and the speed of development also increases.

Required Applications

  • Apache
  • Composer
  • Docker
  • Docker Compose
  • Drupal 8
  • Drush
  • Git
  • MariaDB or MySQL
  • PHP7

Docker

It is very easy to install Docker with Homebrew on OSX. If Homebrew is not installed, you can install it with the command line below.

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After installing Homebrew, install Docker with the following command.

$ brew install docker
$ brew cask install docker // GUI interface for Docker

Project

For this blog post we will create a Drupal based blog called "MyBloge".

$ mkdir -p ~/Project/my-bloge
$ cd ~/Project/my-bloge

Create Dockerfile

Dockerfile is a text document containing all the commands that a user can search from the command line to defragment an image.

$ touch ~/Project/my-bloge/Dockerfile

Fill the Dockerfile with the following:

FROM drupal:8.6-apache

RUN apt-get update && apt-get install -y \
    curl \
    git \
    mysql-client \
    vim \
    wget

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
    php composer-setup.php && \
    mv composer.phar /usr/local/bin/composer && \
    php -r "unlink('composer-setup.php');"

RUN wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/download/0.4.2/drush.phar && \
    chmod +x drush.phar && \
    mv drush.phar /usr/local/bin/drush

RUN rm -rf /var/www/html/*

COPY apache-drupal.conf /etc/apache2/sites-enabled/000-default.conf

WORKDIR /app

Description

  • This is the official Docker Library Drupal Image. By using this image we don't need to configure Apache and PHP.
FROM drupal:8.6-apache
  • We install the necessary applications for Drupal development in Docker Image
RUN apt-get update && apt-get install -y \
    curl \
    git \
    mysql-client \
    vim \
    wget
  • Then we install composer with the following command:
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php composer-setup.php && \
mv composer.phar /usr/local/bin/composer && \
php -r "unlink('composer-setup.php');"
  • We're setting up Drush:
RUN wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/download/0.4.2/drush.phar && \
chmod +x drush.phar && \
mv drush.phar /usr/local/bin/drush
  • We delete the existing Drupal in Docker Image. We will install Drupal with Composer.
RUN rm -rf /var/www/html/*
  • Apache Conf.
COPY apache-drupal.conf /etc/apache2/sites-enabled/000-default.conf

Here we synchronise the apache-drupal.conf file in local with the 000-default.conf file in the image. Changes made in the apache-drupal.conf file will automatically be reflected in the 000-default.conf file in the image.

Create Apache VHost Config File

Create an apache-drupal.conf file with the following content.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /app/web

    <Directory /app/web>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Create Docker Compose File

Our Drupal site will consist of two Docker images. The main image that holds Drupal-specific things, we created this image with the Dockerfile file we created, we also created the database image. Docker Compose allows us to create containers from these images and will provide the connection between these containers automatically.

Docker Compose file should also be created at the root of our project, ./docker-compose.yml.

version: '3'
services:
  db:
    image: mariadb:10.2
    environment:
      MYSQL_DATABASE: drupal
      MYSQL_ROOT_PASSWORD: drupalPassword
    volumes:
      - db_data:/var/lib/mysql
    restart: always
  drupal:
    depends_on:
      - db
    build: .
    ports:
      - "8080:80"
    volumes:
      - ./app:/app
    restart: always
volumes:
  db_data:

Description

  • Version of Docker Compose.
version: '3'
  • We create the database (db) container as drupal database and drupalPassword encrypted.
services:
  db:
    image: mariadb:10.2
    environment:
      MYSQL_DATABASE: drupal
      MYSQL_ROOT_PASSWORD: drupalPassword
  • We specify that we want to use a Docker Volume named db_data to store the contents of /var/lib/mysql. This directory is where MariaDB stores its information for the databases it contains.
 volumes:
      - db_data:/var/lib/mysql
Here we define the second container as `drupal` which is connected to another container called `db`.drupal:
    depends_on:
      - db
  • Instead of using an image like we did for DB and then specifying a Docker image, this tells Docker Compose to use the local Docker file as the basis for our image. This means we can build on the fly without running a separate command, or worse, forcing it into a common Docker repository to make a quick change.
build: .
  • Map port 8080 on our local region to port 80 on the Docker machine. This will allow you to access our Drupal website from our local machine.
ports:
      - "8080:80"
  • This links the ./app folder, the root of the project, to the /app folder inside the Docker container.
  volumes:
      - ./app:/app

Create Application Directory

Create an app directory at the root of our project.

mkdir app

Building The Drupal 8 Website

Once Docker is configured, we can now create our containers using the following command:

docker-compose up -d --build

--build tells Docker Compose to build our 'Dockerfile' fresh when we run this command.

Login to the container using the following command:

docker exec -it MyBloge bash

After logging in to Container, we can install Drupal 8 with Composer.

/app #  composer create-project drupal-composer/drupal-project:8.x-dev /app --stability dev --no-interaction
/app #  mkdir -p /app/config/sync
/app #  chown -R www-data:www-data /app/web

Now visit http://localhost:8080 in your browser and you can install Drupal from the interface.

Our Offices

Drupart Locations

Our Officess

London

151 West Green Road, London, England

442038156478

[email protected]

Drupart R&D

GOSB Teknopark Hi-Tech Bina 3.Kat B3 Gebze - KOCAELİ

+90 262 678 8872

[email protected]

Newark

112 Capitol Trail Suite, A437 Newark DE, 19711

+17406666255

[email protected]

Wiesbaden

Hinterbergstraße 27
65207 Wiesbaden
Deutschland

+49 (0) 6151 – 492 70 23

[email protected]