T0X1N commited on
Commit
7d110d0
·
1 Parent(s): 7caf4dc

chore: implement true multi-stage docker build to reduce image size

Browse files
Files changed (1) hide show
  1. Dockerfile +37 -16
Dockerfile CHANGED
@@ -9,12 +9,9 @@
9
  # ===========================================================================
10
 
11
  # ---------------------------------------------------------------------------
12
- # Base stage — common dependencies
13
  # ---------------------------------------------------------------------------
14
- FROM python:3.11-slim AS base
15
-
16
- # Non-interactive apt
17
- ENV DEBIAN_FRONTEND=noninteractive
18
 
19
  # Python settings
20
  ENV PYTHONDONTWRITEBYTECODE=1 \
@@ -24,21 +21,50 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
24
 
25
  WORKDIR /app
26
 
27
- # System dependencies
 
28
  RUN apt-get update && \
29
  apt-get install -y --no-install-recommends \
30
  build-essential \
31
- curl \
32
  git \
33
  && rm -rf /var/lib/apt/lists/*
34
 
 
 
 
 
35
  # Copy requirements
36
  COPY requirements.txt ./requirements.txt
37
  COPY huggingface/requirements.txt ./huggingface-requirements.txt
 
 
38
  RUN pip install --upgrade pip && \
39
- pip install -r requirements.txt
 
 
40
 
41
- # Copy the entire project
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  COPY . .
43
 
44
  # Create necessary directories
@@ -59,8 +85,7 @@ RUN useradd -m -u 1000 appuser && \
59
  chown -R appuser:appuser /app
60
 
61
  USER appuser
62
- ENV HOME=/home/appuser \
63
- PATH=/home/appuser/.local/bin:$PATH
64
 
65
  EXPOSE 8000
66
 
@@ -81,16 +106,12 @@ ENV GRADIO_SERVER_NAME="0.0.0.0" \
81
  GRADIO_SERVER_PORT=7860 \
82
  EMBEDDING_PROVIDER=huggingface
83
 
84
- # Install HuggingFace-specific requirements
85
- RUN pip install -r huggingface-requirements.txt
86
-
87
  # Create non-root user (HF Spaces requirement)
88
  RUN useradd -m -u 1000 user && \
89
  chown -R user:user /app
90
 
91
  USER user
92
- ENV HOME=/home/user \
93
- PATH=/home/user/.local/bin:$PATH
94
 
95
  EXPOSE 7860
96
 
 
9
  # ===========================================================================
10
 
11
  # ---------------------------------------------------------------------------
12
+ # Stage 1: Builder stage — compiles dependencies
13
  # ---------------------------------------------------------------------------
14
+ FROM python:3.11-slim AS builder
 
 
 
15
 
16
  # Python settings
17
  ENV PYTHONDONTWRITEBYTECODE=1 \
 
21
 
22
  WORKDIR /app
23
 
24
+ # System dependencies for building packages
25
+ # These will NOT be included in the final image
26
  RUN apt-get update && \
27
  apt-get install -y --no-install-recommends \
28
  build-essential \
 
29
  git \
30
  && rm -rf /var/lib/apt/lists/*
31
 
32
+ # Create virtual environment
33
+ RUN python -m venv /opt/venv
34
+ ENV PATH="/opt/venv/bin:$PATH"
35
+
36
  # Copy requirements
37
  COPY requirements.txt ./requirements.txt
38
  COPY huggingface/requirements.txt ./huggingface-requirements.txt
39
+
40
+ # Install dependencies into virtual environment
41
  RUN pip install --upgrade pip && \
42
+ pip install -r requirements.txt && \
43
+ pip install -r huggingface-requirements.txt
44
+
45
 
46
+ # ---------------------------------------------------------------------------
47
+ # Stage 2: Final runtime base — strictly required files only
48
+ # ---------------------------------------------------------------------------
49
+ FROM python:3.11-slim AS base
50
+
51
+ ENV PYTHONDONTWRITEBYTECODE=1 \
52
+ PYTHONUNBUFFERED=1
53
+
54
+ WORKDIR /app
55
+
56
+ # Install minimal runtime dependencies (curl for healthcheck)
57
+ # We don't need build-essential or git here
58
+ RUN apt-get update && \
59
+ apt-get install -y --no-install-recommends \
60
+ curl \
61
+ && rm -rf /var/lib/apt/lists/*
62
+
63
+ # Copy virtual environment from builder stage
64
+ COPY --from=builder /opt/venv /opt/venv
65
+ ENV PATH="/opt/venv/bin:$PATH"
66
+
67
+ # Copy the entire project source code
68
  COPY . .
69
 
70
  # Create necessary directories
 
85
  chown -R appuser:appuser /app
86
 
87
  USER appuser
88
+ ENV HOME=/home/appuser
 
89
 
90
  EXPOSE 8000
91
 
 
106
  GRADIO_SERVER_PORT=7860 \
107
  EMBEDDING_PROVIDER=huggingface
108
 
 
 
 
109
  # Create non-root user (HF Spaces requirement)
110
  RUN useradd -m -u 1000 user && \
111
  chown -R user:user /app
112
 
113
  USER user
114
+ ENV HOME=/home/user
 
115
 
116
  EXPOSE 7860
117