Setup Dify for HF Spaces deployment with optimized Docker configuration
Browse files- Dockerfile +54 -204
- docker/docker-compose.yaml +12 -4
- docker/entrypoint.sh +5 -1
Dockerfile
CHANGED
|
@@ -3,133 +3,34 @@
|
|
| 3 |
# ============================================
|
| 4 |
FROM python:3.10-slim-bookworm AS base
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
&& npm install -g npm@latest \
|
| 27 |
-
&& npm install -g yarn \
|
| 28 |
-
&& apt-get clean \
|
| 29 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 30 |
-
|
| 31 |
-
# Install system dependencies
|
| 32 |
-
RUN apt-get update && \
|
| 33 |
-
apt-get install -y --no-install-recommends \
|
| 34 |
-
build-essential \
|
| 35 |
-
gcc \
|
| 36 |
-
g++ \
|
| 37 |
-
libc6-dev \
|
| 38 |
-
libffi-dev \
|
| 39 |
-
libgmp-dev \
|
| 40 |
-
libmpfr-dev \
|
| 41 |
-
libmpc-dev \
|
| 42 |
-
libssl-dev \
|
| 43 |
-
postgresql-client \
|
| 44 |
-
redis-tools \
|
| 45 |
-
make \
|
| 46 |
-
pkg-config \
|
| 47 |
-
&& apt-get clean \
|
| 48 |
&& rm -rf /var/lib/apt/lists/*
|
| 49 |
|
| 50 |
-
# Install
|
| 51 |
-
RUN
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
protobuf \
|
| 57 |
-
grpcio-tools
|
| 58 |
-
|
| 59 |
-
RUN pip install --no-cache-dir \
|
| 60 |
-
flask \
|
| 61 |
-
flask-cors \
|
| 62 |
-
Flask-SQLAlchemy==3.1.1 \
|
| 63 |
-
Flask-Migrate==4.0.7 \
|
| 64 |
-
flask-login \
|
| 65 |
-
flask-restful \
|
| 66 |
-
flask-limiter \
|
| 67 |
-
flask-caching \
|
| 68 |
-
flask-jwt-extended \
|
| 69 |
-
flask-socketio
|
| 70 |
-
|
| 71 |
-
RUN pip install --no-cache-dir \
|
| 72 |
-
PyYAML \
|
| 73 |
-
celery \
|
| 74 |
-
redis \
|
| 75 |
-
psycopg2-binary \
|
| 76 |
-
sqlalchemy \
|
| 77 |
-
alembic \
|
| 78 |
-
pyjwt \
|
| 79 |
-
requests
|
| 80 |
-
|
| 81 |
-
# Install ML and AI packages separately
|
| 82 |
-
RUN pip install --no-cache-dir \
|
| 83 |
-
numpy \
|
| 84 |
-
pandas \
|
| 85 |
-
python-dotenv \
|
| 86 |
-
pycryptodome \
|
| 87 |
-
cryptography \
|
| 88 |
-
bcrypt \
|
| 89 |
-
python-jose[cryptography] \
|
| 90 |
-
passlib \
|
| 91 |
-
python-multipart \
|
| 92 |
-
gmpy2
|
| 93 |
-
|
| 94 |
-
RUN pip install --no-cache-dir \
|
| 95 |
-
transformers \
|
| 96 |
-
torch \
|
| 97 |
-
tensorflow \
|
| 98 |
-
sentencepiece \
|
| 99 |
-
tokenizers \
|
| 100 |
-
nltk
|
| 101 |
-
|
| 102 |
-
# Install remaining packages
|
| 103 |
-
RUN pip install --no-cache-dir \
|
| 104 |
-
openai==1.14.0 \
|
| 105 |
-
anthropic==0.23.1 \
|
| 106 |
-
Pillow \
|
| 107 |
-
opencv-python-headless \
|
| 108 |
-
scikit-learn \
|
| 109 |
-
scipy \
|
| 110 |
-
google-cloud-aiplatform \
|
| 111 |
-
google-generativeai \
|
| 112 |
-
vertexai \
|
| 113 |
-
google-cloud-core \
|
| 114 |
-
google-api-core \
|
| 115 |
-
yarl \
|
| 116 |
-
aiohttp \
|
| 117 |
-
"tritonclient[all]" \
|
| 118 |
-
cohere==4.43 \
|
| 119 |
-
replicate \
|
| 120 |
-
aleph-alpha-client \
|
| 121 |
-
stability-sdk \
|
| 122 |
-
huggingface_hub \
|
| 123 |
-
langchain \
|
| 124 |
-
langchain-community \
|
| 125 |
-
langchain-core \
|
| 126 |
-
langchain-openai
|
| 127 |
-
|
| 128 |
-
# Install NLTK data
|
| 129 |
-
RUN python -c "import nltk; nltk.download('punkt'); nltk.download('averaged_perceptron_tagger')"
|
| 130 |
-
|
| 131 |
-
# Verify installations
|
| 132 |
-
RUN node --version && npm --version && yarn --version
|
| 133 |
|
| 134 |
# ============================================
|
| 135 |
# Web builder stage - optimized
|
|
@@ -159,16 +60,29 @@ FROM base AS python-builder
|
|
| 159 |
|
| 160 |
WORKDIR /app
|
| 161 |
|
| 162 |
-
|
| 163 |
-
COPY api/ api/
|
| 164 |
-
|
| 165 |
-
WORKDIR /app/api
|
| 166 |
-
|
| 167 |
-
# Install core dependencies first
|
| 168 |
RUN pip install --no-cache-dir poetry==1.8.3 && \
|
| 169 |
poetry config virtualenvs.create false && \
|
| 170 |
poetry install --no-dev --no-interaction --no-ansi
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
# ============================================
|
| 173 |
# Final stage - minimal runtime
|
| 174 |
# ============================================
|
|
@@ -186,86 +100,22 @@ RUN apt-get update && \
|
|
| 186 |
RUN apt-get update && \
|
| 187 |
apt-get install -y --no-install-recommends \
|
| 188 |
build-essential \
|
| 189 |
-
|
| 190 |
-
|
| 191 |
libgmp-dev \
|
| 192 |
libmpfr-dev \
|
| 193 |
libmpc-dev \
|
| 194 |
libssl-dev \
|
| 195 |
postgresql-client \
|
| 196 |
redis-tools && \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
apt-get clean && \
|
| 198 |
-
rm -rf /var/lib/apt/lists/*
|
| 199 |
-
pip install --no-cache-dir \
|
| 200 |
-
gunicorn \
|
| 201 |
-
gevent \
|
| 202 |
-
grpcio \
|
| 203 |
-
pydantic-settings \
|
| 204 |
-
protobuf \
|
| 205 |
-
grpcio-tools \
|
| 206 |
-
flask \
|
| 207 |
-
flask-cors \
|
| 208 |
-
Flask-SQLAlchemy==3.1.1 \
|
| 209 |
-
Flask-Migrate==4.0.7 \
|
| 210 |
-
flask-login \
|
| 211 |
-
flask-restful \
|
| 212 |
-
flask-limiter \
|
| 213 |
-
flask-caching \
|
| 214 |
-
flask-jwt-extended \
|
| 215 |
-
flask-socketio \
|
| 216 |
-
PyYAML \
|
| 217 |
-
celery \
|
| 218 |
-
redis \
|
| 219 |
-
psycopg2-binary \
|
| 220 |
-
sqlalchemy \
|
| 221 |
-
alembic \
|
| 222 |
-
pyjwt \
|
| 223 |
-
requests \
|
| 224 |
-
numpy \
|
| 225 |
-
pandas \
|
| 226 |
-
python-dotenv \
|
| 227 |
-
pycryptodome \
|
| 228 |
-
cryptography \
|
| 229 |
-
bcrypt \
|
| 230 |
-
python-jose[cryptography] \
|
| 231 |
-
passlib \
|
| 232 |
-
python-multipart \
|
| 233 |
-
gmpy2 \
|
| 234 |
-
transformers \
|
| 235 |
-
torch \
|
| 236 |
-
tensorflow \
|
| 237 |
-
sentencepiece \
|
| 238 |
-
tokenizers \
|
| 239 |
-
nltk \
|
| 240 |
-
openai==1.14.0 \
|
| 241 |
-
anthropic==0.23.1 \
|
| 242 |
-
flask-migrate==4.0.5 \
|
| 243 |
-
Pillow \
|
| 244 |
-
opencv-python-headless \
|
| 245 |
-
scikit-learn \
|
| 246 |
-
scipy \
|
| 247 |
-
google-cloud-aiplatform \
|
| 248 |
-
google-generativeai \
|
| 249 |
-
vertexai \
|
| 250 |
-
google-cloud-core \
|
| 251 |
-
google-api-core \
|
| 252 |
-
yarl \
|
| 253 |
-
aiohttp \
|
| 254 |
-
tritonclient[all] \
|
| 255 |
-
cohere==4.43 \
|
| 256 |
-
anthropic \
|
| 257 |
-
replicate \
|
| 258 |
-
aleph-alpha-client \
|
| 259 |
-
stability-sdk \
|
| 260 |
-
huggingface_hub \
|
| 261 |
-
langchain \
|
| 262 |
-
langchain-community \
|
| 263 |
-
langchain-core \
|
| 264 |
-
langchain-openai \
|
| 265 |
-
openai==1.14.0 \
|
| 266 |
-
Flask-Migrate==4.0.7 \
|
| 267 |
-
Flask-SQLAlchemy==3.1.1 && \
|
| 268 |
-
python -m nltk.downloader punkt averaged_perceptron_tagger
|
| 269 |
|
| 270 |
# Set up directory structure
|
| 271 |
WORKDIR /app
|
|
|
|
| 3 |
# ============================================
|
| 4 |
FROM python:3.10-slim-bookworm AS base
|
| 5 |
|
| 6 |
+
# Reduce layer size and memory usage
|
| 7 |
+
ENV DEBIAN_FRONTEND=noninteractive \
|
| 8 |
+
PYTHONDONTWRITEBYTECODE=1 \
|
| 9 |
+
PYTHONUNBUFFERED=1 \
|
| 10 |
+
PIP_NO_CACHE_DIR=1 \
|
| 11 |
+
POETRY_VERSION=1.8.3 \
|
| 12 |
+
POETRY_VIRTUALENVS_CREATE=false \
|
| 13 |
+
POETRY_CACHE_DIR=/tmp/poetry_cache
|
| 14 |
+
|
| 15 |
+
# Install only essential build dependencies
|
| 16 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 17 |
+
build-essential \
|
| 18 |
+
curl \
|
| 19 |
+
gnupg \
|
| 20 |
+
libgmp-dev \
|
| 21 |
+
libmpfr-dev \
|
| 22 |
+
libmpc-dev \
|
| 23 |
+
libssl-dev \
|
| 24 |
+
postgresql-client \
|
| 25 |
+
redis-tools \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
&& rm -rf /var/lib/apt/lists/*
|
| 27 |
|
| 28 |
+
# Install Node.js efficiently
|
| 29 |
+
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
|
| 30 |
+
apt-get install -y nodejs && \
|
| 31 |
+
npm install -g npm@latest && \
|
| 32 |
+
apt-get clean && \
|
| 33 |
+
rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# ============================================
|
| 36 |
# Web builder stage - optimized
|
|
|
|
| 60 |
|
| 61 |
WORKDIR /app
|
| 62 |
|
| 63 |
+
COPY api/pyproject.toml api/poetry.lock ./
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
RUN pip install --no-cache-dir poetry==1.8.3 && \
|
| 65 |
poetry config virtualenvs.create false && \
|
| 66 |
poetry install --no-dev --no-interaction --no-ansi
|
| 67 |
|
| 68 |
+
# Install core dependencies first
|
| 69 |
+
RUN pip install --no-cache-dir \
|
| 70 |
+
gunicorn \
|
| 71 |
+
gevent \
|
| 72 |
+
flask \
|
| 73 |
+
flask-cors \
|
| 74 |
+
Flask-SQLAlchemy==3.1.1 \
|
| 75 |
+
Flask-Migrate==4.0.7 \
|
| 76 |
+
redis \
|
| 77 |
+
psycopg2-binary
|
| 78 |
+
|
| 79 |
+
# Install ML dependencies separately with --no-deps
|
| 80 |
+
RUN pip install --no-cache-dir --no-deps \
|
| 81 |
+
numpy \
|
| 82 |
+
pandas \
|
| 83 |
+
torch \
|
| 84 |
+
transformers
|
| 85 |
+
|
| 86 |
# ============================================
|
| 87 |
# Final stage - minimal runtime
|
| 88 |
# ============================================
|
|
|
|
| 100 |
RUN apt-get update && \
|
| 101 |
apt-get install -y --no-install-recommends \
|
| 102 |
build-essential \
|
| 103 |
+
curl \
|
| 104 |
+
gnupg \
|
| 105 |
libgmp-dev \
|
| 106 |
libmpfr-dev \
|
| 107 |
libmpc-dev \
|
| 108 |
libssl-dev \
|
| 109 |
postgresql-client \
|
| 110 |
redis-tools && \
|
| 111 |
+
# Install Node.js from NodeSource
|
| 112 |
+
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
|
| 113 |
+
apt-get install -y nodejs && \
|
| 114 |
+
# Install npm separately
|
| 115 |
+
npm install -g npm@latest && \
|
| 116 |
+
# Cleanup
|
| 117 |
apt-get clean && \
|
| 118 |
+
rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Set up directory structure
|
| 121 |
WORKDIR /app
|
docker/docker-compose.yaml
CHANGED
|
@@ -276,26 +276,34 @@ services:
|
|
| 276 |
image: langgenius/dify-api:0.11.0
|
| 277 |
restart: always
|
| 278 |
environment:
|
| 279 |
-
# Use the shared environment variables
|
| 280 |
<<: *shared-api-worker-env
|
| 281 |
-
# Startup mode, 'api' starts the API server
|
| 282 |
MODE: api
|
|
|
|
|
|
|
| 283 |
depends_on:
|
| 284 |
db:
|
| 285 |
condition: service_healthy
|
| 286 |
redis:
|
| 287 |
condition: service_healthy
|
| 288 |
volumes:
|
| 289 |
-
# Mount the storage directory to the container
|
| 290 |
- ./volumes/app/storage:/app/api/storage
|
| 291 |
networks:
|
| 292 |
- ssrf_proxy_network
|
| 293 |
- default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
# worker service
|
| 296 |
# The Celery worker for processing the queue.
|
| 297 |
worker:
|
| 298 |
-
image: langgenius/dify-api
|
| 299 |
restart: always
|
| 300 |
environment:
|
| 301 |
# Use the shared environment variables.
|
|
|
|
| 276 |
image: langgenius/dify-api:0.11.0
|
| 277 |
restart: always
|
| 278 |
environment:
|
| 279 |
+
# Use the shared environment variables
|
| 280 |
<<: *shared-api-worker-env
|
| 281 |
+
# Startup mode, 'api' starts the API server
|
| 282 |
MODE: api
|
| 283 |
+
MALLOC_ARENA_MAX: 2
|
| 284 |
+
NODE_OPTIONS: "--max-old-space-size=2048"
|
| 285 |
depends_on:
|
| 286 |
db:
|
| 287 |
condition: service_healthy
|
| 288 |
redis:
|
| 289 |
condition: service_healthy
|
| 290 |
volumes:
|
| 291 |
+
# Mount the storage directory to the container
|
| 292 |
- ./volumes/app/storage:/app/api/storage
|
| 293 |
networks:
|
| 294 |
- ssrf_proxy_network
|
| 295 |
- default
|
| 296 |
+
deploy:
|
| 297 |
+
resources:
|
| 298 |
+
limits:
|
| 299 |
+
memory: 6G
|
| 300 |
+
reservations:
|
| 301 |
+
memory: 2G
|
| 302 |
|
| 303 |
# worker service
|
| 304 |
# The Celery worker for processing the queue.
|
| 305 |
worker:
|
| 306 |
+
image: langgenius/dify-api
|
| 307 |
restart: always
|
| 308 |
environment:
|
| 309 |
# Use the shared environment variables.
|
docker/entrypoint.sh
CHANGED
|
@@ -155,4 +155,8 @@ NEXT_TELEMETRY_DISABLED=1 \
|
|
| 155 |
node server.js &
|
| 156 |
|
| 157 |
# Wait for both processes
|
| 158 |
-
wait
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
node server.js &
|
| 156 |
|
| 157 |
# Wait for both processes
|
| 158 |
+
wait
|
| 159 |
+
|
| 160 |
+
# Add to entrypoint.sh
|
| 161 |
+
export MALLOC_ARENA_MAX=2
|
| 162 |
+
export NODE_OPTIONS="--max-old-space-size=2048"
|