skip to Main Content

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


  1. Chosen as BEST ANSWER

    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
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search