0

I am trying to develop an automatic Drupal installer which creates an already configured Drupal docker container ready to be used with one single command execution.

In order to achieve this, I have this docker-compose.yml:

version: "3"

services:

  # Database
  drupal_db:
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    container_name: drupal_db
    ports:
      - "33061:3306"
    restart: unless-stopped
    volumes:
      - drupal_db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: drupal
      MYSQL_DATABASE: drupal
      MYSQL_USER: drupal
      MYSQL_PASSWORD: drupal
    networks:
      - drupal

  # Drupal
  drupal:
    image: drupal:9-php7.4-apache
    container_name: drupal
    ports:
      - "8080:80"
    restart: unless-stopped
    volumes:
      - ./drupal/d_modules:/var/www/html/modules
      - ./drupal/d_profiles:/var/www/html/profiles
      - ./drupal/d_sites:/var/www/html/sites
      - ./drupal/d_sites/default/files/translations:/var/www/html/sites/default/files/translations
      - ./drupal/d_themes:/var/www/html/themes
      - ./scripts/drush:/opt/drupal/scripts
    depends_on:
      - drupal_db
    env_file:
      - drupal-install.env
    links:
      - drupal_db:mysql
    networks:
      - drupal

volumes:
  drupal_db_data: {}
  drupal_data: {}

networks:
  drupal:

Together with this Makefile:

clear:
    docker-compose down -v

autoinstall:
    docker-compose up -d
    docker exec drupal composer require drush/drush
    docker exec drupal bash -c '/opt/drupal/scripts/autoinstall.sh'

autoinstall.sh is an script which is mounted via one of Drupal's container volumes, which runs this:

#!/bin/bash

drush site-install ${DRUPAL_PROFILE} \
    --locale=${LOCALE} \
    --db-url=${DB_URL} \
    --site-name=${SITE_NAME} \
    --site-mail=${SITE_MAIL} \
    --account-name=${ACCOUNT_NAME} \
    --account-mail=${ACCOUNT_MAIL} \
    --account-pass=${ACCOUNT_PASS} \
    --yes

This uses environment variables, which are specified at docker-compose.yml through the env-file drupal-install.env:

HOST=drupal_db:33061
DBASE=drupal
USER=drupal
PASS=drupal
DATABASE_HOST=drupal_db:33061
DRUPAL_PROFILE=standard
LOCALE=en
DB_URL=mysql://drupal:drupal@drupal_db:3306/drupal
SITE_NAME=NewSite
SITE_MAIL=newsite@test.com
ACCOUNT_NAME=admin
ACCOUNT_MAIL=admin@test.com
ACCOUNT_PASS=admin

However, when running the make autoinstall command, first two lines run with no issues, but the last one throws this error:

Database settings:<br /><br />Resolve all issues below to continue the installation. For help  
   configuring your database server, see the <a href="https://www.drupal.org/docs/8/install">in  
  stallation handbook</a>, or contact your hosting provider.<div class="item-list"><ul><li>Fail  
  ed to connect to your database server. The server reports the following message: <em class="p  
  laceholder">SQLSTATE[HY000] [2002] Connection refused</em>.<ul><li>Is the database server run  
  ning?</li><li>Does the database exist or does the database user have sufficient privileges to  
   create the database?</li><li>Have you entered the correct database name?</li><li>Have you en  
  tered the correct username and password?</li><li>Have you entered the correct database hostna  
  me and port number?</li></ul></li></ul></div>    

If I manually run:

docker-compose up -d
docker exec -it drupal bash
composer require drush/drush
/opt/drupal/scripts/autoinstall.sh

Everything works perfectly, but the makefile script doesn't work.

Something really weird happens because if I run make autoinstall twice, the first time will throw this error, but it actually works when I run it a second time. It is really strange and I can't find a solution, but I would like to not to have to run the command twice.

pabpazjim
  • 13
  • 2
  • If you're trying to make this run with a single command, see if you can make it be `docker-compose up -d`. You shouldn't normally need `docker{,-compose} exec` to modify running containers: any changes you make this way will get lost as soon as the container exits, and you'll need to repeat those manual installation steps every time you re-run the container. – David Maze Nov 07 '22 at 14:05

0 Answers0