Anthony Taylor
Anthony Taylor

Reputation: 3383

View logs for all docker containers simultaneously

I currently use docker for my backend, and when I first start them up with

docker-compose up

I get log outputs of all 4 dockers at once, so I can see how they are interacting with each other when a request comes in. Looking like this, one request going from nginx to couchdb

http://i.imgur.com/E4GQ43F.png

The issue is now that I am running on GCE with load balancing, when a new VM spins up, it auto starts the dockers and runs normally, I would like to be able to access a load balanced VM and view the live logs, but I can not get docker to allow me this style, when I use logs, it gives me normal all white font with no label of where it came from.

Using

docker events  

does nothing, it won't return any info.

tldr; what is the best way to obtain a view, same as the log output you get when running "docker-compose up"

Upvotes: 69

Views: 85615

Answers (6)

user1707414
user1707414

Reputation: 4133

Make sure that the last process stays in foreground.

(trap 'kill 0' SIGINT; docker logs -f container1 & docker logs -f container2 & docker logs -f container3)

Upvotes: 1

Yuri Astrakhan
Yuri Astrakhan

Reputation: 9955

You can see the logs for all running containers with

docker ps -q | xargs -L 1 docker logs

This may work with the --follow too if xargs is ran with -P <count>, where the count is higher than the number of running containers (thx @kisna for the example):

docker ps -q | xargs -L 1 -P `docker ps | wc -l` docker logs --since 30s -f

Upvotes: 85

StackzOfZtuff
StackzOfZtuff

Reputation: 3106

Try "watch"

Here's a quick and dirty multitail/xtail for docker containers.

watch 'docker ps --format "{{.Names}}" | sort | xargs --verbose --max-args=1 -- docker logs --tail=8 --timestamps'

How this works:

  • watch to run every few seconds
    • docker ps --format "{{.Names}}" to get the names of all running containers
    • sort to sort them
    • xargs to give these names to docker logs:
      • docker logs to print the actual logs

Adjust parameter "--tail=8" as needed so that everything still fits on one screen.

The "xargs" methods listed above (in another user's answer) will stop working as containers are stopped and restarted. This "watch" method here does not have that problem. (But it's not great either.)

Upvotes: 9

Christoffer Nissen
Christoffer Nissen

Reputation: 614

If you are using Docker Swarm, you can find your services by

docker service ls

Grap the id, and then run

docker service logs $ID -f 

if the service is defined with tty: true, then you must run with the --raw flag. Notice, this wont tell you which container is giving the outputted log entry.

Upvotes: 4

BoeroBoy
BoeroBoy

Reputation: 1200

I use a variation of this to live tail (--follow) all logs and indicate which log is tailing at the time. This bash includes both stdout and stderr. Note you may need to purge the /tmp dir of *.{log,err} afterwards.

for c in $(docker ps -a --format="{{.Names}}")
do
       docker logs -f $c > /tmp/$c.log 2> /tmp/$c.err &
done
tail -f /tmp/*.{log,err}

Hope this helps. Logging has become so problematic these days, and other get-off-my-lawn old man rants...

Upvotes: 24

Anthony Taylor
Anthony Taylor

Reputation: 3383

If using docker-compose, you use

docker-compose logs --tail=0 --follow

instead of

docker logs --tail=0 --follow

This will get the output I was originally looking for.

Upvotes: 102

Related Questions