|
| 1 | +# Use a minimal Debian base image for a smaller container footprint |
| 2 | +FROM debian:12-slim |
| 3 | + |
| 4 | +# Avoid interactive prompts during package installation |
| 5 | +ARG DEBIAN_FRONTEND=noninteractive |
| 6 | + |
| 7 | +# Define the default allowed models and the default model |
| 8 | +ARG ALLOWED_MODELS="pixtral-12b" |
| 9 | +ARG DEFAULT_MODEL="pixtral-12b" |
| 10 | + |
| 11 | +# Set default model as a build argument and runtime environment variable |
| 12 | +ENV DEFAULT_MODEL=${DEFAULT_MODEL} |
| 13 | + |
| 14 | +# Ensure the container is running as root for package installations |
| 15 | +USER root |
| 16 | + |
| 17 | +# Set the working directory early for clarity and organization |
| 18 | +WORKDIR /usr/src/app |
| 19 | + |
| 20 | +# Install system dependencies, including Python 3.11 and venv |
| 21 | +RUN apt-get update \ |
| 22 | + && apt-get install -y --no-install-recommends \ |
| 23 | + ffmpeg \ |
| 24 | + wget \ |
| 25 | + git \ |
| 26 | + gnupg2 \ |
| 27 | + libtbb12 \ |
| 28 | + python3.11 \ |
| 29 | + python3.11-venv \ |
| 30 | + && apt-get clean && rm -rf /var/lib/apt/lists/* |
| 31 | + |
| 32 | +# Install Intel GPU drivers with error handling and version pinning |
| 33 | +RUN mkdir /tmp/neo \ |
| 34 | + && cd /tmp/neo \ |
| 35 | + && wget -q https://github.com/intel/intel-graphics-compiler/releases/download/igc-1.0.17791.9/intel-igc-core_1.0.17791.9_amd64.deb \ |
| 36 | + && wget -q https://github.com/intel/intel-graphics-compiler/releases/download/igc-1.0.17791.9/intel-igc-opencl_1.0.17791.9_amd64.deb \ |
| 37 | + && wget -q https://github.com/intel/compute-runtime/releases/download/24.39.31294.12/intel-level-zero-gpu_1.6.31294.12_amd64.deb \ |
| 38 | + && wget -q https://github.com/intel/compute-runtime/releases/download/24.39.31294.12/intel-opencl-icd_24.39.31294.12_amd64.deb \ |
| 39 | + && wget -q https://github.com/intel/compute-runtime/releases/download/24.39.31294.12/libigdgmm12_22.5.2_amd64.deb \ |
| 40 | + && dpkg -i *.deb \ |
| 41 | + && rm -rf /tmp/neo |
| 42 | + |
| 43 | +# Install Intel NPU drivers with error handling and version pinning |
| 44 | +RUN mkdir /tmp/npu-driver \ |
| 45 | + && cd /tmp/npu-driver \ |
| 46 | + && wget -q https://github.com/intel/linux-npu-driver/releases/download/v1.10.0/intel-driver-compiler-npu_1.10.0.20241107-11729849322_ubuntu22.04_amd64.deb \ |
| 47 | + && wget -q https://github.com/intel/linux-npu-driver/releases/download/v1.10.0/intel-fw-npu_1.10.0.20241107-11729849322_ubuntu22.04_amd64.deb \ |
| 48 | + && wget -q https://github.com/intel/linux-npu-driver/releases/download/v1.10.0/intel-level-zero-npu_1.10.0.20241107-11729849322_ubuntu22.04_amd64.deb \ |
| 49 | + && wget -q https://github.com/oneapi-src/level-zero/releases/download/v1.17.6/level-zero_1.17.6+u22.04_amd64.deb \ |
| 50 | + && dpkg -i *.deb \ |
| 51 | + && rm -rf /tmp/npu-driver |
| 52 | + |
| 53 | +# Create a non-root user for OpenVINO and avoid privilege escalation |
| 54 | +RUN groupadd -r openvino && useradd -r -g openvino -G video openvino |
| 55 | + |
| 56 | +# Set up a dedicated home directory for the user |
| 57 | +RUN mkdir -p /home/openvino && \ |
| 58 | + chown -R openvino:openvino /home/openvino |
| 59 | + |
| 60 | +# Copy application files and adjust permissions (excluding the virtual environments) |
| 61 | +COPY . /usr/src/app |
| 62 | +RUN find /usr/src/app -not -path "/usr/src/app/*/.venv*" -exec chown openvino:openvino {} + \ |
| 63 | + && find /usr/src/app -not -path "/usr/src/app/*/.venv*" -exec chmod 755 {} + |
| 64 | + |
| 65 | +# Remove existing virtual environments to ensure a clean install |
| 66 | +RUN rm -rf /usr/src/app/*/.venv |
| 67 | + |
| 68 | +# Create a Python virtual environment for each model and install dependencies |
| 69 | +RUN for model in $ALLOWED_MODELS; do \ |
| 70 | + python3.11 -m venv /usr/src/app/$model/.venv; \ |
| 71 | + /usr/src/app/$model/.venv/bin/python -m pip install --no-cache-dir --upgrade pip; \ |
| 72 | + if [ -f "/usr/src/app/$model/requirements.txt" ]; then \ |
| 73 | + /usr/src/app/$model/.venv/bin/python -m pip install --no-cache-dir -r /usr/src/app/$model/requirements.txt; \ |
| 74 | + fi; \ |
| 75 | + chown -R openvino:openvino /usr/src/app/$model/.venv; \ |
| 76 | + done |
| 77 | + |
| 78 | +# Set the environment variable for the virtual environment based on the selected model |
| 79 | +ENV PATH="/usr/src/app/${SELECTED_MODEL}/.venv/bin:$PATH" |
| 80 | + |
| 81 | +# Switch to the non-root user for security |
| 82 | +USER openvino |
| 83 | + |
| 84 | +# Set the working directory based on the selected model |
| 85 | +WORKDIR /usr/src/app/${SELECTED_MODEL} |
| 86 | + |
| 87 | +# Expose port for the microservice |
| 88 | +EXPOSE 8100 |
| 89 | + |
| 90 | +# Allow runtime injection of Hugging Face Token and model selection |
| 91 | +# If HF_TOKEN is not provided, models requiring it will fail securely. |
| 92 | +CMD ["bash", "-c", \ |
| 93 | + "SELECTED_MODEL=${MODEL:-$DEFAULT_MODEL} && \ |
| 94 | + export PATH=/usr/src/app/${SELECTED_MODEL}/.venv/bin:$PATH && \ |
| 95 | + export HF_TOKEN=${HF_TOKEN:-''} && \ |
| 96 | + echo Using model: $SELECTED_MODEL && \ |
| 97 | + echo HF_TOKEN set to: ${HF_TOKEN:0:5}****** && \ |
| 98 | + /usr/src/app/${SELECTED_MODEL}/.venv/bin/python /usr/src/app/${SELECTED_MODEL}/backend/server.py"] |
| 99 | + |
| 100 | +# Add a basic health check |
| 101 | +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s \ |
| 102 | + CMD exit 0 |
0 commit comments