From e90ecc020e1b5c0646fe9fa5f1ded8b3b234c9fa Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Fri, 10 May 2024 23:09:33 +0200 Subject: [PATCH] Added cleaning logs to discard older logs as not need while developing --- docker/php/Dockerfile | 10 +++++++++- docker/php/clean_logs.sh | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 docker/php/clean_logs.sh diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile index 1583ab7b2..73c122e5a 100644 --- a/docker/php/Dockerfile +++ b/docker/php/Dockerfile @@ -3,7 +3,7 @@ FROM debian:bookworm # Install apache2 and PHP and PHP modules RUN apt update && \ - apt install -y apache2 curl unzip php8.2 php8.2-curl php8.2-cli php8.2-mysqli php8.2-gd php8.2-zip php8.2-dom php8.2-mbstring && \ + apt install -y apache2 curl cron unzip php8.2 php8.2-curl php8.2-cli php8.2-mysqli php8.2-gd php8.2-zip php8.2-dom php8.2-mbstring && \ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \ apt clean && \ rm -rf /var/lib/apt/lists/* @@ -17,5 +17,13 @@ WORKDIR /var/www/html COPY ../../composer.json ./ RUN composer install --no-interaction +COPY ./docker/php/clean_logs.sh /root/clean_logs.sh +RUN chmod +x /root/clean_logs.sh + +# Add cron job for log cleanup +RUN echo "* * * * * /root/clean_old_logs.sh" > /etc/cron.d/clean_old_logs && \ + chmod 0644 /etc/cron.d/clean_old_logs && \ + crontab /etc/cron.d/clean_old_logs + # Start Apache in the foreground CMD ["apachectl", "-D", "FOREGROUND"] diff --git a/docker/php/clean_logs.sh b/docker/php/clean_logs.sh new file mode 100644 index 000000000..4f8ae3420 --- /dev/null +++ b/docker/php/clean_logs.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Variables +LOG_DIR="/var/log/apache2" +ERROR_LOG="${LOG_DIR}/error.log" +ACCESS_LOG="${LOG_DIR}/access.log" +LINES_TO_KEEP=50 + +# Truncate logs to keep only the last 50 lines +tail -n $LINES_TO_KEEP $ERROR_LOG > ${ERROR_LOG}.tmp && mv ${ERROR_LOG}.tmp $ERROR_LOG +tail -n $LINES_TO_KEEP $ACCESS_LOG > ${ACCESS_LOG}.tmp && mv ${ACCESS_LOG}.tmp $ACCESS_LOG