LesionDetection / Dockerfile
maregu2023's picture
fix: sanitize OMP_NUM_THREADS at 3 levels to prevent libgomp crash on HF Spaces
8d0d76d
# =============================================================================
# Dockerfile for Brain Lesion Segmentation — Hugging Face Spaces (Docker SDK)
# Serves FastAPI backend + VTK.js static frontend on port 7860
# =============================================================================
FROM pytorch/pytorch:2.1.0-cuda11.8-cudnn8-runtime
# Install system dependencies required for nnunetv2 and its transitive deps
# (SimpleITK, blosc2, imagecodecs need C compilation toolchains)
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
curl \
git \
pkg-config \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libopenblas-dev \
graphviz \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user (HF Spaces requirement)
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
WORKDIR /home/user/app
# Copy requirements first for Docker cache
COPY --chown=user:user requirements.txt .
# Step 1: Install all deps EXCEPT nnunetv2 (keep base image's CUDA torch)
RUN pip install --no-cache-dir --upgrade pip && \
grep -v nnunetv2 requirements.txt | pip install --no-cache-dir -r /dev/stdin
# Step 2: Install nnunetv2's non-torch dependencies, then nnunetv2 itself
# with --no-deps so it does NOT overwrite the CUDA PyTorch
RUN pip install --no-cache-dir \
acvl-utils batchgenerators batchgeneratorsv2 blosc2 \
dynamic-network-architectures graphviz imagecodecs \
pandas requests scikit-image scikit-learn seaborn \
SimpleITK tifffile tqdm yacs && \
pip install --no-cache-dir --no-deps nnunetv2>=2.2
# Verify critical packages installed (fail build early if not)
# Note: CUDA is NOT available during docker build, only at runtime
RUN python -c "import nnunetv2; print('nnunetv2 OK')" && \
python -c "import torch; print('torch', torch.__version__)" && \
python -c "import fastapi; print('fastapi OK')" && \
python -c "import monai; print('monai OK')"
# Copy application code
COPY --chown=user:user . .
# Hugging Face Spaces expects port 7860
EXPOSE 7860
# Use entrypoint script that sanitizes OMP_NUM_THREADS before starting.
# HF Spaces Kubernetes injects OMP_NUM_THREADS=3500m which crashes libgomp.
# The entrypoint forcibly overrides it with a valid integer.
CMD ["bash", "entrypoint.sh"]