pelican
pelican

Reputation: 6224

docker-compose drupal not accepting my database name and throwing error below

I have the following docker-compose.yml file:

version: '3'

services:
  maria_service:
    build: ./db_maria
    restart: always
    environment:
      MYSQL_DATABASE: mariadb
      MYSQL_USER: joel
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - ./db:/var/lib/mysql

  drupal_service:
    build: ./website
    restart: always
    ports:
      - 8080:80
    volumes:
      - /var/www/html/modules
      - /var/www/html/profiles
      - /var/www/html/themes
      # this takes advantage of the feature in Docker that a new anonymous
      # volume (which is what we're creating here) will be initialized with the
      # existing content of the image at the same location
      - /var/www/html/sites
    depends_on:
      - maria_service

Here's my working directory:

enter image description here

Here's the drupal dockerfile where all I'm doing is to pull the drupal image:

  1. enter image description here

Here's the mariadb dockerfile:

enter image description here

It automatically generate this "db" subfolder seen in the pic below:

enter image description here

My issue is everytime I enter mariadb on the drupal UI at localhost:8080, it throws this error below: enter image description here

UPDATES:

Based on @Tarun Lalwani answer, my issue was that, in the Drupal UI, I would enter my username, password and db name but if you expand that Advanced Options in that Drupal screenshot, you'll see that the HOSTNAME was pointing to "localhost" when it should be pointing to the actual hostname of the mariadb database server which in DOCKER WORLD, the hostname name of a running container is ITS SERVICE NAME i.e "mariadb_service" as seen in the docker-compose.yml file - see screenshot. Hope I wasn't the only newbie that bumped into that and will help others, thanks Tarun Lalwani!!

Upvotes: 0

Views: 823

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

You need to set the Host name also for the DB in Drupal. This db host will be maria_service as per the service name from your docker-compose.yml file. This needs to be done by expanding the Advanced options

Advanced config

Using Environment Variables

You could also try setting the environment variables for these settings

version: '3'

services:
  maria_service:
    build: ./db_maria
    restart: always
    environment:
      MYSQL_DATABASE: mariadb
      MYSQL_USER: joel
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - ./db:/var/lib/mysql

  drupal_service:
    build: ./website
    restart: always
    ports:
      - 8080:80
    volumes:
      - /var/www/html/modules
      - /var/www/html/profiles
      - /var/www/html/themes
      # this takes advantage of the feature in Docker that a new anonymous
      # volume (which is what we're creating here) will be initialized with the
      # existing content of the image at the same location
      - /var/www/html/sites
    depends_on:
      - maria_service
    environment:
      DB_HOST: maria_service
      DB_USER: joel
      DB_PASSWORD: password
      DB_NAME: mariadb
      DB_DRIVER: mysql

Upvotes: 5

Related Questions