I am trying to get the error logs out of my php docker. I found that the error.log file was going nowhere, hence modified my dockerfile like this:
FROM php:7.4-apache
RUN a2enmod rewrite
RUN docker-php-ext-install mysqli pdo pdo_mysql;
docker-php-ext-enable mysqli pdo pdo_mysql;
# Redirect apache log files to stdout, see https://github.com/moby/moby/issues/19616
RUN ln -sf /proc/1/fd/1 /var/log/apache2/access.log
RUN ln -sf /proc/1/fd/1 /var/log/apache2/error.log
Resulting in the files below:
# ls -l /var/log/apache2/
total 0
lrwxrwxrwx 1 root root 12 May 11 18:25 access.log -> /proc/1/fd/1
lrwxrwxrwx 1 root root 12 May 11 18:25 error.log -> /proc/1/fd/1
lrwxrwxrwx 1 www-data www-data 11 Nov 15 04:17 other_vhosts_access.log -> /dev/stdout
That part is now working: running echo "test" > /var/log/apache2/error.log
does write test to the docker logs. Plus, file permissions are not restrictive (see https://stackoverflow.com/a/12566881/3519951).
I added a crashing call, dfgdfga.test()
, but the error doesn’t show up. Same if I create a log file instead of redirecting to docker output. It remains empty.
2
Answers
While I was reviewing the question I figured it out. Sharing here in case it might help.
Running
php -i | grep log
I found this:It's a surprising default in my opinion, but more than enough to explain the absence of logs. So I created a small ini file (inspired from this article) to pipe it straight to the docker logs:
And copied it in dockerfile like this:
I ran this inside my php8-apache docker container and it fixed it for me. I probably should have made a backup of the production php.ini first.
cp "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
If I was building a new one I would put this in my DockerFile, and cp the php.ini to php.ini-production before copying the development one in place.
After this change all my errors come out in a typical docker fashion.