Log rotation for a MongoDB running in a docker container

Hi,

I have a MongoDB version 4.2 running in a docker container.
The mongod.log is mapped to a volume, so is visible from the file system.

As my log is too big now - what is the way to enable log rotation for the configuration above?

Thanks

Hi @Tamar_Nirenberg did you got any automated solution for this?

This is really an anti pattern for docker containers. Log to stdout and use docker’s log management options.

1 Like

I don’t understand why it’s anti pattern. It’s always boiled down to each system requirements.

For instance, at my company, I was required to keep logs on production. So I only use docker log for testing and staging environment. For production, I config mongodb instances to output log to files, rotate them weekly then compress and move them to long term storage.

This is how I implement log rotation:

  • Create role & user on admin database
    db.createRole({ role: "logRotate", roles: [], privileges: [{ resource: { cluster: true }, actions: ["logRotate"] }] });
    db.createUser({ user: "logRotater", pwd: "LogRotater", roles: ["logRotate"] });
    
  • Create a file contains all direct connection string to each MongoDB instance that I want to rotate log. For example: file conn-strs contains
    mongodb://logRotater:LogRotater@<replset1-member1>/admin?directConnection=true&appName=LogRotation
    mongodb://logRotater:LogRotater@<replset1-member2>/admin?directConnection=true&appName=LogRotation
    ...
    mongodb://logRotater:LogRotater@<replset2-member2>/admin?directConnection=true&appName=LogRotation
    mongodb://logRotater:LogRotater@<replset2-member3>/admin?directConnection=true&appName=LogRotation
    
  • Shell script rotate-log.sh will loop through each connection strings and execute logRotation admin command:
    #!/bin/bash
    readarray -t CONNSTRS < conn-strs
    for CONN in ${CONNSTRS[@]}; do
      mongosh ${CONN} --eval "console.log(db.adminCommand({ logRotate: 1 }))" --quiet --norc
    done
    
  • Set up crontab to execute this script every week

After that, depend on your need to access the log, you can implement a different crontab on each server to compress rotated logs:

  • Shell script compress-log.sh will find all log that have timestamp then compress them using zstd
    cd /<path>/<to>/<log-folder>
    zstd -9 --rm -T4 -- $(ls -1 mongod.log.* | grep -v .zst)
    

Hope this can help.

Hi @Billy_Bui @chris the reason being in some startup we don’t require to take a backup and also no need of logs after sometime, in this scenario if mongod.conf provided an option of retain of 30 days. It would be straight forward, now we need to write a serperate code to remove those log files.

Actually this is our use case.

thanks