Logrotate - nginx logs not rotating inside docker container
As stated on the edit on my question the problem was that CMD
from nginx:1.11
was only starting the nginx
process. A work around is to place the following command on my Dockerfile
CMD service cron start && nginx -g 'daemon off;'
This will start nginx as nginx:1.11
starts it and well as start the cron service.
The Dockerfile would look something like:
FROM nginx:1.11# Remove sym links from nginx imageRUN rm /var/log/nginx/access.logRUN rm /var/log/nginx/error.log# Install logrotateRUN apt-get update && apt-get -y install logrotate# Copy MyApp nginx configCOPY config/nginx.conf /etc/nginx/nginx.conf#Copy logrotate nginx configurationCOPY config/logrotate.d/nginx /etc/logrotate.d/# Start nginx and cron as a serviceCMD service cron start && nginx -g 'daemon off;'
i found a method from this link, its core idea is use logrotate
outside, and conf is as below:
$ sudo vi /etc/logrotate.d/test/home/test/logs/*log { rotate 90 missingok ifempty sharedscripts compress postrotate /usr/bin/docker exec nginx-test /bin/sh -c '/usr/sbin/nginx -s reopen > /dev/null 2>/dev/null' endscript}
docker exec --user root `docker container ls --filter "name=nginx" --format "{{.Names}}"` nginx -s reopen