diff --git a/Dockerfile b/Dockerfile index 556440e..69cffa2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,10 +5,6 @@ MAINTAINER Michal Raczka me@michaloo.net RUN apt-get update \ && apt-get install -y curl -# install docker-gen -RUN curl -sL https://github.com/jwilder/docker-gen/releases/download/0.3.2/docker-gen-linux-amd64-0.3.2.tar.gz \ - | tar -xz -C /usr/local/bin - # install go-cron RUN curl -sL https://github.com/michaloo/go-cron/releases/download/v0.0.2/go-cron.tar.gz \ | tar -x -C /usr/local/bin @@ -18,21 +14,21 @@ ADD . /app WORKDIR /app # clear logrotate ubuntu installation and modify logrotate script -# add docker-gen execution and enable debug mode -RUN rm /etc/logrotate.d/* && \ - sed -i \ +# add the template and enable debug mode +RUN rm /etc/logrotate.d/* \ + && cp /app/logrotate.tmpl /etc/logrotate.d/docker \ + && chmod 0644 /etc/logrotate.d/docker \ + && sed -i \ -e 's/^\/usr\/sbin\/logrotate.*/\/usr\/sbin\/logrotate \-v \/etc\/logrotate.conf/' \ - -e '/\#\!\/bin\/sh/a /usr/local/bin/docker-gen /root/logrotate.tmpl /etc/logrotate.d/docker' \ /etc/cron.daily/logrotate - + #&& apt-get clean \ + #&& rm -rf /var/lib/apt/lists/* # set default configuration -ENV DOCKER_HOST unix:///var/run/docker.sock ENV DOCKER_DIR /var/lib/docker/ ENV GOCRON_SCHEDULER 0 0 * * * * ENV LOGROTATE_MODE daily -ENV LOGROTATE_ROTATE 3 -ENV LOGROTATE_SIZE 512M +ENV LOGROTATE_ROTATE 7 ENTRYPOINT [ "/bin/bash" ] CMD [ "/app/start" ] diff --git a/README.md b/README.md index 3d90923..4c40dbe 100644 --- a/README.md +++ b/README.md @@ -2,20 +2,19 @@ Docker image with golang utilities: -- [docker-gen](https://github.com/jwilder/docker-gen) - [go-cron](https://github.com/michaloo/go-cron) which rotate logs from all docker containers. -By default it runs every hour and rotate files daily or when they exceed 512M filesize. +By default it runs every hour and rotate files daily. Logrotate interval is controlled by `GOCRON_SCHEDULER` environmental variable according to [golang cron package format](http://godoc.org/github.com/robfig/cron#hdr-CRON_Expression_Format). -Rotation config is created using `/app/logrotate.tmpl` docker-gen template and could be easily overwritten in custom Dockerfile: +Rotation config is created using `/app/logrotate.tmpl` and could be easily overwritten in custom Dockerfile: `ADD my-custom-logrotate-template.tmpl /app/logrotate.tmpl` # Example usage -`docker run -d -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker michaloo/logrotate` \ No newline at end of file +`docker run -d -v /var/lib/docker:/var/lib/docker michaloo/logrotate` \ No newline at end of file diff --git a/logrotate.tmpl b/logrotate.tmpl index 7241e21..cea4049 100644 --- a/logrotate.tmpl +++ b/logrotate.tmpl @@ -1,10 +1,8 @@ -{{ range $index, $value := $ }} -/var/lib/docker/containers/{{ $value.ID }}/{{ $value.ID }}-json.log{{ end }} +/var/lib/docker/containers/*/*.log { {{LOGROTATE_MODE}} missingok rotate {{LOGROTATE_ROTATE}} - size {{LOGROTATE_SIZE}} compress notifempty copytruncate diff --git a/start b/start index 180e9d2..1e15a5d 100644 --- a/start +++ b/start @@ -2,8 +2,7 @@ sed -e 's/{{LOGROTATE_MODE}}/'"$LOGROTATE_MODE"'/' \ -e 's/{{LOGROTATE_ROTATE}}/'"$LOGROTATE_ROTATE"'/' \ - -e 's/{{LOGROTATE_SIZE}}/'"$LOGROTATE_SIZE"'/' \ - /app/logrotate.tmpl > /root/logrotate.tmpl + /app/logrotate.tmpl > /etc/logrotate.d/docker exec /usr/local/bin/go-cron "$GOCRON_SCHEDULER" /etc/cron.daily/logrotate