How to keep a persistant database connection with Drupal

I am running Docker version 20.10.12, build e91ed57

I would like my Drupal development environment to stay intact even if I run a docker compose down, and then up again. I am able to get my local project up and running successfully. But when I run docker compose down, I have to re-install everything on the next docker compose up.

My yml setup specifically for my Drupal project

version: '3.9'

services:
  # db running on network!
  drupal:
    image: drupal:8-apache
    ports:
      - '17223:80'
    volumes:
      - ./sb01/sites:/var/www/html/sites'
      - ./sb01/mods:/var/www/html/modules'
      - ./sb01/prof:/var/www/html/profiles'
      - ./sb01/themes:/var/www/html/themes'
      - ./php/php.ini:/usr/local/etc/php/conf.d/my.ini
    depends_on:
      - mysql_data
    restart: always
    networks:
     - wpsite
networks:
  wpsite:
    external: true

My network setup for all database connections

version: '3.9'
services:
  db:
    image: 'mysql:5.7'
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: MYSQL_ROOT_PASSWORD
    volumes:
      - 'mysql_data:/var/lib/mysql'
    networks:
      - phpmyadmin_net
  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
      - 8080:80
    environment:
      PMA_HOST: mysql
      MYSQL_ROOT_PASSWORD: password
    depends_on:
      - mysql_data
networks:
  phpmyadmin_net:
    external: true
volumes:
  mysql_data:

How can I improve my setup this way Drupal has a persistent database connection? Is there anything else that seems wrong or unneeded from my setup? Many thanks on helping me sharpening my docker compose skills!

Hi kensleylewis,

You can check out this link Docker-Compose persistent data MySQL - Stack Overflow

Best Regards,
Fousseyni B.

Thanks fouscou. There must be something else missing because as you can can with my code, I am applying a “volumes” location under my db service.

1 Like

The database connection cannot be persistent to survive docker-compose down. You actually want persistent data and not persistent connection. So @fouscou hint was good. Yes, I can see that you use volumes, but you have to make sure that you also have the right volumes for Drupal 8 and MySql too. MySQL’s volumes are probably right, so it should work.

  1. Check if the database still have the data after docker-compose down
  2. Check if every file which needs to be persistent is on volume. You can use docker container diff <containername> before and after
    docker-compose down
    docker compose up -d
    
  3. Consider removing “mysql_data” from the depends_on list of drupal. I can’t see any service in thet file called “mysql_data”. I tried, and the compose file works even with a non-existent dependency, but it may have some side effects.
  4. An immportant question is whether you run docker-compose down only on drupal or MySQL too. None of them should be a problem, but knowing that can bring us closer to the solution.

One more small advice. If you work with Docker, you need to learn debugging and prepare yourself to investiagete situation like this :slight_smile: . It means you check the existence and correctness of every configuration file, all data including the database records and the complete state of the container before and after stopping and starting the container again. If we can’t help you, maybe the Drupal Community can tell you what is missing, if that is not explainable without knowing Drupal 8 and its files better.

1 Like

Could I add that there are easier paths to Docker for Drupal developers. In particular Lando at lando.dev and ddev. Please see the Drupal documentation here if you haven’t already Docker solutions overview | Docker development environments | Drupal Wiki guide on Drupal.org

1 Like

Friends, many thanks on the good learning points here! I feel this is all good for me soak and journey through. Much appreciated on the support from everyone!