File size: 8,205 Bytes
0df77ff
 
 
51364f2
7ce37bf
9d4903c
 
 
9f4c1e4
7752907
 
 
 
fd3e8ba
 
 
 
7752907
fd3e8ba
 
 
7752907
 
 
 
 
fd3e8ba
 
 
 
 
51364f2
9d4903c
302aead
 
 
9d4903c
302aead
 
 
 
 
7752907
 
302aead
9d4903c
 
 
90f57a2
7752907
90f57a2
9d4903c
 
 
 
 
7752907
 
 
9d4903c
 
 
 
 
 
 
 
 
 
90f57a2
7752907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64cf3a5
0df77ff
b2b04b8
0df77ff
1f54115
0c0d5e5
64cf3a5
 
51364f2
64cf3a5
 
0ae370c
116ec26
64cf3a5
 
b2b04b8
5fc6cff
 
 
 
b2b04b8
 
0df77ff
b2b04b8
0df77ff
51364f2
0c0d5e5
64cf3a5
 
51364f2
64cf3a5
0df77ff
64cf3a5
1f54115
74adc3c
c3121a2
64cf3a5
b2b04b8
0c0d5e5
0df77ff
51364f2
0df77ff
51364f2
496ca32
51364f2
0848c81
 
fbd2dcf
0848c81
 
 
443bf8a
302aead
 
 
 
 
 
 
 
 
 
 
 
 
 
74adc3c
302aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51364f2
496ca32
9f4c1e4
0404cc9
61e7c18
a4bef0b
64cf3a5
 
9f4c1e4
64cf3a5
5fc6cff
 
 
9f4c1e4
45ac7d0
0404cc9
51364f2
7ce37bf
 
 
e3e3266
51364f2
a300380
08efa7b
 
 
 
 
 
 
 
 
 
 
 
 
51364f2
 
 
 
443bf8a
51364f2
61e7c18
0df77ff
51364f2
 
c7a62ec
fbd2dcf
 
 
0404cc9
443bf8a
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# ============================================
# Base stage for shared configuration
# ============================================
FROM python:3.10-slim-bookworm AS base

# Force package update and installation at build time
ARG CACHEBUST=1
ARG DEBIAN_FRONTEND=noninteractive

# Create directory for Node.js setup
RUN mkdir -p /etc/apt/keyrings

# Install Node.js and npm with all dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        curl \
        gnupg \
        ca-certificates \
    && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
    && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        nodejs \
        build-essential \
        python3-dev \
    && npm install -g npm@latest \
    && npm install -g yarn \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Install system dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        build-essential \
        gcc \
        g++ \
        libc6-dev \
        libffi-dev \
        libgmp-dev \
        libmpfr-dev \
        libmpc-dev \
        libssl-dev \
        postgresql-client \
        redis-tools \
        make \
        pkg-config \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Install Python packages in smaller groups to better handle dependencies
RUN pip install --no-cache-dir \
    gunicorn \
    gevent \
    grpcio \
    pydantic-settings \
    protobuf \
    grpcio-tools

RUN pip install --no-cache-dir \
    flask \
    flask-cors \
    Flask-SQLAlchemy==3.1.1 \
    Flask-Migrate==4.0.7 \
    flask-login \
    flask-restful \
    flask-limiter \
    flask-caching \
    flask-jwt-extended \
    flask-socketio

RUN pip install --no-cache-dir \
    PyYAML \
    celery \
    redis \
    psycopg2-binary \
    sqlalchemy \
    alembic \
    pyjwt \
    requests

# Install ML and AI packages separately
RUN pip install --no-cache-dir \
    numpy \
    pandas \
    python-dotenv \
    pycryptodome \
    cryptography \
    bcrypt \
    python-jose[cryptography] \
    passlib \
    python-multipart \
    gmpy2

RUN pip install --no-cache-dir \
    transformers \
    torch \
    tensorflow \
    sentencepiece \
    tokenizers \
    nltk

# Install remaining packages
RUN pip install --no-cache-dir \
    openai==1.14.0 \
    anthropic==0.23.1 \
    Pillow \
    opencv-python-headless \
    scikit-learn \
    scipy \
    google-cloud-aiplatform \
    google-generativeai \
    vertexai \
    google-cloud-core \
    google-api-core \
    yarl \
    aiohttp \
    "tritonclient[all]" \
    cohere==4.43 \
    replicate \
    aleph-alpha-client \
    stability-sdk \
    huggingface_hub \
    langchain \
    langchain-community \
    langchain-core \
    langchain-openai

# Install NLTK data
RUN python -c "import nltk; nltk.download('punkt'); nltk.download('averaged_perceptron_tagger')"

# Verify installations
RUN node --version && npm --version && yarn --version

# ============================================
# Web builder stage - optimized
# ============================================
FROM base AS web-builder

WORKDIR /app

# Copy web directory first
COPY web/ web/

WORKDIR /app/web

# Install dependencies and build
RUN yarn install --frozen-lockfile && \
    yarn add --dev autoprefixer postcss tailwindcss code-inspector-plugin && \
    NEXT_TELEMETRY_DISABLED=1 yarn build && \
    mkdir -p .next/standalone && \
    cp -r .next/static .next/standalone/.next/ && \
    cp -r public .next/standalone/ && \
    yarn cache clean

# ============================================
# Python builder stage - optimized
# ============================================
FROM base AS python-builder

WORKDIR /app

# Copy api directory
COPY api/ api/

WORKDIR /app/api

# Install core dependencies first
RUN pip install --no-cache-dir poetry==1.8.3 && \
    poetry config virtualenvs.create false && \
    poetry install --no-dev --no-interaction --no-ansi

# ============================================
# Final stage - minimal runtime
# ============================================
FROM base

# Create non-root user and storage directory
RUN apt-get update && \
    useradd -m -u 1000 user && \
    mkdir -p /storage/files /storage/cache /storage/logs && \
    chown -R user:user /storage && \
    mkdir -p /app/api && \
    chown -R user:user /app

# Install runtime dependencies with proper repository update
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        build-essential \
        nodejs \
        npm \
        libgmp-dev \
        libmpfr-dev \
        libmpc-dev \
        libssl-dev \
        postgresql-client \
        redis-tools && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    pip install --no-cache-dir \
        gunicorn \
        gevent \
        grpcio \
        pydantic-settings \
        protobuf \
        grpcio-tools \
        flask \
        flask-cors \
        Flask-SQLAlchemy==3.1.1 \
        Flask-Migrate==4.0.7 \
        flask-login \
        flask-restful \
        flask-limiter \
        flask-caching \
        flask-jwt-extended \
        flask-socketio \
        PyYAML \
        celery \
        redis \
        psycopg2-binary \
        sqlalchemy \
        alembic \
        pyjwt \
        requests \
        numpy \
        pandas \
        python-dotenv \
        pycryptodome \
        cryptography \
        bcrypt \
        python-jose[cryptography] \
        passlib \
        python-multipart \
        gmpy2 \
        transformers \
        torch \
        tensorflow \
        sentencepiece \
        tokenizers \
        nltk \
        openai==1.14.0 \
        anthropic==0.23.1 \
        flask-migrate==4.0.5 \
        Pillow \
        opencv-python-headless \
        scikit-learn \
        scipy \
        google-cloud-aiplatform \
        google-generativeai \
        vertexai \
        google-cloud-core \
        google-api-core \
        yarl \
        aiohttp \
        tritonclient[all] \
        cohere==4.43 \
        anthropic \
        replicate \
        aleph-alpha-client \
        stability-sdk \
        huggingface_hub \
        langchain \
        langchain-community \
        langchain-core \
        langchain-openai \
        openai==1.14.0 \
        Flask-Migrate==4.0.7 \
        Flask-SQLAlchemy==3.1.1 && \
    python -m nltk.downloader punkt averaged_perceptron_tagger

# Set up directory structure
WORKDIR /app
RUN mkdir -p api web && chown -R user:user /app

# Copy Python environment and files
COPY --from=python-builder --chown=user /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
COPY --chown=user api/ /app/api/

# Copy Next.js files with explicit directory creation
RUN mkdir -p /app/web/.next/standalone /app/web/.next/static
COPY --from=web-builder --chown=user /app/web/.next/standalone /app/web/.next/standalone
COPY --from=web-builder --chown=user /app/web/.next/static /app/web/.next/static
COPY --from=web-builder --chown=user /app/web/public /app/web/public

# Set environment variables for HF Spaces compatibility
ENV FLASK_APP=app.py \
    EDITION=SELF_HOSTED \
    DEPLOY_ENV=PRODUCTION \
    PYTHONPATH=/app/api \
    PATH="/usr/local/bin:${PATH}" \
    STORAGE_DIR=/storage \
    # Database configuration - match docker-compose.yaml
    DB_USERNAME=postgres \
    DB_PASSWORD=difyai123456 \
    DB_HOST=db \
    DB_PORT=5432 \
    DB_DATABASE=dify \
    SQLALCHEMY_POOL_SIZE=30 \
    SQLALCHEMY_POOL_RECYCLE=3600 \
    # Redis configuration - match docker-compose.yaml
    REDIS_HOST=redis \
    REDIS_PORT=6379 \
    REDIS_PASSWORD=difyai123456 \
    REDIS_DB=0

# Copy entrypoint script
COPY docker/entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

# Switch to non-root user
USER user

# HF Spaces uses port 7860
EXPOSE 7860 3000

# Set up storage volumes
VOLUME ["/storage/files", "/storage/cache", "/storage/logs"]

WORKDIR /app
CMD ["./entrypoint.sh"]