Logrotate - nginx logs do not rotate inside docker container

I have a docker container running nginx that writes logs to /var/log/nginx Logrotate is installed in the docker container and the logrotate configuration file for nginx is configured correctly. However, logs are not automatically rotated using logrotate. Manual forced log rotation to rotate logs through logrotate -f /path/to/conf-file works as expected.

My conclusion is that something does not start cron, but I can not find the reason.

Here's the Dockerfile for the docker container running nginx:

 FROM nginx:1.11 # Remove sym links from nginx image RUN rm /var/log/nginx/access.log RUN rm /var/log/nginx/error.log # Install logrotate RUN apt-get update && apt-get -y install logrotate # Copy MyApp nginx config COPY config/nginx.conf /etc/nginx/nginx.conf #Copy logrotate nginx configuration COPY config/logrotate.d/nginx /etc/logrotate.d/ 

And the docker-compose file:

 version: '2' services: nginx: build: ./build restart: always ports: - "80:80" - "443:443" volumes: - ./auth:/etc/nginx/auth - ./certs:/etc/nginx/certs - ./conf:/etc/nginx/conf - ./sites-enabled:/etc/nginx/sites-enabled - ./web:/etc/nginx/web - nginx_logs:/var/log/nginx logging: driver: "json-file" options: max-size: "100m" max-file: "1" volumes: nginx_logs: networks: default: external: name: my-network 

Here is the content: /etc/logrotate.d/nginx

 /var/log/nginx/*.log { daily dateext missingok rotate 30 compress delaycompress notifempty create 0640 www-data adm sharedscripts prerotate if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ run-parts /etc/logrotate.d/httpd-prerotate; \ fi \ endscript postrotate [ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid` endscript } 

Content /etc/cron.daily/logrotate

 #!/bin/sh test -x /usr/sbin/logrotate || exit 0 /usr/sbin/logrotate /etc/logrotate.conf 

Contents of /etc/logrotate.conf

 # see "man logrotate" for details # rotate log files weekly weekly # keep 4 weeks worth of backlogs rotate 4 # create new (empty) log files after rotating old ones create # uncomment this if you want your log files compressed #compress # packages drop log rotation information into this directory include /etc/logrotate.d # no packages own wtmp, or btmp -- we'll rotate them here /var/log/wtmp { missingok monthly create 0664 root utmp rotate 1 } /var/log/btmp { missingok monthly create 0660 root utmp rotate 1 } # system-specific logs may be configured here 

Can someone point me in the right direction why nginx logs don't automatically rotate using logrotate?

EDIT

I can trace the root cause of this problem to the cron service, which does not start in the container. A possible solution is to find a way to make the container run both the nginx and cron services at the same time.

+5
source share
1 answer

As pointed out by the editors of my question, the problem was that the CMD from nginx:1.11 only started the nginx process. The work around is placing the following command in my Dockerfile

 CMD service cron start && nginx -g 'daemon off;' 

This will start nginx when nginx:1.11 starts it and starts the cron service.

The Docker file will look something like this:

 FROM nginx:1.11 # Remove sym links from nginx image RUN rm /var/log/nginx/access.log RUN rm /var/log/nginx/error.log # Install logrotate RUN apt-get update && apt-get -y install logrotate # Copy MyApp nginx config COPY config/nginx.conf /etc/nginx/nginx.conf #Copy logrotate nginx configuration COPY config/logrotate.d/nginx /etc/logrotate.d/ # Start nginx and cron as a service CMD service cron start && nginx -g 'daemon off;' 
+2
source

Source: https://habr.com/ru/post/1271984/


All Articles