From de7531e9110c5492dad0480891b70f4ff16c34ae Mon Sep 17 00:00:00 2001 From: Flook Date: Tue, 28 Apr 2026 20:12:16 +0700 Subject: [PATCH] chore: implement self-healing entrypoint and fix line endings --- Dockerfile | 17 +++++++++-------- entrypoint.sh | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 949774e..3a96b13 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,19 +35,20 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . # ========================= -# Collect Static Files +# Setup Entrypoint (เพิ่มตรงนี้ครับ) # ========================= -RUN python manage.py collectstatic --noinput +# แนะนำให้ใช้คำสั่งนี้เพื่อให้มั่นใจว่าสิทธิ์ไฟล์ถูกต้องใน Linux environment +RUN chmod +x /app/entrypoint.sh # ========================= -# Expose Application Port +# Environment Variables (Optional) # ========================= EXPOSE 8000 # ========================= -# Run Application +# ENTRYPOINT & CMD # ENTRYPOINT จะรันสคริปต์เพื่อ Migrate ฐานข้อมูลก่อนเสมอ # ========================= -# ========================= -# Run Application -# ========================= -CMD ["python", "-m", "gunicorn", "core.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3", "--timeout", "120"] \ No newline at end of file +ENTRYPOINT ["/app/entrypoint.sh"] + +# CMD จะถูกส่งไปเป็นพารามิเตอร์ $1 ให้กับ entrypoint.sh +CMD ["gunicorn"] \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..c3aa1a1 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,36 @@ +#!/bin/bash +set -e + +# ฟังก์ชันสำหรับเตรียมระบบ Backend +prepare_backend() { + echo "--- Collecting static files ---" + python manage.py collectstatic --noinput + + echo "--- Applying database migrations ---" + python manage.py migrate --noinput +} + +# ตรวจสอบพารามิเตอร์ที่ส่งมาจาก Kubernetes Deployment (command/args) +if [ "$1" = "gunicorn" ]; then + prepare_backend + + # ดึงค่า PORT และ WORKERS จาก Env (ถ้ามี) หรือใช้ค่า Default + APP_PORT=${PORT:-8000} + WORKERS=${GUNICORN_WORKERS:-3} + + echo "Starting Gunicorn on port $APP_PORT with $WORKERS workers..." + exec gunicorn --bind 0.0.0.0:$APP_PORT \ + --workers $WORKERS \ + --access-logfile - \ + --error-logfile - \ + core.wsgi:application + +elif [ "$1" = "celery" ]; then + echo "Starting Celery Worker..." + exec celery -A core worker -l info + +else + # สำหรับคำสั่งอื่นๆ เช่น python manage.py createsuperuser + echo "Executing custom command: $@" + exec "$@" +fi \ No newline at end of file