最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

linux - Can all Docker containers from Docker swarm log into the same log file? - Stack Overflow

programmeradmin0浏览0评论

I've got several Docker containers running together as a Docker swarm. The software running in each of the containers is a Python code. Each of the components writes into their own log. When I debug the whole system, I constantly find myself greping through many logs in parallel trying to arrange the log entries in order...:

> docker container logs -f container1
> docker container logs -f container2
> docker container logs -f container3

Is there a way to make all (or just some) Docker containers log into the same log?

I've got several Docker containers running together as a Docker swarm. The software running in each of the containers is a Python code. Each of the components writes into their own log. When I debug the whole system, I constantly find myself greping through many logs in parallel trying to arrange the log entries in order...:

> docker container logs -f container1
> docker container logs -f container2
> docker container logs -f container3

Is there a way to make all (or just some) Docker containers log into the same log?

Share Improve this question edited Nov 20, 2024 at 6:08 Vinay Sajip 99.6k15 gold badges183 silver badges195 bronze badges asked Nov 19, 2024 at 15:09 DanijelDanijel 8,62019 gold badges84 silver badges148 bronze badges 1
  • 2 you should look into the different log drivers docker offers docs.docker/engine/logging/configure/… if you consolidate the logs you can look through them all in one place. – erik258 Commented Nov 19, 2024 at 15:12
Add a comment  | 

1 Answer 1

Reset to default 0

This is possible, You can redirect all Docker logs into a central file using a custom script that combines docker logs for each container and timestamps them.

#!/bin/bash
output_file="/path/to/centralized-log.log"
containers=$(docker ps --format "{{.Names}}")

for container in $containers; do
    docker logs -f "$container" | sed "s/^/[$container] /" >> "$output_file" &
done

wait

In Docker Swarm, you can use services like Fluentd, Graylog, or a shared space for all logs

version: '3.*'
services:
  app:
    image: my-app
    logging:
      driver: "fluentd"
      options:
        fluentd-address: "fluentd:****"

(Replace the * with numbers that you are using)

发布评论

评论列表(0)

  1. 暂无评论