Refactor Dockerfile for CPU and CUDA builds

Switched to Chainguard images as base for both CPU and CUDA backend builds for improved security and compatibility. Replaced Ubuntu base with Chainguard's Python image for the CPU builds and PyTorch CUDA image for GPU acceleration, resolving python requirements conflicts. Updated package installation commands to align with the new Redhat-compatible base images. The Dockerfile now installs only the necessary dependencies, as Python is provided by the base image.

These changes will facilitate a more secure and streamlined build process with better dependency management across different platforms.
This commit is contained in:
Joseph Young 2024-03-17 17:03:43 -04:00
parent e3b1cbbb86
commit c004ecdccc

View file

@ -15,16 +15,21 @@ RUN npm run build
######## CPU-only WebUI backend ######## ######## CPU-only WebUI backend ########
# To support both CPU and GPU backend, we need to keep the ability to build the CPU-only image. # To support both CPU and GPU backend, we need to keep the ability to build the CPU-only image.
#FROM python:3.11-slim-bookworm as base #FROM python:3.11-slim-bookworm as base
FROM --platform=linux/amd64 cgr.dev/chainguard/python:latest-dev AS cpu-build-amd64
#FROM --platform=linux/amd64 ubuntu:22.04 AS cpu-builder-amd64 #FROM --platform=linux/amd64 ubuntu:22.04 AS cpu-builder-amd64
#FROM --platform=linux/amd64 cpu-builder-amd64 AS cpu-build-amd64 #FROM --platform=linux/amd64 cpu-builder-amd64 AS cpu-build-amd64
#RUN OPENWEBUI_CPU_TARGET="cpu" sh gen_linux.sh #RUN OPENWEBUI_CPU_TARGET="cpu" sh gen_linux.sh
#FROM --platform=linux/amd64 cpu-builder-amd64 AS cpu_avx-build-amd64 #FROM --platform=linux/amd64 cpu-builder-amd64 AS cpu_avx-build-amd64
#RUN OPENWEBUI_CPU_TARGET="cpu_avx" sh gen_linux.sh #RUN OPENWEBUI_CPU_TARGET="cpu_avx" sh gen_linux.sh
#FROM --platform=linux/amd64 cpu-builder-amd64 AS cpu_avx2-build-amd64 #FROM --platform=linux/amd64 cpu-builder-amd64 AS cpu_avx2-build-amd64
#RUN OPENWEBUI_CPU_TARGET="cpu_avx2" sh gen_linux.sh #RUN OPENWEBUI_CPU_TARGET="cpu_avx2" sh gen_linux.sh
######## CUDA WebUI backend ######## ######## CUDA WebUI backend ########
FROM --platform=linux/amd64 nvidia/cuda:"$CUDA_VERSION"-devel-ubuntu22.04 AS cuda-build-amd64 #FROM --platform=linux/amd64 nvidia/cuda:"$CUDA_VERSION"-devel-ubuntu22.04 AS cuda-build-amd64
#FROM --platform=linux/amd64 cgr.dev/chainguard/pytorch-cuda12:latest AS cuda-build-amd64 # fails with python requirements conflicts
# Set environment variables for NVIDIA Container Toolkit # Set environment variables for NVIDIA Container Toolkit
ENV LD_LIBRARY_PATH=/usr/local/nvidia/lib:/usr/local/nvidia/lib64 \ ENV LD_LIBRARY_PATH=/usr/local/nvidia/lib:/usr/local/nvidia/lib64 \
@ -64,9 +69,20 @@ ENV RAG_EMBEDDING_MODEL="all-MiniLM-L6-v2" \
WORKDIR /app/backend WORKDIR /app/backend
# Install Python & dependencies in the container # Install Python & dependencies in the container
RUN apt-get update && \ # Used for Debian
apt-get install -y --no-install-recommends python3.11 python3-pip ffmpeg libsm6 libxext6 pandoc netcat-openbsd && \ #RUN apt-get update && \
rm -rf /var/lib/apt/lists/* # apt-get install -y --no-install-recommends python3.11 python3-pip ffmpeg libsm6 libxext6 pandoc netcat-openbsd && \
# rm -rf /var/lib/apt/lists/*
# Used for Redhat
#RUN apk update && \
# apk add --no-install-recommends python3.11 python3-pip ffmpeg libsm6 libxext6 pandoc netcat-openbsd && \
# apk del /var/cache/apk/*.tbz2
# Install only the dependencies in the container, python will come from the base image used
RUN apk update && \
apk add --no-install-recommends ffmpeg libsm6 libxext6 pandoc netcat-openbsd && \
apk del /var/cache/apk/*.tbz2
COPY ./backend/requirements.txt ./requirements.txt COPY ./backend/requirements.txt ./requirements.txt
RUN pip3 install torch torchvision torchaudio --no-cache-dir && \ RUN pip3 install torch torchvision torchaudio --no-cache-dir && \