Skip to content

Commit

Permalink
Ensure that all the output on bootstrap goes to stderr
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stronk7 committed Oct 5, 2023
1 parent d4ffecc commit 97474d4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test_buildx_and_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ jobs:
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
# Verify that CLI execution only includes the expected output (if we discard stderr).
stdout=$(docker run --rm -v $PWD/tests/fixtures:/var/www/html \
moodle-php-apache php /var/www/html/check-stderr.php 2>/dev/null)
[[ ${stdout} == "Hello World!" ]] && echo "OK" || echo "Fail"
# Verify that stderr still contains information about the bootstrap.
stdout=$(docker run --rm -v $PWD/tests/fixtures:/var/www/html \
moodle-php-apache php /var/www/html/check-stderr.php 2>&1)
[[ ${stdout} =~ "Checking for php configuration" ]] && echo "OK" || echo "Fail"
- name: Display container logs on failure
if: failure()
Expand Down
15 changes: 6 additions & 9 deletions root/usr/local/bin/moodle-docker-php-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,26 @@ docker_process_init_files() {
rm -f /tmp/testscript
cp "$f" /tmp/testscript
if [ -x "/tmp/testscript" ]; then
echo "$0: running $f"
echo "$0: running $f" >/dev/stderr
"$f"
else
echo "$0: sourcing $f"
echo "$0: sourcing $f" >/dev/stderr
. "$f"
fi
;;
*.ini)
echo "$0: copying $f into /usr/local/etc/php/conf.d/"
echo "$0: copying $f into /usr/local/etc/php/conf.d/" >/dev/stderr
cp "$f" /usr/local/etc/php/conf.d/
;;
esac
done
}

echo "Running PHP Configuration fetcher"
echo "Running PHP Configuration fetcher" >/dev/stderr
/usr/local/bin/moodle-docker-php-ini
echo

echo "Running entrypoint files from /docker-entrypoint.d/*"
echo "Running entrypoint files from /docker-entrypoint.d/*" >/dev/stderr
docker_process_init_files /docker-entrypoint.d/*
echo

echo "Starting docker-php-entrypoint with $@"
echo "Starting docker-php-entrypoint with $@" >/dev/stderr
source /usr/local/bin/docker-php-entrypoint
echo
10 changes: 5 additions & 5 deletions root/usr/local/bin/moodle-docker-php-ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

echo "Checking for php configuration in environment"
echo "Checking for php configuration in environment" >/dev/stderr

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

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

cat << EOF >> $localinifile
; $fullname=$value
Expand All @@ -29,11 +29,11 @@ EOF

if [[ $fullname = PHP_EXTENSION_* ]]; then
extension=`echo $fullname | sed 's/^PHP_EXTENSION_//'`
echo "=> Found extension to enable: '${extension}'"
echo "=> Found extension to enable: '${extension}'" >/dev/stderr
if [[ -f "/usr/local/etc/php/conf.d/docker-php-ext-${extension}.ini" ]]; then
echo "=> Extension already enabled: '${extension}'"
echo "=> Extension already enabled: '${extension}'" >/dev/stderr
else
echo "=> Enabling extension '${extension}'"
echo "=> Enabling extension '${extension}'" >/dev/stderr
docker-php-ext-enable ${extension}
fi
fi
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/check-stderr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo "Hello World!";

0 comments on commit 97474d4

Please sign in to comment.