Skip to content

Commit

Permalink
simplify the template to include all logs from the docker direcotory.
Browse files Browse the repository at this point in the history
This way even if you start the container before all other conatiners the logroatet will always work for all logs.

Remove the size condition form the template as date and size cannot exist in the same tmeplate

Update the documentation with the changes
  • Loading branch information
Krasimir Georgiev committed Mar 26, 2015
1 parent 0d98008 commit 8f42f76
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
20 changes: 8 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ MAINTAINER Michal Raczka [email protected]
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
Expand All @@ -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" ]
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
`docker run -d -v /var/lib/docker:/var/lib/docker michaloo/logrotate`
4 changes: 1 addition & 3 deletions logrotate.tmpl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions start
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8f42f76

Please sign in to comment.