Skip to content

Commit 50f902c

Browse files
committed
Add support for ./moodle-docker.env
1 parent 1a7e8c5 commit 50f902c

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,36 @@ to the instance you expect to. See
6161
[envvars](https://docs.docker.com/compose/reference/envvars/)
6262
to see more about `docker-compose` environment variables.
6363

64+
## Environment setting defaults via ./moodle-docker.env
65+
66+
In Linux and macOS it is also possible to created `moodle-docker.env` file
67+
in directory where `bin/moodle-docker-compose` is executed, the file contents are
68+
then used as default environment values.
69+
70+
If `moodle-docker.env` is in Moodle code root, then MOODLE_DOCKER_WWWROOT default
71+
is set to Moodle directory and COMPOSE_PROJECT_NAME defaults to Moodle directory name.
72+
73+
Example:
74+
75+
1. Create `/path/to/moodle/moodle-docker.env` file with the following content:
76+
```
77+
# file location /path/to/moodle/moodle-docker.env
78+
MOODLE_DOCKER_DB=pgsql
79+
```
80+
2. Copy config.docker-template.php file to `/path/to/moodle/config.php`
81+
3. Start containers:
82+
```bash
83+
cd /path/to/moodle
84+
/path/to/moodle-docker/bin/moodle-docker-compose up -d
85+
```
86+
4. Switch to MariaDB temporarily:
87+
```bash
88+
cd /path/to/moodle
89+
/path/to/moodle-docker/bin/moodle-docker-compose down
90+
export MOODLE_DOCKER_DB=mariadb
91+
/path/to/moodle-docker/bin/moodle-docker-compose up -d
92+
```
93+
6494
## Use containers for running behat tests
6595

6696
```bash

bin/include/env.sh

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
# If moodle-docker.env file found then use it as default environment values.
4+
filename="moodle-docker.env"
5+
if [ -f $filename ]; then
6+
envbackup=$( export -p)
7+
export $(grep -v '^#' $filename | xargs)
8+
eval "$envbackup"
9+
if [ -z "$MOODLE_DOCKER_WWWROOT" ] && [ -f 'lib/moodlelib.php' ];
10+
then
11+
# We know that moodle is in current directory, so use it as default value.
12+
currentdir="$( pwd -P )";
13+
export MOODLE_DOCKER_WWWROOT="$currentdir";
14+
fi
15+
if [ -z "$COMPOSE_PROJECT_NAME" ] && [ -f 'lib/moodlelib.php' ];
16+
then
17+
# Use moodle directory name as default Compose project name.
18+
name="$( basename "$( pwd -P )" )"
19+
export COMPOSE_PROJECT_NAME="$name";
20+
fi
21+
fi
22+
23+
if [ ! -d "$MOODLE_DOCKER_WWWROOT" ]; then
24+
echo 'Error: MOODLE_DOCKER_WWWROOT is not set or not an existing directory'
25+
exit 1
26+
fi
27+
28+
if [ -z "$MOODLE_DOCKER_DB" ];
29+
then
30+
echo 'Error: MOODLE_DOCKER_DB is not set'
31+
exit 1
32+
fi

bin/moodle-docker-compose

+2-11
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,8 @@ set -e
77
thisfile=$( readlink "${BASH_SOURCE[0]}" ) || thisfile="${BASH_SOURCE[0]}"
88
basedir="$( cd "$( dirname "$thisfile" )/../" && pwd -P )"
99

10-
if [ ! -d "$MOODLE_DOCKER_WWWROOT" ];
11-
then
12-
echo 'Error: $MOODLE_DOCKER_WWWROOT is not set or not an existing directory'
13-
exit 1
14-
fi
15-
16-
if [ -z "$MOODLE_DOCKER_DB" ];
17-
then
18-
echo 'Error: $MOODLE_DOCKER_DB is not set'
19-
exit 1
20-
fi
10+
# Load all environment settings.
11+
source "${basedir}/bin/include/env.sh"
2112

2213
export ASSETDIR="${basedir}/assets"
2314

bin/moodle-docker-wait-for-app

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ set -e
44
thisfile=$( readlink "${BASH_SOURCE[0]}" ) || thisfile="${BASH_SOURCE[0]}"
55
basedir="$( cd "$( dirname "$thisfile" )/../" && pwd -P )"
66

7+
# Load all environment settings.
8+
source "${basedir}/bin/include/env.sh"
9+
710
if [[ ! -z "$MOODLE_DOCKER_BROWSER" ]] && [[ "$MOODLE_DOCKER_BROWSER" == "chrome" ]] && ([[ ! -z "$MOODLE_DOCKER_APP_PATH" ]] || [[ ! -z "$MOODLE_DOCKER_APP_VERSION" ]] || [[ ! -z "$MOODLE_APP_VERSION" ]]);
811
then
912
until $basedir/bin/moodle-docker-compose logs moodleapp | grep -q -E 'dev server running: |Angular Live Development Server is listening|Configuration complete; ready for start up';

bin/moodle-docker-wait-for-db

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ set -e
44
thisfile=$( readlink "${BASH_SOURCE[0]}" ) || thisfile="${BASH_SOURCE[0]}"
55
basedir="$( cd "$( dirname "$thisfile" )/../" && pwd -P )"
66

7-
if [ -z "$MOODLE_DOCKER_DB" ];
8-
then
9-
echo 'Error: $MOODLE_DOCKER_DB is not set'
10-
exit 1
11-
fi
7+
# Load all environment settings.
8+
source "${basedir}/bin/include/env.sh"
129

1310
if [ "$MOODLE_DOCKER_DB" = "mssql" ];
1411
then

0 commit comments

Comments
 (0)