log rotation

This commit is contained in:
Paolo Asperti 2024-03-04 18:10:47 +01:00
parent 6e7b4d830a
commit 7d7667f1f9
Signed by: paspo
GPG Key ID: 06D46905D19D5182
3 changed files with 50 additions and 2 deletions

View File

@ -9,7 +9,7 @@ FROM alpine:latest
RUN \
apk -U upgrade && \
apk add nginx curl && \
apk add nginx curl logrotate && \
mkdir -p /wwwroot /logs
COPY docker/rootfs /
@ -24,4 +24,4 @@ RUN nginx -t
HEALTHCHECK --timeout=10s CMD curl --silent --fail -o /dev/null http://127.0.0.1:80/
ENTRYPOINT [ "/usr/sbin/nginx" ]
ENTRYPOINT [ "/bin/sh", "/app/entrypoint.sh" ]

View File

@ -78,3 +78,23 @@ services:
ports:
- 6080:80
```
## Configure log rotation
By default logs are stored in a volume mounted on `/logs` directory inside the container.
The access log file is `/logs/nginx.access.log` and it is rotate daily with a configurable retention, `7 days` by default.
```yaml
version: "3.9"
services:
web:
image: git.libreschool.org/libreschool/libreschool.org:latest
restart: always
container_name: libreschool_website
volumes:
- ./logs:/logs
environment:
LOG_DAYS: 14
ports:
- 6080:80
```

28
docker/rootfs/app/entrypoint.sh Executable file
View File

@ -0,0 +1,28 @@
#!/bin/sh
############ CHECK LOG DIRECTORY
mkdir -p /logs
chown nginx:www-data /logs
chmod 0755 /logs
############ CONFIGURE LOGROTATE
LOG_DAYS=${LOG_DAYS:-7}
############ START CRON
crond -b
cat >/etc/logrotate.d/nginx <<EOF
/logs/nginx.access.log {
missingok
daily
rotate ${LOG_DAYS}
compress
delaycompress
sharedscripts
postrotate
/usr/sbin/nginx -s reopen
endscript
}
EOF
############ START
/usr/sbin/nginx