# ============================================ # 🚀 FINAL OPTIMIZED DOCKERFILE FOR HUGGING FACE SPACES # - Sub‑20 second responses on free CPU tier # - Multi‑stage build for minimal image size # - Open WebUI v0.9.1 (latest stable) # - Ollama CPU optimizations baked in # ============================================ # --- Stage 1: Builder (Install Ollama) --- FROM python:3.11-slim-bookworm AS builder # Install Ollama (requires curl + zstd for extraction) RUN apt-get update && apt-get install -y --no-install-recommends curl zstd \ && curl -fsSL https://ollama.com/install.sh | sh \ && apt-get clean && rm -rf /var/lib/apt/lists/* # --- Stage 2: Final Image --- FROM python:3.11-slim-bookworm # Copy Ollama binaries from the builder stage COPY --from=builder /usr/local/bin/ollama /usr/local/bin/ollama COPY --from=builder /usr/local/lib/ollama /usr/local/lib/ollama # Install only essential runtime dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ curl ca-certificates \ && apt-get clean && rm -rf /var/lib/apt/lists/* # Install the latest Open WebUI (pinned to 0.9.1 for reproducibility) RUN pip install --no-cache-dir open-webui==0.9.1 # --- CPU & Ollama Optimizations --- # These environment variables are critical for fast CPU inference ENV OLLAMA_NUM_THREADS=2 \ OLLAMA_NUM_PARALLEL=1 \ OLLAMA_MAX_LOADED_MODELS=1 \ OLLAMA_KEEP_ALIVE=-1 \ OLLAMA_CONTEXT_LENGTH=2048 \ OLLAMA_DEBUG=0 \ OLLAMA_LLM_LIBRARY=cpu \ OLLAMA_BASE_URL=http://127.0.0.1:11434 # Disable Open WebUI’s resource‑heavy background features ENV ENABLE_TITLE_GENERATION=false \ ENABLE_TAG_GENERATION=false \ ENABLE_AUTOCOMPLETE_GENERATION=false \ ENABLE_RAG_WEB_SEARCH=false \ ENABLE_QUERIES_CACHE=true \ ENABLE_BASE_MODELS_CACHE=true \ ENABLE_PERSISTENT_CONFIG=false # --- Startup Script --- # Starts Ollama, waits for it to be ready, then launches Open WebUI RUN echo '#!/bin/bash\n\ set -e\n\ echo "Starting Ollama..."\n\ ollama serve &\n\ OLLAMA_PID=$!\n\ echo "Waiting for Ollama to be ready..."\n\ until curl -s http://127.0.0.1:11434/api/tags > /dev/null 2>&1; do\n\ sleep 1\n\ done\n\ echo "Ollama is ready."\n\ echo "Starting Open WebUI..."\n\ exec open-webui serve --host 0.0.0.0 --port 7860\n\ ' > /start.sh && chmod +x /start.sh # Hugging Face Spaces expects the app on port 7860 ENV PORT=7860 EXPOSE 7860 CMD ["/start.sh"]