How to set-up a Drupal or WordPress site locally using Docker

This article is not aiming to provide explanations about the tools that I use to set-up Drupal/WordPress sites, I think there is a lot of information available on the internet about them Docker, Traefik, Compose. The only goal of this post is to document the process that I follow since it is really easy, and hoping that it can help other people too.

Prerequisites: Before you begin, you need:

  1. Docker installed.
  2. Basic knowledge of git.
  3. To be familiar with the terminal.
  4. An existing repo/codebase of Drupal/WordPress. If you only want to try a fresh install, please follow the official doc of the vanilla installs from wodby.com, they are easier than this. The URLs are: Drupal or WordPress

The steps for Drupal 7/8/9:

  1. Create a folder where you want to keep your project’s files. The folder name is important since the network will be created based on this. Choose a short and just a word name, if possible. The name will be used for the URL too. For purpose of this tutorial, I will create a folder named d8base.
  2. Clone the Docker4Drupal repository in the folder that you created in step 1. You can run this command from the terminal to do so: git clone [email protected]:wodby/docker4drupal.git .
  3. Create a new folder and name it web, inside the folder that you created in step 1. In the docker-compose file, the Nginx by default is configured to use the web folder as the server root. We are cloning our Drupal codebase in this folder too.
  4. Remove the docker-compose.override.yml file and your files structure should look like this: Alt Text
  5. files in your preferred IDE and edit the .env file. The lines that we need to update are lines 7 and 8. As project name, we should use the same one that we used for the folder that we created in step 1 (in my case d8base). As the project’s URL, I use the name of the folder created in step one as the prefix and I keep the docker.localhost part. This is just my preference.
PROJECT_NAME=d8base
PROJECT_BASE_URL=d8base.docker.localhost
  1. Edit the docker-compose.yml file, and make the following updates: *Comment out all the lines related to traefik since we will create a global traefik.yml file later.
  2. Alt Text
    *Uncomment the lines related to PhpMyAdmin (pma) so we have it available to import our database:Alt Text
  3. In the web folder, created as part of step 3, clone the repo of your Drupal codebase. Please make sure that the Drupal code is in this folder itself instead of a sub-folder
  4. In the terminal, inside the folder created in step 1, run the following command to start the services:
make up

Configure traefik

  1. Create a new folder, I recommend to name it traefik, an inside of it create a traefik.yml file, and the content of the file should be this:
version: '3'
services:
  traefik:
    image: traefik:v2.0
    command: --api.insecure=true --providers.docker
    networks:
      - d8base
    ports:
      - '80:80'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
networks:
  d8base:
    external:
      name: d8base_default

If you have more projects that you need to set up, you just need to add them to the traefik.yml, similar to this:
Alt Text

  1. In the terminal, run the following command in the traefik folder:

docker-compose -f traefik.yml up -d

The steps for WordPress

  1. The process is the same as the one we followed for Drupal, the only difference comes in step 2, where we have to clone de Docker4Wordpress instead.

IMPORTANT: Make sure this line is not commented: NGINX_SERVER_ROOT: /var/www/html/web

Notes:

  1. You can import the database from http://pma.d8base.docker.localhost/ replace d8base with the name of the folder created in step 1
  2. Your site should be accessible from http://d8base.docker.localhost/ replace d8base with the name of the folder created in step 1
  3. Make sure that port 80 of your machine is not being used by any other application/site before following this tutorial.
  4. Drupal: If you need to run drush, composer or drupal console commands run this command docker exec -it d8base_php /bin/bash and cd web replace d8base with the name of the folder created in step 1
  5. If you need to downgrade the version of the composer to version 1 run this: composer self-update --1
  6. You may need to edit the /etc/hosts file and add an entry like this: 127.0.0.1 d8base.docker.localhost
  7. IMPORTANT: Each time that docker is restarted you need to run docker-compose -f traefik.yml up -d from the traefik folder.
  8. Drupal: Make sure that the database information is correct in your settings.php file. The .env file has this information, and the default ones should look like this:
$databases['default']['default'] = array (
  'database' => 'drupal',
  'username' => 'drupal',
  'password' => 'drupal',
  'prefix' => '',
  'host' => 'mariadb',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

Posted

in

by