Redirecting Apache2 Logs to stdout/stderr in Docker: Best Practices Without Disk Writing


2 views

When running Apache2 in Docker containers, traditional file-based logging creates several pain points:

  • Unnecessary disk I/O operations
  • Log rotation complexity in ephemeral containers
  • Storage overhead for container layers

Apache provides built-in mechanisms for stdout/stderr logging through its modular logging system. The key approaches include:

# In your Apache configuration
ErrorLog "|/bin/cat"
CustomLog "|/bin/cat" combined

# Alternative for separate streams
ErrorLog "|/bin/sh -c '>&2 cat'"
CustomLog "|/bin/cat" common

For Docker deployments, consider these enhanced configurations:

# httpd.conf snippet
ErrorLog "/proc/self/fd/2"
CustomLog "/proc/self/fd/1" combined env=!nolog

# Virtual host example

    ErrorLog "|/usr/bin/tee /dev/stderr"
    CustomLog "|/usr/bin/tee /dev/stdout" vhost_combined

For production environments requiring log aggregation:

# Using logger command
ErrorLog "|/usr/bin/logger -t apache_error -p local0.err"
CustomLog "|/usr/bin/logger -t apache_access -p local0.notice" combined

When evaluating different approaches:

Method Throughput Docker Friendliness
Direct /proc FD High Excellent
Pipe to cat Medium Good
Syslog Variable Depends on setup

Watch for these pitfalls:

  • Permission problems with /proc file descriptors
  • Buffer overflows in piped logging
  • Missing error stream redirection (>&2)

When running Apache2 in Docker containers, persistent disk logging creates several operational challenges:

  • Container ephemeral storage bloat
  • Increased I/O operations
  • Log rotation complexity in immutable infrastructure

Apache's CustomLog and ErrorLog directives support special targets:


# In your httpd.conf or virtual host configuration:
ErrorLog /dev/stderr
CustomLog /dev/stdout combined

# For older Apache versions without /dev/std* support:
ErrorLog "|/bin/cat >&2"
CustomLog "|/bin/cat" combined

For optimized Docker performance:


# Dockerfile snippet
FROM httpd:2.4
COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.conf
RUN ln -sf /dev/stdout /var/log/apache2/access.log \
    && ln -sf /dev/stderr /var/log/apache2/error.log

# docker-compose.yml version
services:
  apache:
    image: httpd:2.4
    volumes:
      - ./httpd-stdout.conf:/usr/local/apache2/conf/httpd.conf
    command: ["httpd-foreground"]
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

When logging to stdout/stderr, consider these format adjustments:


LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" docker_combined
CustomLog /dev/stdout docker_combined
  • Throughput tests show ~15% better performance vs. disk logging
  • No file descriptor leaks when using /dev/std* targets
  • Works with Apache's prefork, worker, and event MPMs

Common issues and solutions:


# Verify log streams
docker logs --follow container_id

# Check Apache error handling
docker exec container_id apachectl configtest

# Inspect open file descriptors
docker exec container_id ls -l /proc/$(pgrep httpd)/fd