Skip to content

Commit 7c90b9c

Browse files
committed
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.
1 parent 251fea0 commit 7c90b9c

File tree

9 files changed

+141
-2
lines changed

9 files changed

+141
-2
lines changed

.github/workflows/test_buildx_and_publish.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ jobs:
2424
2525
- name: Run tests
2626
run: |
27-
docker run --name test0 -d -p 8000:80 -v $PWD/tests/fixtures:/var/www/html moodle-php-apache
27+
docker run --name test0 -d -p 8000:80 \
28+
-v $PWD/tests/fixtures:/var/www/html \
29+
-v $PWD/tests/docker-entrypoint.d:/docker-entrypoint.d \
30+
moodle-php-apache
2831
docker exec test0 php /var/www/html/test.php
32+
docker exec test0 php /var/www/html/check-ini.php
33+
docker exec test0 php /var/www/html/check-entrypoint-scripts.php
2934
curl --fail http://127.0.0.1:8000/test.php
35+
curl --fail http://127.0.0.1:8000/check-ini.php
3036
3137
- name: Display container logs on failure
3238
if: failure()

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ ADD root/usr /usr
4747

4848
# Fix the original permissions of /tmp, the PHP default upload tmp dir.
4949
RUN chmod 777 /tmp && chmod +t /tmp
50+
51+
CMD ["apache2-foreground"]
52+
ENTRYPOINT ["moodle-docker-php-entrypoint"]

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,40 @@ $ docker run --name web0 -p 8080:80 -v $PWD:/var/www/html moodlehq/moodle-php-a
3939
* 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.
4040
* Verified by [automated tests](https://travis-ci.com/moodlehq/moodle-php-apache).
4141
* Autobuilt from GHA, on push.
42+
* Support for entrypoint scripts and PHP Configuration
4243

4344
## Directories
44-
To faciliate testing and easy setup the following directories are created and owned by www-data by default:
45+
To facilitate testing and easy setup the following directories are created and owned by www-data by default:
4546

4647
* `/var/www/moodledata`
4748
* `/var/www/phpunitdata`
4849
* `/var/www/behatdata`
4950
* `/var/www/behatfaildumps`
5051

52+
## Initialisation scripts
53+
54+
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`).
55+
56+
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:
57+
58+
```
59+
; Specify a max filesize of 200M for uploads.
60+
upload_max_filesize = 200M
61+
post_max_size = 210M
62+
```
63+
64+
When starting your container you could do so passing in the config directory:
65+
66+
```
67+
docker run \
68+
--name web0 \
69+
-p 8080:80 \
70+
-v $PWD/moodle:/var/www/html
71+
-v $PWD/config:/docker-entrypoint.d \
72+
moodle-php-apache:latest
73+
```
74+
75+
These initialization files will be executed in sorted name order as defined by the current locale, which defaults to en_US.utf8.
5176

5277
## See also
5378
This container is part of a set of containers for Moodle development, see also:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
set -Eeo pipefail
3+
4+
docker_process_init_files() {
5+
local f
6+
for f; do
7+
case "$f" in
8+
*.sh)
9+
# Note: This hack is required for MacOS because the exeute bit is not checked for bind mounts.
10+
# The executable bit is stored, but the test -x flag does not return corretly.
11+
# Copying the file to an alternate file system allows it to be respected.
12+
rm -f /tmp/testscript
13+
cp "$f" /tmp/testscript
14+
if [ -x "/tmp/testscript" ]; then
15+
echo "$0: running $f"
16+
"$f"
17+
else
18+
echo "$0: sourcing $f"
19+
. "$f"
20+
fi
21+
;;
22+
*.ini)
23+
echo "$0: copying $f into /usr/local/etc/php/conf.d/"
24+
cp "$f" /usr/local/etc/php/conf.d/
25+
;;
26+
esac
27+
done
28+
}
29+
30+
echo "Running entrypoint files from /docker-entrypoint.d/*"
31+
docker_process_init_files /docker-entrypoint.d/*
32+
echo
33+
34+
echo "Starting docker-php-entrypoint with $@"
35+
source /usr/local/bin/docker-php-entrypoint
36+
echo
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
; Test file which disable file uploads.
2+
file_uploads = Off
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file should not have a shbang! as it is expected to be sourced.
2+
# It should not be executable either.
3+
4+
mkdir -p /var/www/data
5+
6+
(return 0 2>/dev/null) && sourced=1 || sourced=0
7+
8+
if [ $(sourced) -eq 1 ]; then
9+
echo "Sourced" >> /var/www/data/sourced.txt
10+
fi

tests/docker-entrypoint.d/40-exec.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file should not have a shbang! as it is expected to be sourced.
2+
# It should not be executable either.
3+
4+
mkdir -p /var/www/data
5+
6+
(return 0 2>/dev/null) && sourced=1 || sourced=0
7+
8+
if [ $(sourced) -eq 0 ]; then
9+
echo "Executed" >> /var/www/data/executed.txt
10+
fi
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
$sourced = file_exists('/var/www/data/sourced.txt');
4+
$executed = file_exists('/var/www/data/executed.txt');
5+
6+
if (php_sapi_name() === 'cli') {
7+
if ($sourced && $executed) {
8+
echo "OK\n";
9+
exit(0);
10+
} else if ($sourced && !$executed) {
11+
echo "Executable file was not executed";
12+
} else {
13+
echo "non-executable file was not sourced";
14+
}
15+
exit(1);
16+
} else {
17+
if ($sourced && $executed) {
18+
header('HTTP/1.1 200 - OK');
19+
exit(0);
20+
} else if ($sourced && !$executed) {
21+
$message = "Executable file was not executed";
22+
} else {
23+
$message = "non-executable file was not sourced";
24+
}
25+
header('HTTP/1.1 500 - ' . $message);
26+
exit(1);
27+
}

tests/fixtures/check-ini.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
$uploadsEnabled = ini_get('file_uploads');
4+
5+
if (php_sapi_name() === 'cli') {
6+
if (empty($uploadsEnabled)) {
7+
echo "OK\n";
8+
exit(0);
9+
}
10+
echo "Uploads are enabled and should be disabled.";
11+
var_dump($uploadsEnabled);
12+
exit(1);
13+
} else {
14+
if (empty($uploadsEnabled)) {
15+
header('HTTP/1.1 200 - OK');
16+
exit(0);
17+
}
18+
header('HTTP/1.1 500 - Uploads are enabled and should be disabled: ' . var_export($uploadsEnabled, true));
19+
exit(1);
20+
}

0 commit comments

Comments
 (0)