File size: 2,750 Bytes
b9bc222
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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/api \
    FLASK_APP=/app/api/app.py

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 with correct permissions
COPY --chown=user . /app/api
WORKDIR /app/api

# Install Python dependencies including Flask-Migrate
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 && PYTHONPATH=/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 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"]