dockernginxloggingsymlinknginx-log

Nginx logs in both file as well as docker log collector


I am deploying a Nginx container and in the standard Dockerfile logs are being redirected to container logs with these commands

RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

If I remove above mentioned commands then logs are written to access.log and error.log files as expected. How can I keep the logs in the files as well as send them to container logs ?


Solution

  • 1- The below command does not redirect the content of /var/log/nginx/access.log to /dev/stdout instead it created a symlink from /dev/stdout to /var/log/nginx/access.log . In other words, /var/log/nginx/access.log is just a symlink.

    ln -sf /dev/stdout /var/log/nginx/access.log
    
    # ls -alh /var/log/nginx/access.log
    lrwxrwxrwx 1 root root 11 Nov 23 01:12 /var/log/nginx/access.log -> /dev/stdout
    

    2- Storing files in the docker containers is not recommended at all for two reasons. First, the files will be lost once the containers are recreated. Second, the files could grow to huge sizes especially if there is not log rotation over these files. Instead of keeping a copy inside the container you can use FluentD or configure docker itself to forward the logs to an external server or a file on the host node.