Search code examples
dockerdocker-compose

How do I tail the logs of ALL my docker containers?


I can tail the logs of a single docker container by doing:

docker logs -f container1

But, how can I tail the logs of multiple containers on the same screen?

docker logs container1 container2 

doesn’t work. It gives an error:

“docker logs” requires exactly 1 argument(s).

Thank you.


Solution

  • The docker logs command can't stream multiple logs files.

    docker-compose logs -f can stream logs for a project

    Logging Drivers

    You could use one of the logging drivers other than the default json to ship the logs to a common point. The systemd journald or syslog drivers would readily work on most systems. Any of the other centralised log systems would work too.

    Note that configuring syslog on the Docker daemon means that docker logs command can no longer query the logs, they will only be stored where your syslog puts them.

    A simple daemon.json for syslog:

    {
      "log-driver": "syslog",
      "log-opts": {
        "syslog-address": "tcp://10.8.8.8:514",
        "syslog-format": "rfc5424"
      }
    }
    

    API

    You could write tool that attaches to each container via the API and streams the logs via a websocket. Two of the Java libararies are docker-client and docker-java.

    Hack

    Or run multiple docker logs and munge the output, in node.js:

    const { spawn } = require('child_process')
    
    function run(id){
      let dkr = spawn('docker', [ 'logs', '--tail', '1', '-t', '--follow', id ])
      dkr.stdout.on('data', data => console.log('%s: stdout', id, data.toString().replace(/\r?\n$/,'')))
      dkr.stderr.on('data', data => console.error('%s: stderr', id, data.toString().replace(/\r?\n$/,'')))
      dkr.on('close', exit_code => {
        if ( exit_code !== 0 ) throw new Error(`Docker logs ${id} exited with ${exit_code}`)
      })
    }
        
    let args = process.argv.splice(2)
    args.forEach(arg => run(arg))
    

    Which dumps data as docker logs writes it.

    ○→ node docker-logs.js 958cc8b41cd9 1dad69882b3d db4b844d9478
    958cc8b41cd9: stdout 2018-03-01T06:37:45.152010823Z hello2
    1dad69882b3d: stdout 2018-03-01T06:37:49.392475996Z hello
    db4b844d9478: stderr 2018-03-01T06:37:47.336367247Z hello2
    958cc8b41cd9: stdout 2018-03-01T06:37:55.155137606Z hello2
    db4b844d9478: stderr 2018-03-01T06:37:57.339710598Z hello2
    1dad69882b3d: stdout 2018-03-01T06:37:59.393960369Z hello