Skip to content

Commit

Permalink
docker image improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
maksii committed Jun 17, 2024
1 parent a61fa23 commit f2dc81c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
20 changes: 9 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,17 +29,17 @@ 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

# Switch to the non-root user
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"]
10 changes: 4 additions & 6 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f2dc81c

Please sign in to comment.