Search code examples
phpdockererror-logging

How to log to a file from a dockerized php app


I have a php app dockerized. My issue is how to capture errors from php service into a dedicated file on the host. docker file looks is next:

version: "3.9"

services:
  web:
    image: nginx:latest
    ports:
      - "3000:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./public:/public
  php:
    build:
      context: .
      dockerfile: PHP.Dockerfile
    environment:
      APP_MODE: 'development'
    env_file:
      - 'dev.env'
    volumes:
      - ./app:/app
      - ./public:/public
      - ./php.conf:/usr/local/etc/php-fpm.d/zz-log.conf
  mysql:
    image: mariadb:latest
    environment:
      MYSQL_ROOT_PASSWORD: <pass here>
    env_file:
      - 'dev.env'
    volumes:
      - mysqldata:/var/lib/mysql
      - ./app:/app
    ports:
        - '3306:3306'
volumes:
  mysqldata: {}

my php.conf that maps as /usr/local/etc/php-fpm.d/zz-log.conf inside php service looks like bellow:

php_admin_value[error_log] = /app/php-error.log
php_admin_flag[log_errors] = on
catch_workers_output = yes

My intention is using php error_log() function and have all the logs recorded in php-error.log which is a file inside volume app. Now, all logs from containers are shown on terminal only.

I have been struggling with this several hours and have no ideea how to continue. Thank you


Solution

  • I don't know what is your source image. I assume some official docker image for PHP like https://hub.docker.com/_/php

    All containerized applications are usually configured to log to stdout so you must override that behaviour. This is really PHP specific and I'm no PHP expert. From what you let us know it looks like you know how to override that behaviour (by using some error_log() function and php_admin_value[error_log] = /app/php-error.log property.

    If the behaviour is overridden you should ensure the file app/php-error.log exists inside of the PHP container (i.e. get inside the container by something like docker exec -it my-container-id /bin/bash and then do ls /app/php-error.log and cat /app/php-error.log to see if the file is created.

    Because you're mounting the ./app directory from the host to /app directory in container you already have them mirrored. Whatever is inside container's /app you will find in also your /path/to/docker/compose/app directory. You can check if file exists and some content is inside. If not you failed to override the default behaviour of where PHP is logging to.