Skip to content

Commit 0b3dcfe

Browse files
authored
Merge pull request #66 from abias/localhost-only
Add env variables to control the ip address a port binds to
2 parents d509c8d + 1534244 commit 0b3dcfe

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ You can change the configuration of the docker images by setting various environ
132132
| `MOODLE_DOCKER_BROWSER` | no | firefox, chrome | firefox | The browser to run Behat against |
133133
| `MOODLE_DOCKER_PHPUNIT_EXTERNAL_SERVICES` | no | any value | not set | If set, dependencies for memcached, redis, solr, and openldap are added |
134134
| `MOODLE_DOCKER_WEB_HOST` | no | any valid hostname | localhost | The hostname for web |
135-
| `MOODLE_DOCKER_WEB_PORT` | no | any integer value | 8000 | The port number for web. If set to 0, no port is used |
136-
| `MOODLE_DOCKER_SELENIUM_VNC_PORT` | no | any integer value | not set | If set, the selenium node will expose a vnc session on the port specified |
135+
| `MOODLE_DOCKER_WEB_PORT` | no | any integer value (or bind_ip:integer)| 127.0.0.1:8000| The port number for web. If set to 0, no port is used.<br/>If you want to bind to any host IP different from the default 127.0.0.1, you can specify it with the bind_ip:port format (0.0.0.0 means bind to all) |
136+
| `MOODLE_DOCKER_SELENIUM_VNC_PORT` | no | any integer value (or bind_ip:integer)| not set | If set, the selenium node will expose a vnc session on the port specified. Similar to MOODLE_DOCKER_WEB_PORT, you can optionally define the host IP to bind to. If you just set the port, VNC binds to 127.0.0.1 |
137137

138138
## Advanced usage
139139

bin/moodle-docker-compose

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,16 @@ then
5050
dockercompose="${dockercompose} -f ${basedir}/selenium.${MOODLE_DOCKER_BROWSER}.yml"
5151
fi
5252

53-
if [[ -z "$MOODLE_DOCKER_SELENIUM_VNC_PORT" ]]
53+
# Selenium VNC port
54+
export MOODLE_DOCKER_SELENIUM_SUFFIX=""
55+
if [[ $MOODLE_DOCKER_SELENIUM_VNC_PORT == *":"* ]] || [[ $MOODLE_DOCKER_SELENIUM_VNC_PORT -gt 0 ]]
5456
then
55-
export MOODLE_DOCKER_SELENIUM_SUFFIX=""
56-
else
5757
export MOODLE_DOCKER_SELENIUM_SUFFIX="-debug"
58+
# If no bind ip has been configured (bind_ip:port), default to 127.0.0.1
59+
if [[ ! $MOODLE_DOCKER_SELENIUM_VNC_PORT == *":"* ]]
60+
then
61+
MOODLE_DOCKER_SELENIUM_VNC_PORT=127.0.0.1:$MOODLE_DOCKER_SELENIUM_VNC_PORT
62+
fi
5863
dockercompose="${dockercompose} -f ${basedir}/selenium.debug.yml"
5964
fi
6065

@@ -69,8 +74,13 @@ export MOODLE_DOCKER_WEB_HOST=${MOODLE_DOCKER_WEB_HOST:-localhost}
6974

7075
# Webserver port
7176
export MOODLE_DOCKER_WEB_PORT=${MOODLE_DOCKER_WEB_PORT:-8000}
72-
if [[ $MOODLE_DOCKER_WEB_PORT -gt 0 ]]
77+
if [[ $MOODLE_DOCKER_WEB_PORT == *":"* ]] || [[ $MOODLE_DOCKER_WEB_PORT -gt 0 ]]
7378
then
79+
# If no bind ip has been configured (bind_ip:port), default to 127.0.0.1
80+
if [[ ! $MOODLE_DOCKER_WEB_PORT == *":"* ]]
81+
then
82+
MOODLE_DOCKER_WEB_PORT=127.0.0.1:$MOODLE_DOCKER_WEB_PORT
83+
fi
7484
dockercompose="${dockercompose} -f ${basedir}/webserver.port.yml"
7585
fi
7686

bin/moodle-docker-compose.cmd

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,31 @@ IF "%MOODLE_DOCKER_WEB_PORT%"=="" (
5252
SET MOODLE_DOCKER_WEB_PORT=8000
5353
)
5454

55-
IF NOT "%MOODLE_DOCKER_WEB_PORT%"=="0" (
55+
SET "TRUE="
56+
IF NOT "%MOODLE_DOCKER_WEB_PORT%"=="%MOODLE_DOCKER_WEB_PORT::=%" SET TRUE=1
57+
IF NOT "%MOODLE_DOCKER_WEB_PORT%"=="0" SET TRUE=1
58+
IF DEFINED TRUE (
59+
REM If no bind ip has been configured (bind_ip:port), default to 127.0.0.1
60+
IF "%MOODLE_DOCKER_WEB_PORT%"=="%MOODLE_DOCKER_WEB_PORT::=%" (
61+
SET MOODLE_DOCKER_WEB_PORT=127.0.0.1:%MOODLE_DOCKER_WEB_PORT%
62+
)
5663
SET DOCKERCOMPOSE=%DOCKERCOMPOSE% -f "%BASEDIR%\webserver.port.yml"
5764
)
5865

5966
IF "%MOODLE_DOCKER_SELENIUM_VNC_PORT%"=="" (
6067
SET MOODLE_DOCKER_SELENIUM_SUFFIX=
6168
) ELSE (
62-
SET MOODLE_DOCKER_SELENIUM_SUFFIX=-debug
63-
SET DOCKERCOMPOSE=%DOCKERCOMPOSE% -f "%BASEDIR%\selenium.debug.yml"
69+
SET "TRUE="
70+
IF NOT "%MOODLE_DOCKER_SELENIUM_VNC_PORT%"=="%MOODLE_DOCKER_SELENIUM_VNC_PORT::=%" SET TRUE=1
71+
IF NOT "%MOODLE_DOCKER_SELENIUM_VNC_PORT%"=="0" SET TRUE=1
72+
IF DEFINED TRUE (
73+
SET MOODLE_DOCKER_SELENIUM_SUFFIX=-debug
74+
SET DOCKERCOMPOSE=%DOCKERCOMPOSE% -f "%BASEDIR%\selenium.debug.yml"
75+
REM If no bind ip has been configured (bind_ip:port), default to 127.0.0.1
76+
IF "%MOODLE_DOCKER_SELENIUM_VNC_PORT%"=="%MOODLE_DOCKER_SELENIUM_VNC_PORT::=%" (
77+
SET MOODLE_DOCKER_SELENIUM_VNC_PORT=127.0.0.1:%MOODLE_DOCKER_SELENIUM_VNC_PORT%
78+
)
79+
)
6480
)
6581

6682
%DOCKERCOMPOSE% %*

config.docker-template.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
$CFG->wwwroot = "http://{$host}";
2121
$port = getenv('MOODLE_DOCKER_WEB_PORT');
2222
if (!empty($port)) {
23-
$CFG->wwwroot .= ":{$port}";
23+
// Extract port in case the format is bind_ip:port.
24+
$parts = explode(':', $port);
25+
$port = end($parts);
26+
if ((string)(int)$port === (string)$port) { // Only if it's int value.
27+
$CFG->wwwroot .= ":{$port}";
28+
}
2429
}
2530
$CFG->dataroot = '/var/www/moodledata';
2631
$CFG->admin = 'admin';

0 commit comments

Comments
 (0)