0

After a lot of researching, appearantly when you run Apache as main process for Docker, then docker logs follows /proc/1/fd/1 instead of /proc/self/fd/1.

Problem:

I am using apache2 as web server and unfortunately apache's www-data user cannot write to /proc/1/fd/1

Code for testing:

$handle = fopen('/proc/1/fd/1', 'a');
fwrite($handle, 'abc');
fclose($handle);

This gives me output of: fopen(/proc/1/fd/1): Failed to open stream: Permission denied

Goal:

Having possibility to write to output that Docker logs can follow while having possibility to write persistent volume as well (not exactly part of this topic, but maybe useful).

Extra info:

R.P
  • 500
  • 2
  • 6
  • 17
  • I assume you're using PHP? Can't you use `console.log`? – Hans Kilian Jan 03 '23 at 08:15
  • I am using PHP, thanks for pointing out - added extra tag. Regarding console.log - PHP equivalent is error_log. For simplicity of my example I added handler as www-data has problems, not my main process which is ran as root. If I put ErrorLog "/proc/1/fd/1" into my VirtualHost config, then I successfully can get my data Docker logs, but this solves only half my problem. Main problem is that I need to have logs to persistent storage as well and not only to ones marked in VirtualHost config. – R.P Jan 03 '23 at 08:30
  • Why isn't Apache process ID 1? What image are you running, and how are you modifying its startup sequence? – David Maze Jan 03 '23 at 11:26
  • @DavidMaze Apache process ID is 1. Using php:8.0.23-apache image. – R.P Jan 03 '23 at 23:03

1 Answers1

0

Maybe I misunderstand what you're trying to achieve, but if you want to write to the log of the container, you can use error_log.

Create an index.php file containing

<?php
error_log("abc");
echo "Hello from PHP";
?>

and run a container using something like

docker run -d -p 8080:80 -v $(pwd):/var/www/html php:8.0-apache

Then, when you go to localhost:8080, you will get something like

[Tue Jan 03 08:43:28.255070 2023] [php:notice] [pid 17] [client 172.17.0.1:54326] abc

in the container log.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • This isn't that much about using error_log function rather than having PHP able to write to /proc/1/fd/1 I have no problem logging to proc with Apache & PHP when ErrorLog value is /proc/1/fd/1. – R.P Jan 03 '23 at 23:04