File size: 2,819 Bytes
2155596
 
7e4b3da
2155596
 
 
c92a454
3f8fa9d
 
 
 
 
b563962
2155596
 
 
 
 
 
3f8fa9d
2155596
7ce37bf
3f8fa9d
0c0d5e5
2155596
 
 
 
 
 
0c0d5e5
2155596
 
0c0d5e5
d7a6a99
0c0d5e5
2155596
 
 
 
 
 
0c0d5e5
 
2155596
496ca32
3f8fa9d
443bf8a
 
c92a454
4680b44
0c0d5e5
2155596
0c0d5e5
496ca32
3f8fa9d
0404cc9
3f8fa9d
 
 
 
2155596
 
 
 
3f8fa9d
 
 
 
 
7ce37bf
 
 
6aed364
7ce37bf
6aed364
7ce37bf
0404cc9
a4bef0b
c92a454
443bf8a
 
2155596
4678183
a4bef0b
2155596
 
 
 
 
 
 
 
 
 
3f8fa9d
0404cc9
a4bef0b
3f8fa9d
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
# Base Python image with correct version
FROM python:3.12-slim-bookworm AS base

# Set shared environment variables
ENV NODE_VERSION=20.11.0 \
    NODE_OPTIONS="--max_old_space_size=2048" \
    NEXT_TELEMETRY_DISABLED=1 \
    NODE_ENV=production \
    PYTHONDONTWRITEBYTECODE=1 \
    POETRY_NO_INTERACTION=1 \
    POETRY_VIRTUALENVS_CREATE=false \
    POETRY_CACHE_DIR=/cache/poetry

# Install Node.js
RUN apt-get update && apt-get install -y curl && \
    curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs && \
    npm install -g npm@latest

# Web builder stage
FROM base AS web-builder

WORKDIR /app/web

# Pull and build web components
RUN git clone --depth 1 https://github.com/langgenius/dify.git . && \
    cd web && \
    npm ci && \
    npm run build && \
    npm run standalone

# API builder stage
FROM base AS api-builder

WORKDIR /app/api

# Pull and build API components
RUN git clone --depth 1 https://github.com/langgenius/dify.git . && \
    cd api && \
    pip install --no-cache-dir poetry && \
    poetry install --no-dev && \
    poetry build

# Final stage
FROM base

# Create non-root user (required by Hugging Face)
RUN useradd -m -u 1000 user

# Install runtime dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    git \
    && rm -rf /var/lib/apt/lists/*

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

# Copy artifacts from builders
COPY --from=api-builder --chown=user /app/api /app/api/
COPY --from=web-builder --chown=user /app/web/.next /app/web/.next/
COPY --from=web-builder --chown=user /app/web/public /app/web/public/
COPY --from=web-builder --chown=user /app/web/.next/standalone /app/web/
COPY --from=web-builder --chown=user /app/web/.next/static /app/web/.next/static/

# Install Python requirements
RUN pip install --no-cache-dir gunicorn gevent

# Set environment variables for Hugging Face Spaces
ENV FLASK_APP=app.py \
    EDITION=SELF_HOSTED \
    DEPLOY_ENV=PRODUCTION \
    CONSOLE_API_URL=http://127.0.0.1:7860 \
    CONSOLE_WEB_URL=http://127.0.0.1:3000 \
    SERVICE_API_URL=http://127.0.0.1:7860 \
    APP_WEB_URL=http://127.0.0.1:3000 \
    HOME=/app

# Switch to non-root user
USER user

# Expose Hugging Face Spaces required ports
EXPOSE 7860 3000

# Create startup script
RUN echo '#!/bin/bash\n\
echo "===== Application Startup at $(date "+%Y-%m-%d %H:%M:%S") ====="\n\
echo "Starting Dify services..."\n\
cd /app/api && python -m gunicorn app:app --bind 0.0.0.0:7860 --worker-class gevent --workers 1 &\n\
echo "Starting API server on port 7860..."\n\
cd /app/web && node server.js &\n\
echo "Starting Next.js server on port 3000..."\n\
wait' > /app/entrypoint.sh && \
    chmod +x /app/entrypoint.sh

WORKDIR /app

CMD ["./entrypoint.sh"]