31

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?

Tom
  • 4,157
  • 11
  • 41
  • 52

3 Answers3

34

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

b01
  • 123
  • 6
  • It'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`. – CMCDragonkai Dec 01 '15 at 07:22
  • 2
    There's an error in the answer - it's "access_log" not "access.log" – rfay Mar 27 '18 at 21:19
  • 5
    It appears that it is `access.log`: https://github.com/docker-library/php/blob/6e600f59a4405b5066eb78f5f02180212dffd065/5.6/fpm/php-fpm.conf – CommandZ Jul 06 '18 at 17:38
  • Believe this should be `php5-fpm.conf`, or on a more current PHP, `php-fpm.conf`. Adding those directives to `.log` file would do nothing :D – Jon L. Aug 17 '22 at 14:29
19

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
Darren Gordon
  • 291
  • 2
  • 3
  • Thanks, that's interesting. I didn't know that there's a official image now – Tom Jan 13 '16 at 17:25
  • 3
    Which docker image are you referring to? I ran php:7-fpm and it doesn't appear to be logging errors to `stderr`. – Derek May 15 '19 at 12:06
  • If you run `php-fpm` inside a container it should report what `error_log` value is. In my case it's `/proc/self/fd/2` as needed. Docker image `php:7.2-fpm`. – laimison Apr 11 '20 at 19:35
2

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
chicks
  • 3,639
  • 10
  • 26
  • 36
Andi
  • 129
  • 1
  • 4
    This 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? – Andrew Domaszek Dec 18 '15 at 03:11
  • @AndrewDomaszek file names are different – funder7 Nov 01 '20 at 16:36