scriptsledge commited on
Commit
c37ec45
·
verified ·
1 Parent(s): 3845357

fix: optimize backend Dockerfile with multi-stage build and pre-built wheels

Browse files
Files changed (2) hide show
  1. Dockerfile +35 -20
  2. requirements.txt +1 -1
Dockerfile CHANGED
@@ -1,35 +1,50 @@
1
- # Use the official Python 3.10 slim image
2
- FROM python:3.10-slim
3
-
4
- # Set the working directory to /app
5
  WORKDIR /app
6
 
7
- # Copy the requirements file into the container at /app
 
 
 
 
 
 
 
8
  COPY requirements.txt .
9
 
10
- # Install system dependencies required for building llama-cpp-python
11
- RUN apt-get update && apt-get install -y \
12
- build-essential \
13
- cmake \
14
- && rm -rf /var/lib/apt/lists/*
 
 
 
 
 
 
 
 
 
 
15
 
16
- # Install dependencies
17
- RUN pip install --no-cache-dir -r requirements.txt
18
 
19
- # Copy the current directory contents into the container at /app
20
  COPY . .
21
 
22
- # Create a non-root user and switch to it (required by Hugging Face Spaces)
23
  RUN useradd -m -u 1000 user
24
  USER user
25
- ENV HOME=/home/user \
26
- PATH=/home/user/.local/bin:$PATH
27
 
28
- # Expose port 7860 (Hugging Face Spaces default)
29
  EXPOSE 7860
30
 
31
- # Set context size for Hugging Face Spaces (Pure 16GB RAM)
32
  ENV MODEL_CTX_SIZE=8192
33
 
34
- # Run uvicorn when the container launches
35
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ # Stage 1: Builder
2
+ # We use a standard Python image and install uv, which is safer and still incredibly fast.
3
+ FROM python:3.10-slim-bookworm AS builder
 
4
  WORKDIR /app
5
 
6
+ # Install uv (The extremely fast Python package installer)
7
+ RUN pip install uv
8
+
9
+ # Configure uv
10
+ ENV UV_COMPILE_BYTECODE=1
11
+ ENV UV_LINK_MODE=copy
12
+
13
+ # Copy requirements
14
  COPY requirements.txt .
15
 
16
+ # Create a virtual environment and install dependencies using uv
17
+ # We point to the extra index URL for the pre-built llama-cpp-python wheels
18
+ RUN uv venv /app/.venv && \
19
+ uv pip install \
20
+ --no-cache \
21
+ -r requirements.txt \
22
+ --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu \
23
+ --python /app/.venv
24
+
25
+ # Stage 2: Final Runtime Image
26
+ FROM python:3.10-slim-bookworm
27
+ WORKDIR /app
28
+
29
+ # Copy the virtual environment from the builder stage
30
+ COPY --from=builder /app/.venv /app/.venv
31
 
32
+ # Set environment variables to use the virtual environment automatically
33
+ ENV PATH="/app/.venv/bin:$PATH"
34
 
35
+ # Copy the application code
36
  COPY . .
37
 
38
+ # Create a non-root user for security (Hugging Face Spaces requirement)
39
  RUN useradd -m -u 1000 user
40
  USER user
41
+ ENV HOME=/home/user
 
42
 
43
+ # Expose the application port
44
  EXPOSE 7860
45
 
46
+ # Runtime configuration
47
  ENV MODEL_CTX_SIZE=8192
48
 
49
+ # Run the application
50
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
  fastapi
2
  uvicorn
3
- llama-cpp-python
4
  huggingface-hub
 
1
  fastapi
2
  uvicorn
3
+ llama-cpp-python==0.3.2
4
  huggingface-hub