How to Log php-fpm to stdout/stderr in Docker

best practicesdockerloggingphp-fpm

I have php-fpm in a docker container and in the Dockerfile I edit the fpm config file (/etc/php5/fpm/pool.d/www.conf) to set up access logs to go to /var/log/fpm-access.log and error logs to go to /var/log/fpm-php.www.log:

# Do some php-fpm config
#  Redirect worker stdout and stderr into main error log
#  Activate the fpm access log
#  Enable display errors
#  Enable the error log
RUN sed -i '/^;catch_workers_output/ccatch_workers_output = yes' /etc/php5/fpm/pool.d/www.conf && \
    sed -i '/^;access.log/caccess.log = /var/log/fpm-access.log' /etc/php5/fpm/pool.d/www.conf && \
    sed -i '/^;php_flag\[display_errors\]/cphp_flag[display_errors] = off' /etc/php5/fpm/pool.d/www.conf && \
    sed -i '/^;php_admin_value\[error_log\]/cphp_admin_value[error_log] = /var/log/fpm-php.www.log' /etc/php5/fpm/pool.d/www.conf && \
    sed -i '/^;php_admin_flag\[log_errors\]/cphp_admin_flag[log_errors] = on' /etc/php5/fpm/pool.d/www.conf

This works fine – I can get a shell into the container to see the logs. But… it is not best-practice.

The problem is when I try to use the docker log collector – I need php-fpm to log to stdout or stderr so docker can capture them and provide them to the docker logs command.

I tried to do this in the Dockerfile (which is a idea I copied from the official nginx docker image):

# Redirect fpm logs to stdout and stderr so they are forwarded to the docker log collector
RUN ln -sf /dev/stdout /var/log/fpm-access.log && \
    ln -sf /dev/stderr /var/log/fpm-php.www.log

This is not working – no access logs are seen from docker logs – I'm trying to figure out why? Did anyone else that uses fpm in docker manage to get logging working to the docker log collector?

Best Answer

Ok, the way to do this is to send the error and the access logs to the following address:

/proc/self/fd/2

In php5-fpm.log add:

access.log = /proc/self/fd/2
error_log = /proc/self/fd/2

NOTE: access.log is correct, find in this page https://www.php.net/manual/en/install.fpm.configuration.php