Yousif Abdulhafiz commited on
Commit
00eaa92
·
1 Parent(s): ce8c9bf

Refactor Dockerfile to implement multi-stage builds for cleaner and more efficient setup

Browse files
Files changed (1) hide show
  1. Dockerfile +30 -50
Dockerfile CHANGED
@@ -1,66 +1,46 @@
1
- FROM python:3.13.5-slim
 
2
 
3
- WORKDIR /app
4
 
5
- RUN apt-get update && apt-get install -y \
6
- build-essential \
7
- curl \
8
- git \
9
- && rm -rf /var/lib/apt/lists/*
10
-
11
- # Install uv by copying from the official Docker image
12
  COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
13
 
14
- # Copy the project metadata for dependency resolution
15
- COPY pyproject.toml uv.lock /app/
 
 
 
 
 
 
16
 
17
- # Install dependencies into a virtual environment
18
- # Use cache mount for better build performance
19
  RUN --mount=type=cache,target=/root/.cache/uv \
20
- cd /app && \
21
  uv sync --frozen --no-install-project
22
 
23
- # Set up the virtual environment
24
- ENV VIRTUAL_ENV=/app/.venv
25
- ENV PATH="/app/.venv/bin:$PATH"
26
 
27
- # Route all caches/configs to /app and set HOME for Streamlit and others
28
- ENV HOME=/app \
29
- STREAMLIT_HOME=/app/.streamlit \
30
- STREAMLIT_CACHE_DIR=/app/.streamlit/cache \
31
- STREAMLIT_USER_PATH=/app/.streamlit \
32
- STREAMLIT_DISABLE_TELEMETRY=1 \
33
- PYTHONUNBUFFERED=1 \
34
  PYTHONDONTWRITEBYTECODE=1 \
35
- UV_COMPILE_BYTECODE=1 \
36
- UV_LINK_MODE=copy \
37
- HF_HOME=/app/.cache/huggingface \
38
- TRANSFORMERS_CACHE=/app/.cache/huggingface/transformers \
39
- TORCH_HOME=/app/.cache/torch \
40
- XDG_CONFIG_HOME=/app/.config \
41
- XDG_CACHE_HOME=/app/.cache \
42
- EASY_OCR_CACHE_DIR=/app/.EasyOCR
43
 
44
- # Create writable dirs for all caches/configs, including EasyOCR
45
- RUN mkdir -p \
46
- /app/.streamlit/cache \
47
- /app/.cache/huggingface/transformers \
48
- /app/.cache/torch \
49
- /app/.config \
50
- /app/.cache \
51
- /app/.EasyOCR \
52
- && chmod -R 777 /app/.streamlit /app/.cache /app/.config /app/.EasyOCR
53
 
54
- # Fallback for Marker: redirect any site-packages "static" to /app/static (writable)
55
- RUN mkdir -p /app/static && \
56
- if [ -e /usr/local/lib/python3.13/site-packages/static ]; then \
57
- rm -rf /usr/local/lib/python3.13/site-packages/static; \
58
- fi && \
59
- ln -s /app/static /usr/local/lib/python3.13/site-packages/static && \
60
- chmod -R 777 /app/static
61
 
62
- # Copy app
63
- COPY src/ ./src/
64
 
65
  EXPOSE 8501
66
  HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health || exit 1
 
1
+ # ---- Builder Stage ----
2
+ FROM python:3.13.3-slim-bookworm AS builder
3
 
4
+ WORKDIR /virtualenvs
5
 
6
+ # Install uv
 
 
 
 
 
 
7
  COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
8
 
9
+ ENV PYTHONUNBUFFERED=1 \
10
+ PYTHONDONTWRITEBYTECODE=1 \
11
+ UV_COMPILE_BYTECODE=1 \
12
+ UV_LINK_MODE=copy \
13
+ PATH="/virtualenvs/.venv/bin:$PATH"
14
+
15
+ # Copy dependency files
16
+ COPY pyproject.toml uv.lock /virtualenvs/
17
 
18
+ # Install dependencies (excluding project itself)
 
19
  RUN --mount=type=cache,target=/root/.cache/uv \
20
+ cd /virtualenvs && \
21
  uv sync --frozen --no-install-project
22
 
23
+ # ---- Development Stage ----
24
+ FROM python:3.13.3-slim-bookworm AS development
 
25
 
26
+ WORKDIR /src
27
+
28
+ ENV PYTHONUNBUFFERED=1 \
 
 
 
 
29
  PYTHONDONTWRITEBYTECODE=1 \
30
+ VIRTUAL_ENV=/virtualenvs/.venv \
31
+ PATH="/virtualenvs/.venv/bin:$PATH"
 
 
 
 
 
 
32
 
33
+ # Install system dependencies
34
+ RUN apt-get update && apt-get install -y netcat-openbsd && rm -rf /var/lib/apt/lists/*
35
+
36
+ # Install uv in the runtime stage
37
+ COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
 
 
 
 
38
 
39
+ # Copy virtual environment from builder stage
40
+ COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
 
 
 
 
 
41
 
42
+ # Copy application code
43
+ COPY . /src
44
 
45
  EXPOSE 8501
46
  HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health || exit 1