14

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.

The "official" version checked into Docker Hub (https://hub.docker.com/_/httpd/) still write to disk.

Also, what do I need to do to stop Apache from failing when it tries to roll the logs?

One other thing - ideally, I'd really like to do this without another add-on. nginx can do this trivially.

aronchick
  • 6,786
  • 9
  • 48
  • 75

6 Answers6

9

I'm not positive that this won't mess with httpd's logging at all (e.g. if it tries to seek within the file), but you can set up symlinks from the log paths to /dev/stdout and /dev/stderr, like so:

ln -sf /dev/stdout /path/to/access.log
ln -sf /dev/stderr /path/to/error.log

The entry command to the vanilla httpd container from Docker Hub could be made to be something like

ln -sf /dev/stdout /path/to/access.log && ln -sf /dev/stderr /path/to/error.log && /path/to/httpd
Alex Robinson
  • 12,633
  • 2
  • 38
  • 55
  • I tried this and the logs showed up when docker logs -n command was run. However, when I tried to access the logs from inside the container, cat /path_to_logs.log, the file did not open, The cursor just blinks and nothing happens – mnagdev Sep 24 '21 at 05:50
5

According to the apache mailing list, you can just directly write to /dev/stdio (on Unix like systems) as that's just a regular ol' file handle. Easy! Pasting...

The most efficient answer depends on your operating system. If you're on a UNIX like system which provides /dev/stdout and /dev/stderr (or perhaps /dev/fd/1 and /dev/fd/2) then use those file names. If that isn't an option use the piped output feature. For example, from my config:

CustomLog "|/usr/sbin/rotatelogs -c -f -l -L
/private/var/log/apache2/test-access.log
/private/var/log/apache2/test-access.log.%Y-%m-%d 86400     "
krader_custom ErrorLog "|/usr/sbin/rotatelogs -c -f -l -L
/private/var/log/apache2/test-error.log
/private/var/log/apache2/test-error.log.%Y-%m-%d 86400" 

Obviously you'll want to substitute another program for /usr/sbin/rotatelogs in the example above that writes the data where you want it to go.

https://mail-archives.apache.org/mod_mbox/httpd-users/201508.mbox/%3CCABx2=D-wdd8FYLkHMqiNOKmOaNYb-tAOB-AsSEf2p=ctd6sMdg@mail.gmail.com%3E

Schparky
  • 429
  • 4
  • 10
aronchick
  • 6,786
  • 9
  • 48
  • 75
3

I know it's an old question, but I had this need today.

On an Alpine 3.6, the following instructions, in httpd.conf, are working:

Errorlog /dev/stderr
Transferlog /dev/stdout

I add them to my container this way:

FROM alpine:3.6
RUN apk --update add apache2
RUN sed -i -r 's@Errorlog .*@Errorlog /dev/stderr@i' /etc/apache2/httpd.conf
RUN echo "Transferlog /dev/stdout" >> /etc/apache2/httpd.conf
...
arvymetal
  • 2,787
  • 1
  • 30
  • 39
2

I adjusted config, as from the Dockerfile recipe of httpd, they use sed to adjust the config, to change ErrorLog and CustomLog as follows:

sed -ri ' \
s!^(\s*CustomLog)\s+\S+!\1 /proc/self/fd/1!g; \
s!^(\s*ErrorLog)\s+\S+!\1 /proc/self/fd/2!g; \
' /usr/local/apache2/conf/httpd.conf \

See https://github.com/docker-library/httpd/blob/master/2.4/Dockerfile (towards the end of the file)

wogsland
  • 9,106
  • 19
  • 57
  • 93
Hiren
  • 21
  • 2
1

You can send your ErrorLog to syslog directly, and you can send any CustomLog (access log) to any executable that reads from stdin. There are log aggregation tools, or you can again use syslog w/ e.g. /usr/bin/logger.

covener
  • 17,402
  • 2
  • 31
  • 45
  • Thanks! This is for a docker container, so I'm trying to avoid the file or an additional binary altogether - would I just pipe the access log to stdin via "|cat" – aronchick Aug 14 '15 at 00:32
0

You could try using the dockerize tool. With that you could wrap the httpd-foreground command and redirect its log files to stdout/stderr (don't know exactly the httpd log file paths, simply adjust them to your needs):

CMD ["dockerize", "-stdout", "/var/log/httpd.log", "-stderr", "/var/log/httpd.err", "httpd-foreground"]

In addition to that you could grab that containers stdout/stderr then by specifying a syslog log driver and redirect them to the /var/log/syslog log file on the docker host:

docker run -d --log-driver=syslog ...
Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59