From f2dc81c7c7ca6d63533d941aa9c468a58ce426e8 Mon Sep 17 00:00:00 2001 From: maksii <1761348+maksii@users.noreply.github.com> Date: Mon, 17 Jun 2024 09:05:24 +0300 Subject: [PATCH] docker image improvements --- Dockerfile | 20 +++++++++----------- docker-entrypoint.sh | 10 ++++------ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index bedc3d0..d86a502 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,21 +2,19 @@ FROM python:3.12.4-alpine3.20 # Install system dependencies RUN apk add --no-cache \ - sudo \ bash \ curl \ git \ dcron \ ffmpeg \ - tzdata + tzdata \ + && addgroup -S appgroup \ + && adduser -S appuser -G appgroup -# Set the timezone to Kiev, Ukraine +# Set the timezone ENV TZ=Europe/Kiev RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone -# Create a group and user -RUN addgroup -S appgroup && adduser -S appuser -G appgroup - WORKDIR /app # Install Python dependencies @@ -31,9 +29,12 @@ VOLUME /app/downloads # Set file ownership RUN chown -R appuser:appgroup /app -ENV PORT=5000 -# Copy and prepare the entrypoint +# Setup environment +ENV PORT=5000 \ + CRON_SCHEDULE="0 8 * * *" + +# Prepare the entrypoint COPY docker-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/docker-entrypoint.sh @@ -41,7 +42,4 @@ RUN chmod +x /usr/local/bin/docker-entrypoint.sh USER appuser ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] - -# Set default cron schedule and command to start cron -ENV CRON_SCHEDULE="0 8 * * *" CMD ["crond", "-f", "-d", "8"] \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 8138f99..1f87d85 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -1,18 +1,16 @@ #!/bin/bash -# Ensure environment variables are available +# Check required environment variables if [ -z "$CRON_SCHEDULE" ] || [ -z "$PORT" ]; then echo "Required environment variables are missing." exit 1 fi # Create a cron job file dynamically -echo "$CRON_SCHEDULE cd /app && curl -X POST http://127.0.0.1:$PORT/api/releases/ -H 'x-api-key: your_api_key_here' >> /var/log/cron.log 2>&1" > /etc/cron.d/toloka-job -chmod 0644 /etc/cron.d/toloka-job -crontab -u appuser /etc/cron.d/toloka-job +(crontab -l 2>/dev/null; echo "$CRON_SCHEDULE curl -X POST http://127.0.0.1:$PORT/api/releases/ -H 'x-api-key: your_api_key_here'") | crontab -u appuser - -# Start cron -crond +# Start cron in the foreground to handle logs better +crond -f -L /dev/stdout & # Start the web server exec python -m app \ No newline at end of file