From 4cf823709afe7bb77d862504420a586186fc576d Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 15 Mar 2023 09:34:50 +0800 Subject: [PATCH] Add support for entrypoint scripts This commit adds support for run-time configuration which is executed as part of the startup of the container. Two options are supported: * shell scripts; and * .ini files for PHP configuration. These can be placed into a new directory, located at /docker-entrypoint-initdb.d and files are executed in lexical order returned by a bash glob. --- Dockerfile | 3 ++ README.md | 27 ++++++++++++++- .../local/bin/moodle-docker-php-entrypoint | 34 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100755 root/usr/local/bin/moodle-docker-php-entrypoint diff --git a/Dockerfile b/Dockerfile index ad3d20a..693011d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,3 +38,6 @@ RUN mkdir /var/www/moodledata && chown www-data /var/www/moodledata && \ mkdir /var/www/phpunitdata && chown www-data /var/www/phpunitdata && \ mkdir /var/www/behatdata && chown www-data /var/www/behatdata && \ mkdir /var/www/behatfaildumps && chown www-data /var/www/behatfaildumps + +CMD ["apache2-foreground"] +ENTRYPOINT ["moodle-docker-php-entrypoint"] diff --git a/README.md b/README.md index f0e986b..4a6231e 100644 --- a/README.md +++ b/README.md @@ -39,15 +39,40 @@ $ docker run --name web0 -p 8080:80 -v $PWD:/var/www/html moodlehq/moodle-php-a * For PHP 7.3 and up, both `linux/amd64` and `linux/arm64` images are being built. Note that `linux/arm64` doesn't support the sqlsrv and oci extensions yet. Other than that, both architectures work exactly the same. * Verified by [automated tests](https://travis-ci.com/moodlehq/moodle-php-apache). * Autobuilt from GHA, on push. +* Support for entrypoint scripts and PHP Configuration ## Directories -To faciliate testing and easy setup the following directories are created and owned by www-data by default: +To facilitate testing and easy setup the following directories are created and owned by www-data by default: * `/var/www/moodledata` * `/var/www/phpunitdata` * `/var/www/behatdata` * `/var/www/behatfaildumps` +## Initialisation scripts + +If you would like to do additional initialization, you can add one or more `*.sh`, or `*.ini` scripts under `/docker-entrypoint-initdb.d` (creating the directory if necessary). When the entrypoint script is called, it will run any executable `*.sh` script, source any non-executable `*.sh` scripts found in that directory, and will copy any `*.ini` scripts into the PHP Configuration directory (`/usr/local/etc/php/conf.d`). + +For example, to configure PHP to support a higher `upload_max_filesize` option you might add the following to a `config/10-uploads.ini` file: + +``` +; Specify a max filesize of 200M for uploads. +upload_max_filesize = 200M +post_max_size = 210M +``` + +When starting your container you could do so passing in the config directory: + +``` +docker run \ + --name web0 \ + -p 8080:80 \ + -v $PWD/moodle:/var/www/html + -v $PWD/config:/docker-entrypoint-initdb.d \ + moodle-php-apache:latest +``` + +These initialization files will be executed in sorted name order as defined by the current locale, which defaults to en_US.utf8. ## See also This container is part of a set of containers for Moodle development, see also: diff --git a/root/usr/local/bin/moodle-docker-php-entrypoint b/root/usr/local/bin/moodle-docker-php-entrypoint new file mode 100755 index 0000000..e22b756 --- /dev/null +++ b/root/usr/local/bin/moodle-docker-php-entrypoint @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +set -Eeo pipefail + +docker_process_init_files() { + echo + local f + for f; do + case "$f" in + *.sh) + # Note: This hack is required for MacOS because the exeute bit is not checked for bind mounts. + # The executable bit is stored, but the test -x flag does not return corretly. + # Copying the file to an alternate file system allows it to be respected. + cp "$f" /tmp/testscript + if [ -x "/tmp/testscript" ]; then + echo "$0: running $f" + "$f" + else + echo "$0: sourcing $f" + . "$f" + fi + ;; + *.ini) + echo "$0: copying $f into /usr/local/etc/php/conf.d/" + cp "$f" /usr/local/etc/php/conf.d/ + ;; + esac + done +} + +echo "Running entrypoint files from /docker-entrypoint-initdb.d/*" +docker_process_init_files /docker-entrypoint-initdb.d/* + +echo "Starting docker-php-entrypoint with $@" +source /usr/local/bin/docker-php-entrypoint