[Drupal] How to install Drupal using Docker
In this article, I will explain how to install Drupal using Docker. If you have ever tried to install Drupal on your PC, you probably know that Drupal installation is complicated. Yes, it is difficult to successfully install Drupal manually. However, using Docker, we can set up a Drupal dev environment quite easily. We only need to make a text file and run a single command. Once you read this article, you will be able to set up a Drupal dev environment and start using Drupal!
Table of Contents
Before I start to explain the method, let me write a little about the requirement for this method. Before you go on to the steps below, please be sure that your PC has the Docker engine and Docker compose. This method uses Docker, so these need to be installed on your machine. If you don’t have the Docker engine and Docker compose yet, please reference the links below.
- For Mac users: https://docs.docker.com/docker-for-mac/install/
- For Windows users: https://docs.docker.com/docker-for-windows/install/
- For Ubuntu users: https://docs.docker.com/engine/install/ubuntu/
- For CentOS users: https://shuhei-blog.work/tools-software/install-docker-centos7/
If you’re using or trying to install Docker Desktop, both Docker engine and Docker compose are installed at the same time(Docker Desktop is an option only for Windows or Mac users). However, if you’re a Linux user, you need to install both Docker engine and Docker compose separately, so please make sure you have both.
Step1: make a working directory for the Drupal site
Now, we’re sure that we have the Docker environment, so let’s start Drupal installation! First of all, let’s make a working directory for this Drupal project. This is not mandatory, but this makes the management easier. Below are the instructions for Linux or Mac users, so if you’re using Windows, please make a new directory wherever you like, and go to the directory.
- Make a new directory(This directory will be the working directory)
$ mkdir docker-drupal
2. Go into that directory
$ cd docker-drupal
Step2: make the “docker-compose.yml” file
Next, we will make a file called “docker-compose.yml”. To set up a series of containers using Docker Compose, we need to have a file
docker-compose.yml. So now, let’s make this one for our Drupal environment. Make a file “docker-compose.yml” and copy and paste the code below to the file.
- Make a file “docker-compose.yml”.
$ touch docker-compose.yml
2. Edit the file and copy and paste the code below to that file.
$ vim docker-compose.yml
Below is the code you need to copy and paste.
version: '3.3' services: drupal: # a web server that runs drupal image: drupal:latest ports: - 8080:80 restart: unless-stopped networks: - drupal_net volumes: - drupal_modules:/var/www/html/modules - drupal_profiles:/var/www/html/profiles - drupal_themes:/var/www/html/themes - drupal_sites:/var/www/html/sites mysql: # a mysql database server image: mysql:latest restart: unless-stopped networks: - drupal_net environment: MYSQL_DATABASE: drupal # the name of the database that will be used in Drupal MYSQL_USER: user # the name of the database user MYSQL_PASSWORD: password # the password for the database. You can change this and set any password as you like MYSQL_ROOT_PASSWORD: password # the password for the root user. You can also change this. volumes: - db_data:/var/lib/mysql - db_log:/var/log/mysql networks: drupal_net: volumes: drupal_modules: drupal_profiles: drupal_themes: drupal_sites: db_data: db_log:
Step3: Set up Drupal environment
Now we’re ready to use Docker compose and set up the Drupal environment. It’s really easy to do this. Just run the single command below.
$ docker-compose up -d
Now the Drupal site is ready, so visit the site and do some site configurations. Please visit “localhost:8080” from a web browser.
Then you’ll see a screen below.
Step4: Initial settings for Drupal site
Next, We need to do some site settings for this Drupal site.
First, choose the language of the Drupal site.
Next, choose the installation options. If you’re not familiar with these options, just choose “Standard”.
Next, we need to do database configurations. If you just copied the docker-compose.yml above and didn’t change anything, use the values below. If you have edited the docker-compose.yml and changed the password or user name, use your own value in the configuration fields.
- Database Type: “MySQL, MariaDB, Percona Server, or equivalent”
- Database name: “drupal”
- Database username: “user”
- Database password: “password”
- Host: “mysql”
- Port number: “3306”
Once the database configuration is done successfully, the installation begins. This won’t take so long, in my case, this took about 30 seconds.
At last, we need to do some general configurations for this Drupal site. Here, we can define the site name, user name, user password, etc…
Once the configuration is done, then you’ll see the Drupal site like below.
Congratulations! Now, we could successfully install Drupal. If you try to set up the Drupal environment manually, you need to install and configure the Web server, Database, PHP, and more. However, using Docker, we could set up really easily.
Appendix: how to control the Docker environment
Below is the list of commands that control the Drupal environment. Note that you need to be in the working directory(a directory that has the “docker-compose.yml” file in it) when you run those commands.
↓ Stop the Drupal site temporarily.
$ docker-compose stop
↓ Restart the Drupal site
$ docker-compose start
↓ Delete the Drupal site(This command will not delete the data of the site. So any data stored in the Drupal site won’t be deleted by this command.)
$ docker-compose down
↓ Launch the Drupal site
$ docker-compose up -d
- Using Docker compose, we can easily set up a Drupal environment.
- To use Docker compose, we need to have a file “docker-compose.yml”.
- We can stop, restart, delete and launch the Drupal environment using docker-compose commands.