Skip to content

Commit

Permalink
Add support for entrypoint scripts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andrewnicols committed Mar 15, 2023
1 parent a5411f6 commit 4cf8237
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 34 additions & 0 deletions root/usr/local/bin/moodle-docker-php-entrypoint
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4cf8237

Please sign in to comment.