34

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?

3 Answers 3

38

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.conf (or the appropriate configuration file) 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

4
  • 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. Dec 1, 2015 at 7:22
  • 2
    There's an error in the answer - it's "access_log" not "access.log"
    – rfay
    Mar 27, 2018 at 21:19
  • 6
    It appears that it is access.log: github.com/docker-library/php/blob/…
    – CommandZ
    Jul 6, 2018 at 17:38
  • 3
    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, 2022 at 14:29
20

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
4
  • Thanks, that's interesting. I didn't know that there's a official image now
    – Tom
    Jan 13, 2016 at 17:25
  • 4
    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, 2019 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, 2020 at 19:35
  • @laimison "Report" error_log where? 3 hours ago
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
2
  • 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? Dec 18, 2015 at 3:11
  • @AndrewDomaszek file names are different
    – funder7
    Nov 1, 2020 at 16:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .