Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhand's avatar
Level 6

Laravel Sail Multiple Databases

I am using this docker-compose file setup partly with Laravel Sail and adjusted by me. I set up one host per database so added two more once I set up the basic MySQL, Redis setup with the main application.:

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    app.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
    mysql_publish:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3307}:3307'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_PUBLISH_DATABASE}'
            MYSQL_USER: '${DB_PUBLISH_USERNAME}'
            MYSQL_PASSWORD: '${DB_PUBLISH_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql_publish'
    mysql_backup:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3308}:3308'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_BACKUP_PASSWORD}'
            MYSQL_DATABASE: '${DB_BACKUP_DATABASE}'
            MYSQL_USER: '${DB_BACKUP_USERNAME}'
            MYSQL_PASSWORD: '${DB_BACKUP_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql_backup'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
          retries: 3
          timeout: 5s
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "redis-cli", "ping"]
          retries: 3
          timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local

I changed the hosts for each MySQL database so I run three containers. I also changed the library per setup as without that it seemed to cause issues.

Had on and off

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed

errors or I could connect and had

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'backup.migrations' doesn't exist (SQL: select `migration` from `migrations` order by `batch` asc, `migration` asc)

as last one running sail artisan migrate --database=mysql_backup , which I do not quite comprehend either as we should have migration tables for backup database.

Questions I have are:

  1. What is the common way to set up volumes for multiple database and is mine okay?
  2. How do you normally set up ports for multiple database hosts and is mine correct?
0 likes
14 replies
bugsysha's avatar

I can only answer based on my personal preferences.

What is the common way to set up volumes for multiple database and is mine okay?

I usually point to different paths. Never tried to same. I guess if it works then you don't have anything to worry about. I just love the distinction between services. Explicit is always better in my mind.

How do you normally set up ports for multiple database hosts and is mine correct?

I don't. I usually just expose port 80 so I can access the web app through my browser during development. For everything else, I do it through the web app container or app.test in your case. That forces me into a specific workflow like I'm in production and at some point, it becomes muscle memory. With that approach when I have something to debug in production it feels like I'm faster.

1 like
rhand's avatar
Level 6

But how do you manage the database if you do not expose the ports? All from the command line?

Failed to access the databases using Sequel Pro locally so I am doing something wrong there as the ports are in use.. On production I normally do not allow outside access to the database either though. Just connect it to the web app and that is it. But locally an easier accessible setup is nice to have.

Two, any idea how I can have Sail set up multiple databases in one container instead of three like I do now?

bugsysha's avatar

But how do you manage the database if you do not expose the ports? All from the command line?

Yes.

Failed to access the databases using Sequel Pro locally so I am doing something wrong there as the ports are in use.

You don't need to change ports on the docker container. Those can remain default values, you just map them to different ports on your local machine. For example, you can leave 3306 on all your containers, but map them to 3306, 3307, ...

I assume you haven't changed the default port on the container side and trying to connect to the wrong port.

1 like
rhand's avatar
Level 6

And if I want to do a setup with two databases in one database container. How do I set that up in the docker-compose.yml? Now I feel like I need to add two containers to have two databases to have them automatically created. Surely we should be able to add two databases or more to one container on first firing of the yaml file?

frankielee's avatar
Level 29

@rhand You will need to create a SQL file and linked them in the docker-compose.yml

Example: Sql file: Create the database and allow the account has the permissions the control the account

CREATE DATABASE IF NOT EXISTS `test_database`;

GRANT ALL PRIVILEGES ON  `test_database`.* TO 'sail'@'%';

docker-compose: I put the file in the directory project/docker/8.0/, at the volume, I just linked the file.

    mysql:
        image: 'mysql:8.0'
    volumes:
            - './docker/8.0:/docker-entrypoint-initdb.d'
	........others configuration

3 likes
rhand's avatar
Level 6

@frankielee Another volumes section added to load the database data using docker-entry-point-initdb.d as you suggested like so:

mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
            - './docker/8.0:/docker-entrypoint-initdb.d'

could work. What the 8.0 is in docker/8.0 I am not clear on yet @frankielee . What is it for? Is it just a directory inside the directory .docker?

And then with init.sql:

# publication database
CREATE DATABASE IF NOT EXISTS `mysql_published`;
GRANT ALL PRIVILEGES ON  `mysql_published`.* TO 'sail'@'%';
# backup database
CREATE DATABASE IF NOT EXISTS `mysql_backup`;
GRANT ALL PRIVILEGES ON  `mysql_backup`.* TO 'sail'@'%';

I add the other two databases besides the main database added using the general first part of the mysql container. Will test this as soon as possible.

frankielee's avatar

@rhand It just a directory, it could be foo, bar or anything. Just make sure you have pointed to the correct directory.

https://hub.docker.com/_/mysql

it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d

1 like
frankielee's avatar

Btw, the folder is created after publishing the sail.

1 like
rhand's avatar
Level 6

using

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.2
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.2/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
            - mailpit
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - mysqladmin
                - ping
                - '-p${DB_PASSWORD}'
            retries: 3
            timeout: 5s
    mysql_published:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PUBLISH_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_PUBLISHED_DATABASE: '${DB_PUBLISH_DATABASE}'
            MYSQL_USER: '${DB_PUBLISH_USERNAME}'
            MYSQL_PASSWORD: '${DB_PUBLISH_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './docker/create-published-database.sh:/docker-entrypoint-initdb.d/10-create-published-database.sh'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - mysqladmin
                - ping
                - '-p${DB_PASSWORD}'
            retries: 3
            timeout: 5s
    mysql_backup:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_BACKUP_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_BACKUP_DATABASE: '${DB_BACKUP_DATABASE}'
            MYSQL_USER: '${DB_BACKUP_USERNAME}'
            MYSQL_PASSWORD: '${DB_BACKUP_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './docker/create-backup-database.sh:/docker-entrypoint-initdb.d/10-create-backup-database.sh'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - mysqladmin
                - ping
                - '-p${DB_PASSWORD}'
            retries: 3
            timeout: 5s
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sail-redis:/data'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - redis-cli
                - ping
            retries: 3
            timeout: 5s
    mailpit:
        image: 'axllent/mailpit:latest'
        ports:
            - '${FORWARD_MAILPIT_PORT:-1025}:1025'
            - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local
    sail-redis:
        driver: local

now and it did start, but I cannot connect to database from host using any of the ports 3307,3308, 3309 set.

I can enter a container and see the database however

sail mysql bash
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| performance_schema |
| dbname            |
| testing            |
+--------------------+
4 rows in set (0.00 sec)

The other two database containers are down however . They run into this error

2023-02-14 13:09:57 2023-02-14T06:09:57.151532Z 1 [ERROR] [MY-012574] [InnoDB] Unable to lock ./ibdata1 error: 11

perhaps volume related for some reason.

rhand's avatar
Level 6

Got all containers running with proper volumes now

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.2
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.2/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
            - mailpit
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - mysqladmin
                - ping
                - '-p${DB_PASSWORD}'
            retries: 3
            timeout: 5s
    mysql_publish:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PUBLISH_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_PUBLISHED_DATABASE: '${DB_PUBLISH_DATABASE}'
            MYSQL_USER: '${DB_PUBLISH_USERNAME}'
            MYSQL_PASSWORD: '${DB_PUBLISH_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql_publish'
            - './docker/create-published-database.sh:/docker-entrypoint-initdb.d/10-create-published-database.sh'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - mysqladmin
                - ping
                - '-p${DB_PASSWORD}'
            retries: 3
            timeout: 5s
    mysql_backup:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_BACKUP_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_BACKUP_DATABASE: '${DB_BACKUP_DATABASE}'
            MYSQL_USER: '${DB_BACKUP_USERNAME}'
            MYSQL_PASSWORD: '${DB_BACKUP_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql_backup'
            - './docker/create-backup-database.sh:/docker-entrypoint-initdb.d/10-create-backup-database.sh'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - mysqladmin
                - ping
                - '-p${DB_PASSWORD}'
            retries: 3
            timeout: 5s
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sail-redis:/data'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - redis-cli
                - ping
            retries: 3
            timeout: 5s
    mailpit:
        image: 'axllent/mailpit:latest'
        ports:
            - '${FORWARD_MAILPIT_PORT:-1025}:1025'
            - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local
    sail-redis:
        driver: local

I can connect to the database backup and others now from the host. But running a migration failed

sail artisan migrate --database=mysql_backup

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = backup and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +39 vendor frames 
  40  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()
rhand's avatar
Level 6

Perhaps I need to change DB_BACKUP_HOST=localhost host to mysql_backup in .env. Will test again later on.

rhand's avatar
Level 6

When I updated DB_HOST and gave it the same name as DB_CONNECTION I got location not found

php artisan migrate --database=mysql_backup                

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql_backup failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = backup and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +39 vendor frames 
  40  artisan:35
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

so localhost does seem to work and it does from the host.. but for a docker side command using sail it fails on no such file or directory using localhost and php_network_getaddresses: getaddrinfo for mysql_backup failed when using mysql_backup

Perhaps I need to use the container name like sitecom-mysql_backup-1?

rhand's avatar
Level 6

Doing

cat database/schema/mysql_backup-schema.dump | docker exec -i 992579bd9e4c /usr/bin/mysql -u root --password=password backup
mysql: [Warning] Using a password on the command line interface can be insecure.

gets the dump added to the separate container. But then I would still need to run seeders somehow..

Running basic artisan migrate still fails using

sail artisan migrate --database=mysql_backup

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = backup and table_name = migrations and table_type = 'BASE TABLE')
rhand's avatar
Level 6

Did

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' c7332b9fc167  

Then got the ip address and used it for the migrations

sail artisan migrate --database=mysql_backup                                                                  

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [1045] Access denied for user 'root'@'172.28.0.5' (using password: NO) (SQL: select * from information_schema.tables where table_schema = backup and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +36 vendor frames 
  37  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()
➜  ~/code/smart48.com git:(sail) ✗ sail artisan migrate --database=mysql_backup

   INFO  Preparing database.  

  Creating migration table ............................................................................................................... 21ms DONE

   INFO  Loading stored database schemas.  

  database/schema/mysql_backup-schema.dump .............................................................................................. 205ms DONE


   Illuminate\Database\QueryException 

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'backup.migrations' doesn't exist (SQL: select `migration` from `migrations` order by `batch` asc, `migration` asc)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

  i   A table was not found: You might have forgotten to run your database migrations. 
      https://laravel.com/docs/master/migrations#running-migrations

      +27 vendor frames 
  28  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

But you can also use the container name so

DB_PUBLISH_HOST=smart48com-mysql-1

which you can locate using docker ps

And to use sail mysql bash with ease and for all databases I decided to use one database container but just added three databases to the shell file to load

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.2
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.2/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
            - mailpit
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: '%'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './docker/all-databases.sh:/docker-entrypoint-initdb.d/10-create-backup-database.sh'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - mysqladmin
                - ping
                - '-p${DB_PASSWORD}'
            retries: 3
            timeout: 5s
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sail-redis:/data'
        networks:
            - sail
        healthcheck:
            test:
                - CMD
                - redis-cli
                - ping
            retries: 3
            timeout: 5s
    mailpit:
        image: 'axllent/mailpit:latest'
        ports:
            - '${FORWARD_MAILPIT_PORT:-1025}:1025'
            - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local
    sail-redis:
        driver: local

You can see - './docker/all-databases.sh:/docker-entrypoint-initdb.d/10-create-backup-database.sh' where I load all database bases in using

#!/usr/bin/env bash

mysql --user=root --password="$MYSQL_ROOT_PASSWORD" <<-EOSQL
    CREATE DATABASE IF NOT EXISTS testing;
    GRANT ALL PRIVILEGES ON \`testing%\`.* TO '$MYSQL_USER'@'%';
    CREATE DATABASE IF NOT EXISTS smart48_publish_db;
    GRANT ALL PRIVILEGES ON \`smart48_publish_db%\`.* TO '$MYSQL_USER'@'%';
    CREATE DATABASE IF NOT EXISTS backup;
    GRANT ALL PRIVILEGES ON \`backup%\`.* TO '$MYSQL_USER'@'%';
EOSQL

Please or to participate in this conversation.