Roman-Sky
4/20/2018 - 2:00 PM

Store Docker logs

Store Docker logs using journald and logrotate

Host with systemd is required.

FROM debian:9-slim

RUN apt-get update \
    && apt-get install -y \
        python \
        python-systemd \
    && rm -rf /var/lib/apt/lists/*

ENV PYTHONUNBUFFERED=1
COPY journal.py .
CMD ["python", "journal.py"]
import errno    
import os
import signal
from systemd import journal


PROJECT = 'lampa'
CONTAINERS = ['server', 'nginx']

LOG_DIR = '/var/log/containers'


class GracefulKiller:
    kill_now = False

    def __init__(self):
        signal.signal(signal.SIGINT, self.exit_gracefully)
        signal.signal(signal.SIGTERM, self.exit_gracefully)

    def exit_gracefully(self,signum, frame):
        self.kill_now = True


def open_logfile(name):
    return open('{root}/{container}.log'.format(root=LOG_DIR, container=name), 'a', 0)
 
 
if __name__ == '__main__':
    j = journal.Reader()
    logfiles = {}
    for container in CONTAINERS:
        j.add_match('CONTAINER_TAG={project}_{container}'.format(project=PROJECT, container=container))
        logfiles[container] = open_logfile(container)
    j.seek_tail()
    killer = GracefulKiller()
    while True:
        j.wait(1)
        for msg in j:
            project, container = msg['CONTAINER_TAG'].split('.')
            logfiles[container].write(msg['MESSAGE'] + '\n')
        if killer.kill_now:
            break
    for logfile in logfiles.values():
        logfile.close()
version: "2"

services:
  nginx:
    image: nginx:alpine
    ports:
      - "8005:80"
    logging:
      driver: journald
      options:
        tag: "lampa.nginx"
  journal:
    build: .
    volumes:
      - /run/log/journal:/run/log/journal
      - /etc/machine-id:/etc/machine-id
      - ./log:/var/log/containers