How does one use Apache in a Docker Container and write nothing to disk (all logs to STDIO / STDERR)?

19,176

Solution 1

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

Solution 2

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

Solution 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
...

Solution 4

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)

Solution 5

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.

Share:
19,176

Related videos on Youtube

aronchick
Author by

aronchick

About me: I've heard containers and Kubernetes are a thing. Are they a thing? Yes. Yes, they are. I now work on machine learning.

Updated on September 14, 2022

Comments

  • aronchick
    aronchick over 1 year

    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
    aronchick over 8 years
    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"
  • Alex Robinson
    Alex Robinson over 8 years
    Judging from the blog post, doesn't Dockerize just tail from the specified files after they've been written to disk?
  • Henrik Sachse
    Henrik Sachse over 8 years
    Yes, it does a tail under the hood.
  • aronchick
    aronchick over 8 years
    I appreciate this, but it writes to the disk, so it's not ideal.
  • mnagdev
    mnagdev over 2 years
    I tried this and the logs showed up when docker logs <podname> -n<namespace> 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