How does one use Apache in a Docker Container and

2019-04-06 04: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.

6条回答
Viruses.
2楼-- · 2019-04-06 04:33

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.

查看更多
Emotional °昔
3楼-- · 2019-04-06 04:39

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 ...
查看更多
唯我独甜
4楼-- · 2019-04-06 04:45

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
查看更多
ゆ 、 Hurt°
5楼-- · 2019-04-06 04:46

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)

查看更多
smile是对你的礼貌
6楼-- · 2019-04-06 04:49

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

查看更多
Ridiculous、
7楼-- · 2019-04-06 04:50

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
...
查看更多
登录 后发表回答