From 7c90b9c316e734b349490141acd1df1374a4ec56 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. --- .github/workflows/test_buildx_and_publish.yml | 8 ++++- Dockerfile | 3 ++ README.md | 27 +++++++++++++- .../local/bin/moodle-docker-php-entrypoint | 36 +++++++++++++++++++ tests/docker-entrypoint.d/20-example.ini | 2 ++ tests/docker-entrypoint.d/30-sourced.sh | 10 ++++++ tests/docker-entrypoint.d/40-exec.sh | 10 ++++++ tests/fixtures/check-entrypoint-scripts.php | 27 ++++++++++++++ tests/fixtures/check-ini.php | 20 +++++++++++ 9 files changed, 141 insertions(+), 2 deletions(-) create mode 100755 root/usr/local/bin/moodle-docker-php-entrypoint create mode 100644 tests/docker-entrypoint.d/20-example.ini create mode 100644 tests/docker-entrypoint.d/30-sourced.sh create mode 100755 tests/docker-entrypoint.d/40-exec.sh create mode 100644 tests/fixtures/check-entrypoint-scripts.php create mode 100644 tests/fixtures/check-ini.php diff --git a/.github/workflows/test_buildx_and_publish.yml b/.github/workflows/test_buildx_and_publish.yml index 1a6b0a8..64c8e14 100644 --- a/.github/workflows/test_buildx_and_publish.yml +++ b/.github/workflows/test_buildx_and_publish.yml @@ -24,9 +24,15 @@ jobs: - name: Run tests run: | - docker run --name test0 -d -p 8000:80 -v $PWD/tests/fixtures:/var/www/html moodle-php-apache + docker run --name test0 -d -p 8000:80 \ + -v $PWD/tests/fixtures:/var/www/html \ + -v $PWD/tests/docker-entrypoint.d:/docker-entrypoint.d \ + moodle-php-apache docker exec test0 php /var/www/html/test.php + docker exec test0 php /var/www/html/check-ini.php + docker exec test0 php /var/www/html/check-entrypoint-scripts.php curl --fail http://127.0.0.1:8000/test.php + curl --fail http://127.0.0.1:8000/check-ini.php - name: Display container logs on failure if: failure() diff --git a/Dockerfile b/Dockerfile index 8fb5a03..2b41996 100644 --- a/Dockerfile +++ b/Dockerfile @@ -47,3 +47,6 @@ ADD root/usr /usr # Fix the original permissions of /tmp, the PHP default upload tmp dir. RUN chmod 777 /tmp && chmod +t /tmp + +CMD ["apache2-foreground"] +ENTRYPOINT ["moodle-docker-php-entrypoint"] diff --git a/README.md b/README.md index f0e986b..dba7d9e 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.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.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..3abc465 --- /dev/null +++ b/root/usr/local/bin/moodle-docker-php-entrypoint @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +set -Eeo pipefail + +docker_process_init_files() { + 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. + rm -f /tmp/testscript + 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.d/*" +docker_process_init_files /docker-entrypoint.d/* +echo + +echo "Starting docker-php-entrypoint with $@" +source /usr/local/bin/docker-php-entrypoint +echo diff --git a/tests/docker-entrypoint.d/20-example.ini b/tests/docker-entrypoint.d/20-example.ini new file mode 100644 index 0000000..ec45086 --- /dev/null +++ b/tests/docker-entrypoint.d/20-example.ini @@ -0,0 +1,2 @@ +; Test file which disable file uploads. +file_uploads = Off diff --git a/tests/docker-entrypoint.d/30-sourced.sh b/tests/docker-entrypoint.d/30-sourced.sh new file mode 100644 index 0000000..11dde07 --- /dev/null +++ b/tests/docker-entrypoint.d/30-sourced.sh @@ -0,0 +1,10 @@ +# This file should not have a shbang! as it is expected to be sourced. +# It should not be executable either. + +mkdir -p /var/www/data + +(return 0 2>/dev/null) && sourced=1 || sourced=0 + +if [ $(sourced) -eq 1 ]; then + echo "Sourced" >> /var/www/data/sourced.txt +fi diff --git a/tests/docker-entrypoint.d/40-exec.sh b/tests/docker-entrypoint.d/40-exec.sh new file mode 100755 index 0000000..d48dc75 --- /dev/null +++ b/tests/docker-entrypoint.d/40-exec.sh @@ -0,0 +1,10 @@ +# This file should not have a shbang! as it is expected to be sourced. +# It should not be executable either. + +mkdir -p /var/www/data + +(return 0 2>/dev/null) && sourced=1 || sourced=0 + +if [ $(sourced) -eq 0 ]; then + echo "Executed" >> /var/www/data/executed.txt +fi diff --git a/tests/fixtures/check-entrypoint-scripts.php b/tests/fixtures/check-entrypoint-scripts.php new file mode 100644 index 0000000..aa06d16 --- /dev/null +++ b/tests/fixtures/check-entrypoint-scripts.php @@ -0,0 +1,27 @@ +