From 95b584c07f6fcede76e17ba85d78a835c2aaa03c Mon Sep 17 00:00:00 2001 From: Matteo Scaramuccia Date: Fri, 11 Nov 2022 22:25:00 +0100 Subject: [PATCH] Support a connection to an external DB --- README.md | 27 +++++++++++++++++++++++++++ base.yml | 1 + config.docker-template.php | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1f50cf9e29a..17afda0c753 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,33 @@ When you change them, use `bin/moodle-docker-compose down && bin/moodle-docker-c | `MOODLE_DOCKER_APP_VERSION` | no | a valid [app docker image version](https://docs.moodle.org/dev/Moodle_App_Docker_images) | not set | If set will start an instance of the Moodle app if the chrome browser is selected | | `MOODLE_DOCKER_APP_RUNTIME` | no | 'ionic3' or 'ionic5' | not set | Set this to indicate the runtime being used in the Moodle app. In most cases, this can be ignored because the runtime is guessed automatically (except on Windows using the `.cmd` binary). In case you need to set it manually and you're not sure which one it is, versions 3.9.5 and later should be using Ionic 5. | +## Use an external database + +You can use an external database by setting `MOODLE_DOCKER_DB*` internal environment variables. + +For example to connect to an external DB server hosted in Azure: + +``` +# Configure coordinates and credentials for the external DB instance. +export MOODLE_DOCKER_DBHOST=instance-name.mysql.database.azure.com +export MOODLE_DOCKER_DBNAME=moodle-external-db +export MOODLE_DOCKER_DBUSER=instance-username +export MOODLE_DOCKER_DBPASS="1nst@nc3-p@ssw0rd" +# Start the docker environment. +bin/moodle-docker-compose up -d +... +# Stop the docker environemnt. +bin/moodle-docker-compose down +# Unset the external DB instance refs. +unset MOODLE_DOCKER_DBHOST MOODLE_DOCKER_DBNAME MOODLE_DOCKER_DBUSER MOODLE_DOCKER_DBPASS +``` + +Please note that the local DB server will be still available to allow e.g. the usage of the specific DB client to connect to the remote DB server: + +``` +bin/moodle-docker-compose exec db mysql -h instance-name.mysql.database.azure.com -u ******** -p'********' -e "SELECT VERSION();" +``` + ## Using XDebug for live debugging The XDebug PHP Extension is not included in this setup and there are reasons not to include it by default. diff --git a/base.yml b/base.yml index f736b8fe7e5..ab2962dea24 100644 --- a/base.yml +++ b/base.yml @@ -8,6 +8,7 @@ services: - "${MOODLE_DOCKER_WWWROOT}:/var/www/html" - "${ASSETDIR}/web/apache2_faildumps.conf:/etc/apache2/conf-enabled/apache2_faildumps.conf" environment: + MOODLE_DOCKER_DBHOST: db MOODLE_DOCKER_DBNAME: moodle MOODLE_DOCKER_DBUSER: moodle MOODLE_DOCKER_DBPASS: "m@0dl3ing" diff --git a/config.docker-template.php b/config.docker-template.php index fb8597a3d9a..0945296ad55 100644 --- a/config.docker-template.php +++ b/config.docker-template.php @@ -6,7 +6,7 @@ $CFG->dbtype = getenv('MOODLE_DOCKER_DBTYPE'); $CFG->dblibrary = 'native'; -$CFG->dbhost = 'db'; +$CFG->dbhost = getenv('MOODLE_DOCKER_DBHOST'); $CFG->dbname = getenv('MOODLE_DOCKER_DBNAME'); $CFG->dbuser = getenv('MOODLE_DOCKER_DBUSER'); $CFG->dbpass = getenv('MOODLE_DOCKER_DBPASS');