wodby/mariadb

Container is created, but not always ready for connections

kevinquillen opened this issue · 1 comments

Original issue: wodby/docker4drupal#460

I was able to encounter the same issue on my Macbook Pro for the first time.

Here is my docker-compose.yml with the section for mariadb, using mariadb 10.5-3.9.0:

version: "3"

services:
  mariadb:
    image: wodby/mariadb:$MARIADB_TAG
    container_name: "${PROJECT_NAME}_mariadb"
    stop_grace_period: 30s
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
      MYSQL_DATABASE: $DB_NAME
      MYSQL_USER: $DB_USER
      MYSQL_PASSWORD: $DB_PASSWORD

Here is what I am doing. I have an ahoy file that aids onboarding of projects with two commands. setup will install composer etc, and finally call install-drupal to create a vanilla install.

commands:
  setup:
    cmd: |
      docker-compose pull
      docker-compose up -d --remove-orphans
      docker-compose exec php composer install
      ahoy generate-cert
      ahoy restart
      docker-compose exec php sh -c "cp /var/www/html/drush/sites/PROJECT_NAME.site.yml.example /var/www/html/drush/sites/${PROJECT_NAME}.site.yml"
      docker-compose exec php sh -c "sed -i 's/PROJECT_BASE_URL/$PROJECT_BASE_URL/g' /var/www/html/drush/sites/${PROJECT_NAME}.site.yml"
      ahoy install-drupal
    usage: Installs composer packages, generates local SSL certificate. Used to setup the project the first time.

  install-drupal:
    cmd: |
      if [ ! -f ./docroot/sites/$PROJECT_BASE_URL/settings.php ]; then
        ahoy drush si -y --db-url=mysql://drupal:drupal@mariadb/drupal --site-name=${PROJECT_NAME} -r="/var/www/html/docroot" --sites-subdir=$PROJECT_BASE_URL
      else
        echo "Settings file detected in sites/$PROJECT_BASE_URL, Drupal is already installed."
      fi
      ahoy drush @$PROJECT_NAME.local status
    usage: Installs Drupal to the local database.

What I have observed in that issue and today is that the MariaDB container isn't ready to connect to after starting up. The lag time seems to be anywhere from 5s to 30s before root or drupal user can connect from another container. On older machines with more limited resources, that time can be much longer. It makes sense now, but can be confusing at first when root connection is rejected, and then later accepted.

Following an example here, I added this:

  install-drupal:
    cmd: |
      while ! docker-compose exec mariadb mysqladmin --user=$DB_USER --password=$DB_PASSWORD --host "127.0.0.1" ping --silent &> /dev/null ; do
        echo "Waiting for database connection..."
        sleep 5
      done
      ....

This is able to wait until the database user (drupal) is able to connect. I was not able to replicate further after that. Unsure of what the action item would be here, but perhaps helpful for others who may run into this.

Nothing new here, as any daemon running in a container it takes time for a startup, we mention this in docs (Give it 10-20 seconds to initialize after the start), we have a check-ready action in the image that used in tests and should be used to determine whether mariadb has fully started up