How to rotate nginx logs inside a docker container
- Update your docker mounts, add this line into volumes mount list:
- /var/log/nginx:/var/log/nginx
-
Restart the container. Make sure that /var/log/nginx has access.log and error.log files (if you didn’t forget to specify them in the nginx.conf).
-
Tune your logrotate. I assume, you’re using standard logrotate package. Here I use just
docker, but I highly recommend to use full path to the docker binary. Create new logrotate config like this, replacing NAME-OF_MY-IMAGE by actual name. Or rundocker psand look into the second column.
/var/log/nginx/*.log {
compress
missingok
nocopytruncate
delaycompress
nomail
notifempty
noolddir
dateext
rotate 5
sharedscripts
size=10M
postrotate
docker exec -t $(docker ps | awk '/NAME-OF-MY-IMAGE/ {print $1}') nginx -s reopen
endscript
}
OK! It should work now!