Severian commited on
Commit
89b26ae
·
verified ·
1 Parent(s): de8fa36

Create Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +99 -0
Dockerfile ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Base Python image with correct version
2
+ FROM python:3.12-slim-bookworm AS base
3
+
4
+ # Set shared environment variables
5
+ ENV NODE_VERSION=20.11.0 \
6
+ NODE_OPTIONS="--max_old_space_size=2048" \
7
+ NEXT_TELEMETRY_DISABLED=1 \
8
+ NODE_ENV=production \
9
+ PYTHONDONTWRITEBYTECODE=1 \
10
+ POETRY_NO_INTERACTION=1 \
11
+ POETRY_VIRTUALENVS_CREATE=false \
12
+ POETRY_CACHE_DIR=/cache/poetry
13
+
14
+ # Install Node.js
15
+ RUN apt-get update && apt-get install -y curl && \
16
+ curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
17
+ apt-get install -y nodejs && \
18
+ npm install -g npm@latest
19
+
20
+ # Web builder stage
21
+ FROM base AS web-builder
22
+
23
+ WORKDIR /app/web
24
+
25
+ # Pull and build web components
26
+ RUN git clone --depth 1 https://github.com/langgenius/dify.git . && \
27
+ cd web && \
28
+ npm ci && \
29
+ npm run build && \
30
+ npm run standalone
31
+
32
+ # API builder stage
33
+ FROM base AS api-builder
34
+
35
+ WORKDIR /app/api
36
+
37
+ # Pull and build API components
38
+ RUN git clone --depth 1 https://github.com/langgenius/dify.git . && \
39
+ cd api && \
40
+ pip install --no-cache-dir poetry && \
41
+ poetry install --no-dev && \
42
+ poetry build
43
+
44
+ # Final stage
45
+ FROM base
46
+
47
+ # Create non-root user (required by Hugging Face)
48
+ RUN useradd -m -u 1000 user
49
+
50
+ # Install runtime dependencies
51
+ RUN apt-get update && \
52
+ apt-get install -y --no-install-recommends \
53
+ git \
54
+ && rm -rf /var/lib/apt/lists/*
55
+
56
+ # Set up directory structure
57
+ WORKDIR /app
58
+ RUN mkdir -p api web && chown -R user:user /app
59
+
60
+ # Copy artifacts from builders
61
+ COPY --from=api-builder --chown=user /app/api /app/api/
62
+ COPY --from=web-builder --chown=user /app/web/.next /app/web/.next/
63
+ COPY --from=web-builder --chown=user /app/web/public /app/web/public/
64
+ COPY --from=web-builder --chown=user /app/web/.next/standalone /app/web/
65
+ COPY --from=web-builder --chown=user /app/web/.next/static /app/web/.next/static/
66
+
67
+ # Install Python requirements
68
+ RUN pip install --no-cache-dir gunicorn gevent
69
+
70
+ # Set environment variables for Hugging Face Spaces
71
+ ENV FLASK_APP=app.py \
72
+ EDITION=SELF_HOSTED \
73
+ DEPLOY_ENV=PRODUCTION \
74
+ CONSOLE_API_URL=http://127.0.0.1:7860 \
75
+ CONSOLE_WEB_URL=http://127.0.0.1:3000 \
76
+ SERVICE_API_URL=http://127.0.0.1:7860 \
77
+ APP_WEB_URL=http://127.0.0.1:3000 \
78
+ HOME=/app
79
+
80
+ # Switch to non-root user
81
+ USER user
82
+
83
+ # Expose Hugging Face Spaces required ports
84
+ EXPOSE 7860 3000
85
+
86
+ # Create startup script
87
+ RUN echo '#!/bin/bash\n\
88
+ echo "===== Application Startup at $(date "+%Y-%m-%d %H:%M:%S") ====="\n\
89
+ echo "Starting Dify services..."\n\
90
+ cd /app/api && python -m gunicorn app:app --bind 0.0.0.0:7860 --worker-class gevent --workers 1 &\n\
91
+ echo "Starting API server on port 7860..."\n\
92
+ cd /app/web && node server.js &\n\
93
+ echo "Starting Next.js server on port 3000..."\n\
94
+ wait' > /app/entrypoint.sh && \
95
+ chmod +x /app/entrypoint.sh
96
+
97
+ WORKDIR /app
98
+
99
+ CMD ["./entrypoint.sh"]