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

前端 未结 6 893
不知归路
不知归路 2021-02-13 12:18

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

相关标签:
6条回答
  • 2021-02-13 13:01

    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)

    0 讨论(0)
  • 2021-02-13 13:02

    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
    
    0 讨论(0)
  • 2021-02-13 13:03

    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

    0 讨论(0)
  • 2021-02-13 13:04

    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 ...
    
    0 讨论(0)
  • 2021-02-13 13:05

    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
    ...
    
    0 讨论(0)
  • 2021-02-13 13:09

    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.

    0 讨论(0)
提交回复
热议问题