0

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!!

4
  • 1
    In the Advanced options check for host name and if you have that option then set it as maria_service Commented Dec 13, 2017 at 14:34
  • Thank you that was the issue! I'll update my answer above with a screenshot to help others in the future! BTW I'm new with docker/docker-compose so learning a lot, thank you! Commented Dec 13, 2017 at 14:59
  • Would you know why it creates that db folder instead of putting the db stuff into my db_maria directory? Commented Dec 13, 2017 at 15:05
  • Thats because you used - ./db:/var/lib/mysql. And you should not use db_maria as it already has a Dockerfile. You should use a empty folder for creating the fresh DB Commented Dec 13, 2017 at 15:10

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks, the reason it kept creating that db folder is because I'm mounting that volume - ./db:/var/lib/mysql instead of - ./db_mariadb:/var/lib/mysql so that resolved that if anyone bumps into that. Lastly though, if I use that environment under the drupal_service, then what do I enter in the drupal UI advanced option, will that just auto populate the hostname on the UI for me and that's the advantage of explicitly setting it up in the environment? Let me try it and let u know!
That should auto complete the setup and that should be the way to do also. I didn't test this out but picked it from another project on github which used Drupal 7.x, but i guess it should work on Drupal 8.x
It worked beautifully, it bypassed that screen with the advanced section and automatically started installing Drupal; it's as if it just knew to pick up the environment from there, thank you; just one last question though! Since containers are immutable and won't persist data, I'm mounting my DB folders so they somehow persist no matter where I deploy this docker environment; does that mean that if I kill the container and relaunch a new instance of that image, it'll come up with the old data saved because of the volume mounting or it won't persist the db data?
Since you have the volume mounted, it will persist, You should not kill the containers but stop them. As killing could risk data loss/db corruption
To test that, I created two pages on my drupal site, then I just tested stopping the containers and launching them back up with docker-compose up and the drupal content was maintained. I still wanna kill the containers to test the theory that these are just isolated environment that are very easy to bring up and down and pass around through a CI/CD pipeline etc...
|

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.