File size: 2,664 Bytes
38fb5cc
4e4e60f
38fb5cc
4e4e60f
 
 
 
38fb5cc
 
 
 
336f29d
38fb5cc
 
4e4e60f
 
8a21f91
4e4e60f
7d9154c
4e4e60f
38fb5cc
 
 
 
7c3de99
 
38fb5cc
 
4e4e60f
1f76095
4e4e60f
73efd4a
 
4e4e60f
 
 
 
 
 
 
 
 
73efd4a
4e4e60f
73efd4a
 
 
 
 
4e4e60f
73efd4a
 
 
 
 
 
 
 
 
 
 
 
7a1cc79
 
 
570d40a
7a1cc79
4e4e60f
7a1cc79
 
 
4e4e60f
7a1cc79
 
 
 
 
 
 
0c01f90
38fb5cc
4e4e60f
89b26ae
 
38fb5cc
 
 
 
 
 
 
89b26ae
38fb5cc
 
89b26ae
4e4e60f
570d40a
38fb5cc
 
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

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"]