diff --git a/Dockerfile b/Dockerfile index 39df2d3..c904d71 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,11 +4,17 @@ FROM python:3.9-slim # Set working directory WORKDIR /app +# Set build-time arguments +ARG FLASK_ENV=production +ARG FLASK_APP=app.py +ARG STORAGE_URL=file:///app/static/uploads + # Set environment variables -ENV PYTHONDONTWRITEBYTECODE 1 -ENV PYTHONUNBUFFERED 1 -ENV FLASK_APP=app.py -ENV FLASK_ENV=production +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + FLASK_APP=${FLASK_APP} \ + FLASK_ENV=${FLASK_ENV} \ + STORAGE_URL=${STORAGE_URL} # Install system dependencies RUN apt-get update \ diff --git a/config.py b/config.py index 6e4b96a..2b158fc 100644 --- a/config.py +++ b/config.py @@ -1,8 +1,9 @@ import os from dotenv import load_dotenv -# Load environment variables from .env file -load_dotenv() +# Only load .env file if we're in development +if os.environ.get('FLASK_ENV') != 'production': + load_dotenv() BASE_DIR = os.path.abspath(os.path.dirname(__file__)) diff --git a/entrypoint.sh b/entrypoint.sh index c72325d..f4d3993 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,5 +1,22 @@ #!/bin/bash +# Function to check required environment variables +check_required_vars() { + local missing_vars=0 + for var in "$@"; do + if [ -z "${!var}" ]; then + echo "Error: Required environment variable $var is not set" + missing_vars=1 + fi + done + if [ $missing_vars -eq 1 ]; then + exit 1 + fi +} + +# Check required environment variables (add your required variables here) +check_required_vars "FLASK_APP" "FLASK_ENV" "STORAGE_URL" + # Wait for database to be ready (if using PostgreSQL) # until PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -U "$DATABASE_USER" -d "$DATABASE_NAME" -c '\q'; do # echo "Waiting for database..." @@ -9,5 +26,9 @@ # Apply database migrations flask db upgrade -# Start gunicorn -exec gunicorn --bind 0.0.0.0:5000 app:app +# Start gunicorn with proper environment handling +exec gunicorn --bind 0.0.0.0:5000 \ + --env FLASK_APP=${FLASK_APP} \ + --env FLASK_ENV=${FLASK_ENV} \ + --env STORAGE_URL=${STORAGE_URL} \ + app:app