从 Docker 记录

Logging from Docker

我有一个 rails 应用 运行 到 docker。我使用 docker-compose(下面的配置)调出应用程序。整个应用程序涵盖 mysql、redis、rails(包括 sidekiq worker)、nginx(带有 React 前端)和一个 rsyslog 服务器,它将所有日志转发到 Loggly

我的问题与日志部分有关。来自所有服务的日志基本上都会被转发,除了我来自 sidekiq 的日志。 当我使用 bundler exec sidekiq 启动 sidekiq 时,日志会输出到控制台(stdout),但是当我在 rsyslog 服务器上拖尾 /var/log/messages 时,我看不到它们。

我在 docker 上设置登录时遵循了本教程:

https://medium.com/@yoanis_gil/logging-with-docker-part-1-b23ef1443aac

我的想法是,在 docker 容器中进入 stdout 的所有内容都应该记录到 docker 日志机制中——根据我的设置,这意味着它应该进入系统日志服务器。 .

docker-撰写文件:

version: '3'

services:
  nginx:
    image: nginx
    depends_on:
      - api
      - syslog
    ports:
      - "80:8080"

    logging:
      driver: syslog
      options:
        syslog-facility: "daemon"
        tag: "nginx"
        syslog-address: "tcp://localhost:5514"
    networks:
      - phototankswarm
    env_file: .env.dev
    volumes:
      - ./frontend/nginx/conf.d:/etc/nginx/conf.d
      - ./frontend/public:/www

  db:
    image: mysql
    env_file: .env.dev
    depends_on:
      - syslog
    networks:
      - phototankswarm
    ports:
      - "3306:3306"
    volumes:
      - ./backend/sql/data:/var/lib/mysql
    logging:
      driver: syslog
      options:
        syslog-facility: "daemon"
        tag: "mysql"
        syslog-address: "tcp://localhost:5514"

  redis:
    image: redis
    depends_on:
      - syslog
    networks:
      - phototankswarm
    logging:
      driver: syslog
      options:
        syslog-facility: "daemon"
        tag: "redis"
        syslog-address: "tcp://localhost:5514"

  api:
    image: pt-rails
    env_file: .env.dev
    networks:
      - phototankswarm
    command: >
      sh -c '
      rails s -p 3000 -b 0.0.0.0;
      '
    volumes:
      - /Users/martinhinge/Pictures/docker/phototank:/media/phototank
      - ./backend:/usr/src/app
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
      - syslog
    logging:
      driver: syslog
      options:
        syslog-facility: "daemon"
        tag: "rails"
        syslog-address: "tcp://localhost:5514"

  syslog:
    image: syslog
    ports:
      - "localhost:5514:514"
    networks:
      - phototankswarm
    volumes:
      - /tmp:/var/log/syslog

networks:
  phototankswarm:

正如@Robert 在评论中所建议的那样,解决方案是 运行 将命令作为 docker-compose 文件中 CMD 的一部分:

我的撰写文件中的 api 服务因此变为:

api:
    image: pt-rails
    env_file: .env.dev
    networks:
      - phototankswarm
    command: >
      sh -c '
      bundle exec sidekiq -d -L /dev/stdout && bundle exec rails s -p 3000 -b 0.0.0.0  
      '
    volumes:
      - /Users/martinhinge/Pictures/docker/phototank:/media/phototank
      - ./backend:/usr/src/app
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
      - syslog
    logging:
      driver: syslog
      options:
        syslog-facility: "daemon"
        tag: "rails"
        syslog-address: "tcp://localhost:5514"