# Base Python image with correct version FROM python:3.12-slim-bookworm # Create non-root user early (HF requirement) RUN useradd -m -u 1000 user # Set up environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ POETRY_VERSION=1.8.4 \ POETRY_HOME=/opt/poetry \ POETRY_CACHE_DIR=/tmp/poetry_cache \ POETRY_NO_INTERACTION=1 \ POETRY_VIRTUALENVS_IN_PROJECT=true \ POETRY_VIRTUALENVS_CREATE=true \ POETRY_REQUESTS_TIMEOUT=15 \ PYTHONPATH=/app WORKDIR /app # Install system dependencies RUN pip install --no-cache-dir "poetry==${POETRY_VERSION}" && \ apt-get update && \ apt-get install -y --no-install-recommends \ gcc g++ libc-dev libffi-dev libgmp-dev libmpfr-dev libmpc-dev \ postgresql postgresql-contrib postgresql-server-dev-all \ curl git nodejs npm && \ rm -rf /var/lib/apt/lists/* # Copy application code COPY . /app/api WORKDIR /app/api # Install Python dependencies RUN pip install --no-cache-dir flask==3.0.1 \ gunicorn==22.0.0 \ gevent==24.11.1 \ celery==5.4.0 \ redis==5.0.3 \ psycopg2-binary==2.9.6 \ sqlalchemy==2.0.29 \ flask-migrate==4.0.5 \ flask-sqlalchemy==3.1.1 # Create and set up entrypoint script RUN echo '#!/bin/bash\n\ set -e\n\ \n\ if [[ "${MIGRATION_ENABLED}" == "true" ]]; then\n\ echo "Running migrations"\n\ cd /app/api && flask db upgrade\n\ fi\n\ \n\ if [[ "${DEBUG}" == "true" ]]; then\n\ exec flask run --host=${DIFY_BIND_ADDRESS:-0.0.0.0} --port=7860 --debug\n\ else\n\ exec gunicorn \\\n\ --bind "0.0.0.0:7860" \\\n\ --workers ${SERVER_WORKER_AMOUNT:-1} \\\n\ --worker-class ${SERVER_WORKER_CLASS:-gevent} \\\n\ --timeout ${GUNICORN_TIMEOUT:-200} \\\n\ --preload \\\n\ app:app\n\ fi' > /entrypoint.sh && \ chmod +x /entrypoint.sh && \ chown user:user /entrypoint.sh # Set up directories and permissions RUN mkdir -p /var/run/postgresql /var/lib/postgresql/data /data/storage && \ chown -R postgres:postgres /var/run/postgresql /var/lib/postgresql/data && \ chmod 2777 /var/run/postgresql && \ chmod 700 /var/lib/postgresql/data && \ chown -R user:user /app # Switch to user USER user # Set up user environment (HF requirement) ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH # Set required environment variables ENV FLASK_APP=/app/api/app.py \ EDITION=SELF_HOSTED \ DEPLOY_ENV=PRODUCTION \ MODE=api \ DB_USERNAME=postgres \ DB_PASSWORD=difyai123456 \ DB_HOST=localhost \ DB_PORT=5432 \ DB_DATABASE=dify \ MIGRATION_ENABLED=true # Expose HF required port EXPOSE 7860 WORKDIR /app/api ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]