@@ -5,10 +5,11 @@ FROM python:3.11-slim
55# Avoid interactive prompts during apt installs
66ENV DEBIAN_FRONTEND=noninteractive
77
8- # Python / pip ergonomics
8+ # Python ergonomics
99ENV PYTHONUNBUFFERED=1 \
1010 PYTHONDONTWRITEBYTECODE=1 \
11- PIP_DISABLE_PIP_VERSION_CHECK=1
11+ PIP_DISABLE_PIP_VERSION_CHECK=1 \
12+ PIP_NO_CACHE_DIR=1
1213
1314# System deps (opencv, pillow, etc.). Keep lean but practical.
1415RUN apt-get update && apt-get install -y --no-install-recommends \
@@ -23,7 +24,9 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2324 libgl1 \
2425 && rm -rf /var/lib/apt/lists/*
2526
26- # Create a non-root user (best practice)
27+ # ------------------------------------------------------------
28+ # Non-root user
29+ # ------------------------------------------------------------
2730ARG USERNAME=lm2
2831ARG USER_UID=1000
2932ARG USER_GID=1000
@@ -32,44 +35,52 @@ RUN groupadd --gid ${USER_GID} ${USERNAME} \
3235
3336WORKDIR /app
3437
35- # -------------------------------------------------------------------
36- # Dependency install (cache-friendly):
37- # 1) Copy only requirements first so Docker can cache this layer
38- # 2) Install uv + dependencies using uv's resolver (fast, robust)
39- # 3) Then copy the full repo
40- # -------------------------------------------------------------------
41-
42- # Copy requirements separately for better layer caching
38+ # ------------------------------------------------------------
39+ # Cache-friendly dependency install:
40+ # Copy requirements first, install deps, then copy repo.
41+ # ------------------------------------------------------------
4342COPY requirements.txt /app/requirements.txt
4443
45- # Install uv + Python deps (system install inside container)
46- # Notes:
47- # - --system installs into the container's system Python (no venv).
48- # - UV_LINK_MODE=copy avoids hardlink warnings in some Docker setups.
44+ # Install pip + uv
45+ # UV_LINK_MODE=copy avoids hardlink warnings in some Docker setups.
4946ENV UV_LINK_MODE=copy
50- RUN python -m pip install --no-cache-dir -U pip \
51- && pip install --no-cache-dir uv \
52- && uv pip install --system -r /app/requirements.txt \
53- && uv pip install --system "git+https://github.com/waspinator/pycococreator.git@fba8f4098f3c7aaa05fe119dc93bbe4063afdab8#egg=pycococreatortools" \
47+ RUN python -m pip install --no-cache-dir -U pip setuptools wheel \
48+ && python -m pip install --no-cache-dir uv
49+
50+ # Install Python deps from requirements using BuildKit cache mount
51+ # Do NOT rm -rf the mount target; it's a BuildKit-managed cache.
52+ ENV UV_CACHE_DIR=/root/.cache/uv
53+ RUN --mount=type=cache,target=/root/.cache/uv \
54+ uv pip install --system --no-cache-dir -r /app/requirements.txt
55+
56+ # Required explicit installs (mirrors GPU approach)
57+ RUN uv pip install --system \
58+ "git+https://github.com/waspinator/pycococreator.git@fba8f4098f3c7aaa05fe119dc93bbe4063afdab8#egg=pycococreatortools" \
5459 && uv pip install --system "pycocotools>=2.0.5" \
5560 && uv pip install --system "opencv-contrib-python-headless==4.7.0.72" \
56- && uv pip install --system "vit-pytorch==0.37.1" \
57- && uv pip install --system torch torchvision torchaudio
61+ && uv pip install --system "vit-pytorch==0.37.1"
5862
59- # Optional sanity check (uncomment if you want build-time verification)
60- # RUN python -c "import cv2, torch; print('cv2', cv2.__version__); print('torch', torch.__version__)"
63+ # Torch CPU build (more deterministic than letting it resolve implicitly)
64+ # If you prefer "latest compatible", you can remove the version pins.
65+ RUN uv pip install --system \
66+ "torch==2.3.1" "torchvision==0.18.1" "torchaudio==2.3.1" \
67+ --index-url https://download.pytorch.org/whl/cpu
6168
6269# Now copy the rest of the repo into image
6370COPY . /app
6471
6572# Runtime environment variables expected by LM2
6673ENV LM2_MODELS_DIR=/models \
67- LM2_DATA_DIR=/data
74+ LM2_DATA_DIR=/data \
75+ MPLCONFIGDIR=/tmp/matplotlib \
76+ YOLO_CONFIG_DIR=/tmp/ultralytics
6877
6978# Create default mount points
70- RUN mkdir -p /data /models /output \
71- && chown -R ${USERNAME}:${USERNAME} /app /data /models /output
79+ # Keep ownership consistent for non-root execution.
80+ RUN mkdir -p /data /models /output /tmp/matplotlib /tmp/ultralytics \
81+ && chown -R ${USERNAME}:${USERNAME} /app /data /models /output /tmp/matplotlib /tmp/ultralytics
7282
83+ # Entrypoint
7384COPY docker/entrypoint.sh /entrypoint.sh
7485RUN chmod +x /entrypoint.sh
7586
0 commit comments