Setting up a local Drupal environment with docker-compose

At times for debugging and troubleshooting purposes, we can use the following steps to simulate a local Drupal environment that is “close” to the production environment

  1. Install docker on your machine following the instructions in here for the specific operating system
    • Validate docker is installed by running docker --version
  2. Install docker-compose on your machine following the instructions in here for the specific operating system
    • Validate docker-compose is installed by running docker-compose --version
  3. Clone the following git repo
    git clone --depth=1 https://github.com/mogtofu33/docker-compose-drupal
  4. Change directory to the cloned repo with cd docker-compose-drupal
  5. Create the .env file from template
    cp default.env .env
  6. Create a new docker-compose.yml file and copy the following contents in it
################################################################################
# Docker Compose Drupal 8 full dev stack.
#
# Project page:
#   https://github.com/Mogtofu33/docker-compose-drupal
# Documentation:
#   https://github.com/Mogtofu33/docker-compose-drupal/blob/master/README.md
################################################################################
version: '3'
services:
  nginx:
    image: nginx:alpine
    depends_on:
      - php
    ports:
      - "${NGINX_HOST_HTTP_PORT:-81}:80"
      # - "${NGINX_HOST_HTTPS_PORT:-444}:443"
    volumes:
      - ${HOST_WEB_ROOT:-./drupal}:/var/www/localhost
      - ./config/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      # Php FPM socket
      - php-sock:/sock:ro
    env_file: .env
    restart: unless-stopped
    container_name: ${PROJECT_NAME:-dcd}-nginx
  php:
    image: mogtofu33/php:${PHP_VERSION:-7.3}
    volumes:
      ## If you have composer set locally, share cache to speed up.
      # - ${HOME:-}/.composer/cache:/var/www/.composer/cache
      - ${HOST_WEB_ROOT:-./drupal}:/var/www/localhost
      - ./config/php/${PHP_VERSION:-7.3}/php.ini:/etc/php7/php.ini:ro
      - ./config/php/${PHP_VERSION:-7.3}/php-fpm.conf:/etc/php7/php-fpm.conf:ro
      - ./config/php/${PHP_VERSION:-7.3}/conf.d/:/etc/php7/conf.d/:ro
      - ./config/php/${PHP_VERSION:-7.3}/php-fpm.d/:/etc/php7/php-fpm.d/:ro
      # Drush 9 config file.
      - ./config/drush/drush.yml:/etc/drush/drush.yml:ro
      # Used by dashboard for accessing tools.
      - ./tools:/tools:ro
      # Share db dump folder.
      - ${HOST_DATABASE_DUMP:-./database/dump}:/dump
      # Php FPM socket
      - php-sock:/sock
      ## If PgSQL, ease drush pgsql access.
      - ./config/pgsql/.pg_pass:/home/apache/.pg_pass:ro
    links:
      # Choose database, uncomment service concerned below.
      - mysql
      # - varnish
    env_file: .env
    restart: unless-stopped
    container_name: ${PROJECT_NAME:-dcd}-php
  mysql:
    image: mariadb:latest
    expose:
      - "3306"
    volumes:
      - ${HOST_DATABASE_DUMP:-./database/dump}:/dump
      # All .sql files will be imported on first start by order.
      - ./database/mysql-init:/docker-entrypoint-initdb.d
      - ./config/mysql:/etc/mysql:ro
    env_file: .env
    restart: unless-stopped
    container_name: ${PROJECT_NAME:-dcd}-mysql
  
  ## Full advanced dashboard.
  # portainer:
  #   image: portainer/portainer:latest
  #   ports:
  #     - "9000:9000"
  #   command: --no-auth -H unix:///var/run/docker.sock
  #   volumes:
  #     - /var/run/docker.sock:/var/run/docker.sock
  #   restart: unless-stopped
  #   container_name: ${PROJECT_NAME:-dcd}-portainer

volumes:
  php-sock:

  1. In the curent directoy, create a symlink to your “drupal” directory (the directory in which the drupal files are i.e web, composer.json, scripts, etc)
    ln -s <path to your drupal directory> ./drupal from inside the current directory
  2. To spin up the servers, run docker-compose up --build -d. The drupal site should be accessible on http://localhost:81
  3. To run composer or drush commands exec into the php container using docker exec -it -w /var/www/localhost -u apache dcd-php bash
  4. To stop the servers, run docker-compose down
  5. Refer to GitHub - mogtofu33/docker-compose-drupal: Drupal 8 Docker Compose dev stack. for advanced configurations or for code reference