Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logrotate #1455

Open
azlux opened this issue Jun 21, 2020 · 4 comments
Open

Logrotate #1455

azlux opened this issue Jun 21, 2020 · 4 comments

Comments

@azlux
Copy link
Contributor

azlux commented Jun 21, 2020

Features Requests

Is that possible to add an environnement variable to enable logrotate ?
I see defaults config files are already present in /etc/logrotate.d

Of course I can create another container to do that, but in this case I need the docker socket to send USR1 to nginx (https://nginx.org/en/docs/control.html). What's your opinion ?

Azlux

@ghostiam
Copy link

ghostiam commented Sep 7, 2020

You can use logrotate with the "copytruncate" option.
I've just made a container and it works the way I wanted it to.

FROM alpine:latest

ENV CRON="*/30 * * * *"

RUN apk --update add logrotate
RUN echo "${CRON} /usr/sbin/logrotate -v /etc/logrotate.conf" >> /etc/crontabs/root && \
    mkdir -p /etc/logrotate.d && \
    echo "/var/log/nginx/*.log" > /etc/logrotate.d/nginx && \
    echo "{" >> /etc/logrotate.d/nginx && \
    echo "  dateext" >> /etc/logrotate.d/nginx && \
    echo "  dateformat -%Y-%m-%d-%s" >> /etc/logrotate.d/nginx && \
    echo "  size 10M" >> /etc/logrotate.d/nginx && \
    echo "  compress" >> /etc/logrotate.d/nginx && \
    echo "  missingok" >> /etc/logrotate.d/nginx && \
    echo "  notifempty" >> /etc/logrotate.d/nginx && \
    echo "  copytruncate" >> /etc/logrotate.d/nginx && \
    echo "  rotate 10" >> /etc/logrotate.d/nginx && \
    echo "}" >> /etc/logrotate.d/nginx

CMD ["crond", "-f"]

docker-compose

version: '2.2'
services:
  logrotate:
    build:
      context: .
      dockerfile: Dockerfile.logrotate
    container_name: nginx-logrotate
    restart: always
    volumes:
      - ./data/logs:/var/log/nginx
    stop_grace_period: 1s

@azlux
Copy link
Contributor Author

azlux commented Apr 17, 2021

I finally use the logrotate on the host. Nginx logs are a mount volume.
Here is the config I use:

azlux@<put-cute-name-here>:~$ cat /etc/logrotate.d/docker-nginx
/<path-to-nginx-volume>/logs/*log {
    daily
    rotate 60
    missingok
    notifempty
    sharedscripts
    compress
    delaycompress
    postrotate
        docker exec -it nginx bash -c "kill -USR1 \`cat /var/run/nginx.pid\`" >/dev/null 2>&1
    endscript
}

I hope that can help others. Maybe this can be copy on some kind of wiki ?
Az

@Bob-le-pirate
Copy link

I want to try the solution proposed by @ghostiam but I can't find how to send kill -USR1 to nginx to rotate the logs. Nginx and Logrotate are not in the same container.
Does anyone can help me ?

@ghostiam
Copy link

@Bob-le-pirate With "copytruncate" option you don't need use kill -USR1

@buchdag buchdag added the kind/feature-request Issue requesting a new feature label Mar 21, 2023
@buchdag buchdag removed the kind/feature-request Issue requesting a new feature label Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants