1

I'm using Docker4Drupal docker-compose set-up to run a Drupal 7 install from a previous codebase.

This is the docker compose file:

version: "2"

services:
  mariadb:
    image: wodby/mariadb:10.1-2.3.5
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: drupal
      MYSQL_USER: drupal
      MYSQL_PASSWORD: drupal
    volumes:
      - ./mariadb-init:/docker-entrypoint-initdb.d # Place init .sql file(s) here.
#      - /path/to/mariadb/data/on/host:/var/lib/mysql # I want to manage volumes manually.

  php:
# 2. Images without Drupal – wodby/drupal-php:[PHP_VERSION]-[STABILITY_TAG].
    image: wodby/drupal-php:5.6-3.0.0
    environment:
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      PHP_FPM_CLEAR_ENV: "no"
      DB_HOST: mariadb
      DB_USER: drupal
      DB_PASSWORD: drupal
      DB_NAME: drupal
      DB_DRIVER: mysql
    volumes:
       - docker-sync:/var/www/html # Docker-sync
## For Xdebug profiler files
#      - files:/mnt/files

  nginx:
# wodby/drupal-nginx:[DRUPAL_VERSION]-[NGINX_VERSION]-[STABILITY_TAG].
    image: wodby/drupal-nginx:7-1.13-3.0.1
    depends_on:
      - php
    environment:
      NGINX_STATIC_CONTENT_OPEN_FILE_CACHE: "off"
      NGINX_ERROR_LOG_LEVEL: debug
      NGINX_BACKEND_HOST: php
      NGINX_SERVER_ROOT: /var/www/html
    volumes:
        - docker-sync:/var/www/html # Docker-sync
    labels:
      - 'traefik.backend=nginx'
      - 'traefik.port=80'
      - 'traefik.frontend.rule=Host:drupal.docker.localhost'

  mailhog:
    image: mailhog/mailhog
    labels:
      - 'traefik.backend=mailhog'
      - 'traefik.port=8025'
      - 'traefik.frontend.rule=Host:mailhog.drupal.docker.localhost'

  portainer:
    image: portainer/portainer
    command: --no-auth -H unix:///var/run/docker.sock
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      - 'traefik.backend=portainer'
      - 'traefik.port=9000'
      - 'traefik.frontend.rule=Host:portainer.drupal.docker.localhost'

  traefik:
    image: traefik
    command: -c /dev/null --web --docker --logLevel=INFO
    ports:
      - '8000:80'
      - '8080:8080' # Dashboard
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

volumes:
  docker-sync:
    external: true
## For Xdebug profiler
#  files:

Docker-sync is running and the codebase in the container is consistent with the host.

Getting the following error in Drupal:

PDOException: SQLSTATE[HY000] [2002] No such file or directory in lock_may_be_available() (line 167 of /var/www/html/includes/lock.inc).

So I added some debug code to the index file which gives this output:

PDOException Object
(
    [message:protected] => SQLSTATE[HY000] [2002] Connection refused
    [string:Exception:private] => 
    [code:protected] => 2002
    [file:protected] => /var/www/html/index.php
    [line:protected] => 24
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => /var/www/html/index.php
                    [line] => 24
                    [function] => __construct
                    [class] => PDO
                    [type] => ->
                    [args] => Array
                        (
                            [0] => mysql:host=localhost:3306;dbname=drupal
                            [1] => drupal
                            [2] => drupal
                            [3] => Array
                                (
                                    [3] => 2
                                )

                        )

                )

        )

    [previous:Exception:private] => 
    [errorInfo] => 
)

But there's not trouble when I connect on the command line:

docker exec -ti plantation_mariadb_1 mysql -udrupal -p --host=localhost
MariaDB [(none)]>

Also ls /var/log/ is empty in the mariadb container.

Where do I poke next?

UPDATE:

A linked question suggested that the host should be the name of the MySQL container, in the above docker-compose.yml this is mariadb. The following two connections are successful:

# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'drupal';
$dbuser = 'drupal';
$dbpass = 'drupal';
$dbhost = 'mariadb';
$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);
$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {
  $tblCnt++;
  #echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
  echo "There are no tables<br />\n";
} else {
  echo "There are $tblCnt tables<br />\n";
}

try{
    $dbh = new pdo( 'mysql:host=mariadb:3306;dbname=drupal',
                    'drupal',
                    'drupal',
                    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    echo "<pre>";
    print_r($dbh);
    echo "</pre>";
    //die(json_encode(array('outcome' => true)));
}
catch(PDOException $ex){
    echo "<pre>";
    print_r($ex);
    echo "</pre>";
    die(json_encode(array('outcome' => false, 'message' => 'Unable to connect')));
}

But the Drupal connection, as far as I can tell, still isn't working.

FURTHER DEBUGGING:

This error occurs in the Drupal bootstrap process before the database credentials are even registered. The function referenced in the error message is in includes/lock.inc and I added print_r(debug_backtrace()) shows the stack:

  • lock_may_be_available
  • lock_acquire
  • variable_initialize
  • _drupal_bootstrap_variables
  • drupal_bootstrap
  • _drupal_bootstrap_page_cache
  • drupal_bootstrap

With variable_init being the parameter sent to lock_may_be_available.

I am trying to track down what the SQL string of

db_query('SELECT expire, value FROM {semaphore} WHERE name = :name', array(':name' => $name))->fetchAssoc();

might be so I can run it manually in the MariaDB image.

1 Answer 1

1

I saw the same problem (although with Lando rather than Docker4Drupal) - I was able to resolve it by deleting settings.php and running a fresh install.

Sign up to request clarification or add additional context in comments.

1 Comment

Lando has recipes for various applications, including Drupal 6, 7, 8 and 9: docs.lando.dev/config/recipes.html#supported-recipes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.