nginx-proxy icon indicating copy to clipboard operation
nginx-proxy copied to clipboard

Logrotate

Open azlux opened this issue 5 years ago • 4 comments

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

azlux avatar Jun 21 '20 22:06 azlux

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

ghostiam avatar Sep 07 '20 17:09 ghostiam

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

azlux avatar Apr 17 '21 13:04 azlux

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 ?

Bob-le-pirate avatar Mar 18 '23 10:03 Bob-le-pirate

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

ghostiam avatar Mar 18 '23 19:03 ghostiam