Installing Drupal 8 with the help of composer and Docker


In this tutorial, we are going to check how to install Drupal 8 with the help of drupal-composer and docker4drupal projects. As you know from the 8th version of Drupal it is highly recommended to use composer for your setup and the drupal-composer project is the recommended structure. My personal structure is slightly improved but is also inspired by the drupal-composer project ( I will present that in a different article ). 

How to install composer and docker you may find here and here. Also, you would have to have git installed

So let's start with some words about the two projects - first drupal-composer is a perfect kickstart that will help you to easily maintain your module dependencies in Drupal 8 with the help of composer. We will not get into too many details about it, as the project Github page covers it pretty well.

Regarding the docker4drupal project - it is a docker compose oriented setup that presets definitions of several Docker containers that are needed in order to build and have a functional development environment literally in minutes.

Let's start with creating our drupal-composer setup. In order to do that you may just change from the terminal to the folder where you have your Drupal projects and execute the following:

composer create-project drupal-composer/drupal-project:8.x-dev drupal_dandy --stability dev --no-interaction

this will create a folder, called drupal_dandy with the content of the drupal-composer boilerplate. As a folder structure it should look something like this:

Drupal composer folder structure

This means we have our Drupal codebase prepared and built. Let's now go ahead and take care of building a server where we could install, test and further develop our Drupal 8 project. As we said we will use the docker4drupal. If we go to the GitHub page of the project and get the GitHub link to it we can just clone it in another folder like this:

git clone drupal_docker_server

Then we could just remove the .git file and then copy the content of the drupal_docker_server into the drupal_dandy folder like this:

rm -R drupal_docker_server/.git

cp -R drupal_docker_server drupal_dandy

It is time to change a bit the docker_compose.yml file in order to have it ready for our project structure. Basically, we have declared all the containers that we will create and they will be connected internally and will be part of an environment on which we will run our Drupal 8 projects. 

If we start with the PHP container - there is a possibility to have directly Drupal installed but I like more the idea of having a PHP, let's say 7.x there and that we have our Drupal codebase handled by our neat drupal composer project setup. So in the lines of the PHP container - we would comment out the image with the Drupal and we will uncomment a PHP image. Also, we will change the volumes path so that it starts from the current folder. It will look like this:

# 1. Images with vanilla Drupal – wodby/drupal:[DRUPAL_VERSION]-[PHP_VERSION]-[STABILITY_TAG].
#    image: wodby/drupal:8-7.1-3.0.0
#    image: wodby/drupal:8-7.0-3.0.0
#    image: wodby/drupal:7-7.1-3.0.0
#    image: wodby/drupal:7-7.0-3.0.0
#    image: wodby/drupal:7-5.6-3.0.0
#    image: wodby/drupal:6-5.6-3.0.0
#    image: wodby/drupal:6-5.3-3.0.0
# 2. Images without Drupal – wodby/drupal-php:[PHP_VERSION]-[STABILITY_TAG].
    image: wodby/drupal-php:7.1-3.0.0
#    image: wodby/drupal-php:7.0-3.0.0
#    image: wodby/drupal-php:5.6-3.0.0
#    image: wodby/drupal-php:5.3-3.0.0
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      PHP_FPM_CLEAR_ENV: "no"
      DB_HOST: mariadb
      DB_USER: drupal
      DB_PASSWORD: drupal
      DB_NAME: drupal
      DB_DRIVER: mysql
#      PHP_XDEBUG: 1
#      PHP_XDEBUG_PROFILER_OUTPUT_DIR: /mnt/files/xdebug/profiler
#      PHP_XDEBUG_TRACE_OUTPUT_DIR: /mnt/files/xdebug/traces
      - ./:/var/www/html

Also for the nginx volumes, we could change the volumes as well:

      - ./:/var/www/html

And for accessing the built containers I like to change the ports to 8888 like this:

    image: traefik
    command: -c /dev/null --web --docker --logLevel=INFO
      - '8888:80'
#      - '8080:8080' # Dashboard
      - /var/run/docker.sock:/var/run/docker.sock

Aaand we are ready to build. We would start Docker and then open the terminal.

In the terminal, we would have to make sure we are in the drupal_dandy folder and we could build all the containers like this:

docker-compose up -d

After it is ready we could check which containers are built with

docker ps

Afterwards, we could access our Drupal install screen by going to http://drupal.docker.localhost:8888

How it worked so smooth you would ask... Well, the drupal composer project is creating a folder structure where there is a web folder, containing the whole Drupal codebase and the PHP and Nginx containers are expecting the web folder in the root folder too. As we merged the two projects - it is there and you are able to access it directly under http://drupal.docker.localhost:8888

Drupal 8 composer install

And the last important bit that you have to know when installing is that when you are on the screen where you input the database cridentials - you would have to use the ones specified in the docker_compose.yml :

      DB_HOST: mariadb
      DB_USER: drupal
      DB_PASSWORD: drupal
      DB_NAME: drupal
      DB_DRIVER: mysql

as for inputting the host - you would have to click on advanced when you are on the screen with the database credentials.

If you want to compare something - feel free to check the code from the example on our GitHub page and also you may see the whole process in the video below.

Thanks for reading and don't hesitate to ask questions in the comment section below.


Meet the author

Nikolay is a Drupal PHP developer with more than 7 years of experience. Passion for teaching, passion for simplicity and effectiveness of the code are just some keywords that he cares about. See more ...
Drupal Up is a platform for learning Drupal ...

Drupal Dan promoting
The idea is though not just to be another platform with some boring videos but all the videos to be inspired by real-world problems. That means something that you will most probably need if you work with Drupal. We want you to be successful and we want to teach you everything we know about Drupal!
So watch the videos, try to follow and reproduce, ask questions and you will see you will be able to conquer this wonderful sea that Drupal 8 is.