Apache / PHP error_log location in Docker
When a PHP script inside a container throws a fatal error, such as require(/var/www/foo.php): failed to open stream, the output often appears directly on the browser screen if it's uncaught by a framework or handled poorly. However, relying on visible stack traces for debugging is fragile; you need persistent logs to analyze failures after the request completes or when the application restarts.
In a standard Docker setup using the official Apache PHP image, the logging architecture differs significantly from running Apache directly on a host OS. The confusion usually stems from the fact that Apache does not write its access or error logs to files by default in this environment. Instead, it streams them directly to the container's standard output and standard error.
If you run docker logs containerName, you see the expected Access Log entries formatted like Combined Log Format:192.168.2.1 - - [17/Mar/2019:10:00:00 +0000] "GET / HTTP/1.1" 200 505 ...
However, you might notice that PHP errors don't appear there either. This happens because the default Apache configuration in many Dockerfiles redirects error.log and access.log to /dev/stdout and /dev/stderr. While this seems like a good idea for log aggregation tools, it creates a specific problem: where does PHP's own error_log directive actually write its data?
When you inspect the filesystem inside the container using docker exec, you will see symbolic links rather than actual files. The directory structure typically looks like this:
total 0
lrwxrwxrwx. 1 www-data www-data 11 Feb 6 04:42 other_vhosts_access.log -> /dev/stdout
lrwxrwxrwx. 1 www-data www-data 11 Feb 6 04:42 error.log -> /dev/stderr
lrwxrwxrwx. 1 www-data www-data 11 Feb 6 04:42 access.log -> /dev/stdout
The Apache logs are correctly going to stdout/stderr. The issue arises because the PHP runtime itself is configured differently by default. In many pre-configured Docker images, PHP is set to write error logs to a specific file path (often /var/log/php/error_log or similar) unless overridden. If that file does not exist or is not writable by the current user, PHP might silently fail to log, or it might throw an additional warning indicating it cannot open the log file.
To resolve this and ensure you can capture both Apache and PHP errors in one place (stdout/stderr), you must explicitly configure PHP's logging destination. You can do this by modifying your php.ini or using a command-line override when starting the container.
For example, adding the following line to your php.ini will redirect PHP errors to the standard error stream:
error_log = /proc/self/fd/2
The path /proc/self/fd/2 is a special file representing the current process's stderr. When Apache logs are also configured to use /dev/stderr, both streams merge into the same output. Consequently, docker logs containerName will then display both the HTTP access logs and the PHP error stack traces in chronological order.
Alternatively, if you prefer keeping logs as files for archival purposes, you can mount a host directory into the container and update the configuration:
error_log = /var/log/php_errors.log
log_errors = On
display_errors = Off
This approach requires ensuring the directory exists and has the correct permissions (usually owned by www-data). A common pitfall is ignoring permission errors; if the log file exists but isn't writable, PHP will stop logging entirely without notifying you. Always verify that the target path exists before restarting the container.
If you are using a specific Laravel deployment strategy or need centralized logging, ensure your container entrypoint script handles these configurations correctly. Sometimes, the official Docker image includes a custom Dockerfile that overrides standard paths, which can lead to unexpected behavior if not inspected closely. Always check the docker inspect output for volume mounts and environment variables that might influence log locations.
By aligning PHP's error_log directive with Apache's stdout/stderr streams, you eliminate the need to hunt through multiple directories. This unified approach makes debugging significantly faster and ensures that no critical errors are lost in a hidden file on the ephemeral container filesystem.