# ============================================ # Base stage for shared configuration # ============================================ FROM python:3.10-slim-bookworm AS base # Reduce layer size and memory usage ENV DEBIAN_FRONTEND=noninteractive \ PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 \ POETRY_VERSION=1.8.3 \ POETRY_VIRTUALENVS_CREATE=false \ POETRY_CACHE_DIR=/tmp/poetry_cache # Install only essential build dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ curl \ gnupg \ libgmp-dev \ libmpfr-dev \ libmpc-dev \ libssl-dev \ postgresql-client \ redis-tools \ && rm -rf /var/lib/apt/lists/* # Install Node.js efficiently RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \ apt-get install -y nodejs && \ npm install -g npm@latest && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* # ============================================ # 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/pyproject.toml api/poetry.lock ./ RUN pip install --no-cache-dir poetry==1.8.3 && \ poetry config virtualenvs.create false && \ poetry install --no-dev --no-interaction --no-ansi # Install core dependencies first RUN pip install --no-cache-dir \ gunicorn \ gevent \ flask \ flask-cors \ Flask-SQLAlchemy==3.1.1 \ Flask-Migrate==4.0.7 \ redis \ psycopg2-binary # Install ML dependencies separately with --no-deps RUN pip install --no-cache-dir --no-deps \ numpy \ pandas \ torch \ transformers # ============================================ # 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 \ curl \ gnupg \ libgmp-dev \ libmpfr-dev \ libmpc-dev \ libssl-dev \ postgresql-client \ redis-tools && \ # Install Node.js from NodeSource curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \ apt-get install -y nodejs && \ # Install npm separately npm install -g npm@latest && \ # Cleanup apt-get clean && \ rm -rf /var/lib/apt/lists/* # 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"]