Skip to content

Commit 5e8c4b0

Browse files
committed
Ensure that all the output on bootstrap goes to stderr
That way we can filter out it if needed, for example if we just want to instantiate a php container to run a php script, we don't want all the bootstrap (entrypoint, ini config...) information to pollute the execution. That way, this will work as expected: ``` $ docker run --rm moodlehq/moodle-php-apache php -r 'echo "Hi!" . PHP_EOL;' 2>/dev/null Hi! ``` And, without discarding stderr, we still get all the information (stdout + stderr): ``` $ docker run --rm moodlehq/moodle-php-apache php -r 'echo "Hi!" . PHP_EOL;' Running PHP Configuration fetcher Checking for php configuration in environment Running entrypoint files from /docker-entrypoint.d/* Starting docker-php-entrypoint with php -r echo "Hi!" . PHP_EOL; Hi! ``` The only alternative to the above that I can imagine is to completely suppress any output (to both stdout and stderr) in out bootstrap. Note that I've also tried to send the information to Apache error log, but that was futile, because the images have error.log as alias of stderr.
1 parent d4ffecc commit 5e8c4b0

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

Diff for: .github/workflows/test_buildx_and_publish.yml

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ jobs:
3939
docker exec test0 php /var/www/html/check-entrypoint-scripts.php
4040
curl --fail http://127.0.0.1:8000/test.php
4141
curl --fail http://127.0.0.1:8000/check-ini.php
42+
# Verify that CLI execution only includes the expected output (if we discard stderr).
43+
stdout=$(docker run --rm --name test1 moodle-php-apache php /var/www/html/check-stderr.php 2>/dev/null)
44+
[[ ${stdout} == "Hello World!" ]] && echo "OK" || echo "Fail"
45+
# Verify that stderr still contains information about the bootstrap.
46+
stdout=$(docker run --rm --name test1 moodle-php-apache php /var/www/html/check-stderr.php 2>&1)
47+
[[ ${stdout} =~ "Checking for php configuration" ]] && echo "OK" || echo "Fail"
4248
4349
- name: Display container logs on failure
4450
if: failure()

Diff for: root/usr/local/bin/moodle-docker-php-entrypoint

+6-9
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,26 @@ docker_process_init_files() {
1212
rm -f /tmp/testscript
1313
cp "$f" /tmp/testscript
1414
if [ -x "/tmp/testscript" ]; then
15-
echo "$0: running $f"
15+
echo "$0: running $f" >/dev/stderr
1616
"$f"
1717
else
18-
echo "$0: sourcing $f"
18+
echo "$0: sourcing $f" >/dev/stderr
1919
. "$f"
2020
fi
2121
;;
2222
*.ini)
23-
echo "$0: copying $f into /usr/local/etc/php/conf.d/"
23+
echo "$0: copying $f into /usr/local/etc/php/conf.d/" >/dev/stderr
2424
cp "$f" /usr/local/etc/php/conf.d/
2525
;;
2626
esac
2727
done
2828
}
2929

30-
echo "Running PHP Configuration fetcher"
30+
echo "Running PHP Configuration fetcher" >/dev/stderr
3131
/usr/local/bin/moodle-docker-php-ini
32-
echo
3332

34-
echo "Running entrypoint files from /docker-entrypoint.d/*"
33+
echo "Running entrypoint files from /docker-entrypoint.d/*" >/dev/stderr
3534
docker_process_init_files /docker-entrypoint.d/*
36-
echo
3735

38-
echo "Starting docker-php-entrypoint with $@"
36+
echo "Starting docker-php-entrypoint with $@" >/dev/stderr
3937
source /usr/local/bin/docker-php-entrypoint
40-
echo

Diff for: root/usr/local/bin/moodle-docker-php-ini

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
echo "Checking for php configuration in environment"
5+
echo "Checking for php configuration in environment" >/dev/stderr
66

77
localinifile="/usr/local/etc/php/conf.d/20-local.ini"
88

@@ -18,7 +18,7 @@ env | while IFS= read -r line; do
1818
fullname=${line%%=*}
1919
if [[ $fullname = PHP_INI-* ]]; then
2020
name=`echo $fullname | sed 's/^PHP_INI-//'`
21-
echo "=> Found '${name}' with value '${value}'"
21+
echo "=> Found '${name}' with value '${value}'" >/dev/stderr
2222

2323
cat << EOF >> $localinifile
2424
; $fullname=$value
@@ -29,11 +29,11 @@ EOF
2929

3030
if [[ $fullname = PHP_EXTENSION_* ]]; then
3131
extension=`echo $fullname | sed 's/^PHP_EXTENSION_//'`
32-
echo "=> Found extension to enable: '${extension}'"
32+
echo "=> Found extension to enable: '${extension}'" >/dev/stderr
3333
if [[ -f "/usr/local/etc/php/conf.d/docker-php-ext-${extension}.ini" ]]; then
34-
echo "=> Extension already enabled: '${extension}'"
34+
echo "=> Extension already enabled: '${extension}'" >/dev/stderr
3535
else
36-
echo "=> Enabling extension '${extension}'"
36+
echo "=> Enabling extension '${extension}'" >/dev/stderr
3737
docker-php-ext-enable ${extension}
3838
fi
3939
fi

Diff for: tests/fixtures/check-stderr.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
echo "Hello World!";

0 commit comments

Comments
 (0)