3

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 2

5

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:

log_errors => Off => Off

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:

; log PHP errors to a file. E_ALL=32767
log_errors = on
error_reporting = 32767
error_log = /proc/1/fd/1

And copied it in dockerfile like this:

COPY logging.ini /usr/local/etc/php/conf.d/logging.ini
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.