Docker Compose Tailing of Log Files

Docker Compose Logs

docker compose logs aggregates stdout of containers started with no out-of-the-box capability for aggregating additional logs generated on container filesystems. Implementing sidecar allows tailing logs generated on container volumes.

docker-compose.yaml

services:
  counter:
    image: busybox
    command: |
            /bin/sh -c 'i=0; while true; do echo "$$(date) WARN $$i" >> /var/log/1.log; echo "$$(date) INFO $$i" >> /var/log/2.log; i=$$((i+1)); sleep 5; done'
    volumes:
    - log-volume:/var/log
  counter-log:
    image: busybox
    command: |
            /bin/sh -c 'tail -n+1 -F /var/log/*.log'
    volumes:
    - log-volume:/var/log

volumes:
  log-volume:
    name: counter-log-volume

Running

➜ docker compose up
[+] Running 4/4
 ⠿ Network 202204_docker_default          Created                                                   0.1s
 ⠿ Volume "counter-log-volume"            Created                                                   0.0s
 ⠿ Container 202204_docker-counter-log-1  Created                                                   0.1s
 ⠿ Container 202204_docker-counter-1      Created                                                   0.1s
Attaching to 202204_docker-counter-1, 202204_docker-counter-log-1
202204_docker-counter-log-1  | ==> /var/log/1.log <==
202204_docker-counter-log-1  | Sat Apr 23 01:37:03 UTC 2022 WARN 0
202204_docker-counter-log-1  | 
202204_docker-counter-log-1  | ==> /var/log/2.log <==
202204_docker-counter-log-1  | Sat Apr 23 01:37:03 UTC 2022 INFO 0
202204_docker-counter-log-1  | 
202204_docker-counter-log-1  | ==> /var/log/1.log <==
202204_docker-counter-log-1  | Sat Apr 23 01:37:08 UTC 2022 WARN 1
202204_docker-counter-log-1  | 
202204_docker-counter-log-1  | ==> /var/log/2.log <==
202204_docker-counter-log-1  | Sat Apr 23 01:37:08 UTC 2022 INFO 1
^C

Cleanup

docker compose down
docker volume rm counter-log-volume
comments powered by Disqus

Related