4

I have a rails app running through docker. I bring up the app with docker-compose (config below). The whole app covers a mysql, redis, rails (including sidekiq workers), nginx (with react frontend) and a rsyslog server, which forwards all logs to Loggly

My problem is realated to the logging part. Logs from all services basically gets forwarded, except from my logs from sidekiq. When I start sidekiq with bundler exec sidekiq the logs are output to the console (stdout), but I dont see them when I tail /var/log/messages on the rsyslog server.

I followed this tutorial when setting up the logging on docker:

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

My thinking is that everything that goes to stdout in a docker container should be logged to the docker log mechanism - and with my setup that means it should go to the syslog server...

The docker-compose file:

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:
martin
  • 3,289
  • 5
  • 22
  • 27
  • 1
    Are you starting the app with bundler or rails? I see `rails s -p 3000 -b 0.0.0.0` – Robert Jun 07 '17 at 13:40
  • @Robert good point... i haven't worked sidekiq into the docker script for now. I am starting the rails app as per your comment, and I start sidekiq by logging into the container and starting it: `docker exec -it 8d092f6b4d55 bundle exec sidekiq` – martin Jun 07 '17 at 14:41
  • 1
    That is the key. The output of `bundle` is redirected to your terminal instead of the container stdout. You will need to run bundle inside your command or entrypoint. So I guess you need to run both rails and bundle there. – Robert Jun 07 '17 at 14:48

1 Answers1

2

As suggested by @Robert in the comments the solution is to run bundle commands as part of the CMD in the docker-compose file:

The api service in my compose file thus becomes:

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"
martin
  • 3,289
  • 5
  • 22
  • 27