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.
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
For this blog post we will create a Drupal based blog called "MyBloge".
$ mkdir -p ~/Project/my-bloge
$ cd ~/Project/my-bloge
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
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
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 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
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:
version: '3'
services:
db:
image: mariadb:10.2
environment:
MYSQL_DATABASE: drupal
MYSQL_ROOT_PASSWORD: drupalPassword
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
build: .
ports:
- "8080:80"
volumes:
- ./app:/app
Create an app directory at the root of our project.
mkdir app
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.