How to get php-fpm to log to stdout / stderr when running in a docker container
Solution 1
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
Solution 2
Note that the baked in fpm config for the latest version of the official PHP fpm docker image writes to the standard streams:
error_log = /proc/self/fd/2
...
; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/2
Solution 3
PHP-FPM logs will only appear in STDERR - so you can symlink the fpm.log
to /dev/stderr
if you want.
ln -sf /dev/stderr /var/log/fpm-access.log
ln -sf /dev/stderr /var/log/fpm-error.log
Related videos on Youtube
Tom
Updated on September 18, 2022Comments
-
Tom over 1 year
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? -
CMCDragonkai over 8 yearsIt's possible for there to be
/dev/stdin ~ /dev/fd/0 ~ /proc/self/fd/0
, and/dev/stdout
and/dev/stderr
variants. Might be easier to remember to use/dev/stdin
. -
Andrew Domaszek over 8 yearsThis solution was given in the question and the asker stated it didn't work. Maybe you can specify how he can load it in his dockerfile to make it work properly or other diagnostics he can perform on his container?
-
Tom over 8 yearsThanks, that's interesting. I didn't know that there's a official image now
-
rfay about 6 yearsThere's an error in the answer - it's "access_log" not "access.log"
-
CommandZ almost 6 yearsIt appears that it is
access.log
: github.com/docker-library/php/blob/… -
Derek about 5 yearsWhich docker image are you referring to? I ran php:7-fpm and it doesn't appear to be logging errors to
stderr
. -
laimison about 4 yearsIf you run
php-fpm
inside a container it should report whaterror_log
value is. In my case it's/proc/self/fd/2
as needed. Docker imagephp:7.2-fpm
. -
funder7 over 3 years@AndrewDomaszek file names are different