41

I'm running Apache2 in a docker container, and want to write nothing to the disk, writing logs to stdout and stderr. I've seen a few different ways to do this (Supervisord and stdout/stderr, Apache access log to stdout) but these seem like hacks. Is there no way to do this by default?

To be clear, I do not want to tail the log, since that will result in things being written to the disk in the container.

3
  • Aren't you going to want those logs to be easily accessible for troubleshooting/debugging purposes? Why not just write them to a (r)syslog server instead?
    – HTTP500
    Apr 4, 2016 at 18:30
  • 1
    @HTTP500 - They get captured on the outside of the docker container.
    – hookenz
    Jun 18, 2017 at 22:14
  • 1
    If you use: FROM php:5.6-apache, that already includes logs to stdout and stderr.
    – Martlark
    Sep 4, 2017 at 2:37

3 Answers 3

51
  ErrorLog /dev/stderr
  TransferLog /dev/stdout

works on ubuntu and centos fo me

3
  • which file should it go into etc pls May 21, 2019 at 0:26
  • This goes in you domain.conf file or .htaccess if you aren't using a conf. Sep 20, 2019 at 15:55
  • 2
    For Alpine Linux you set it in /etc/apache2/httpd.conf as ErrorLog /dev/stderr and CustomLog /dev/stdout combined Jul 9, 2020 at 15:12
39

How about placing this in your Dockerfile after the apache2 package is installed?

RUN ln -sf /proc/self/fd/1 /var/log/apache2/access.log && \
    ln -sf /proc/self/fd/1 /var/log/apache2/error.log

Assuming that this is the path of the logs. It is for Ubuntu 14.04 and also works for Ubuntu 16.04.

Note: if you're certain that the symbolic links /dev/stdout or /proc/stderr are there, then you may also use those. I prefer the path to the real file as this is guaranteed present.

9
  • works great also with Ubuntu 16.04 :)
    – OkieOth
    Sep 23, 2016 at 10:04
  • 2
    Damn, that's an ingenious hack! Apache tries to open a regular file, but gets redirected via symlink to its own stdout from its own perspective.
    – joonas.fi
    Dec 3, 2016 at 10:22
  • 1
    Just want to say thanks... the official apache httpd 2.4 docker container fails to write logs after enabling ssl. Adding these lines + ssl_request_log to the Dockerfile that pulls from httpd2.4 worked.
    – j.con
    Apr 4, 2017 at 17:22
  • 3
    You can abbreviate /proc/self/fd/1 as /dev/stdout. They're exactly the same thing. Jan 9, 2018 at 23:12
  • 1
    @ChuckAdams - they are a soft link and normally there, but no guarantees when building images that they are present. Especially cut down micro images. Whereas the kernel will always export /proc/self/fd/1 & 2.
    – hookenz
    Feb 8, 2018 at 0:48
2

Not specifically an answer asked for but maybe a better way, depending on your scenario, would be to to not log to stdout/stderr at all. Just pipe the logs to cat in a JSON format. This would remove the need to differentiate the streams as the json could have the data needed in it to distinguish them. eg something along the lines of the following. This can then be ingested much more easily into something like graylog

GlobalLog "| cat - " gelf
ErrorLog "| cat - " 

LogFormat "{ \"apache_log\": \"ACCESS\", \"app_name\": \"apache\",  \"Connection\": \"%{X-Forwarded-Proto}i:%{X-Forwarded-Port}i \", \"X-Forwarded-For\": \"%{X-Forwarded-For}i\",  \"version\": \"1.1\", \"vhost\": \"%V\", \"short_message\": \"%r\", \"timestamp\": %{%s}t, \"level\": 6, \"user_agent\": \"%{User-Agent}i\", \"source_ip\": \"%a\", \"duration_usec\": %D, \"duration_sec\": %T, \"request_size_byte\": %O, \"http_status\": %s, \"http_request_path\": \"%U\", \"http_request\": \"%U%q\", \"http_method\": \"%m\", \"http_referer\": \"%{Referer}i\", \"X-Powered-By\": \"%{X-Powered-By}i\" }" gelf

ErrorLogFormat "{ \"app_name\": \"apache\",  \"apache_log\": \"ERROR\", \"time\":\"%{%Y-%m-%d}tT%{%T}t.%{msec_frac}tZ\", \"function\" : \"[%-m:%l]\" , \"process\" : \" [pid %P:tid %T] \" , \"message\" : \"%M\" ,\ \"referer\"\ : \" %{Referer}i \" }"

There is also a gelf logging module, so you can stream direct from apache to a graylog type server as well if you wish

You must log in to answer this question.

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