From 61a0b0401bda6a030ce53080150e8240e7ce7a60 Mon Sep 17 00:00:00 2001
From: Jurie Germishuys <jurie.germishuys@combine.se>
Date: Fri, 27 Sep 2024 17:14:25 +0200
Subject: [PATCH] Test conda fix

---
 Dockerfile | 51 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 9b81ac75..6e20d4bf 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,5 +1,9 @@
-# Build ffmpeg with CUDA support from source.
+# Build the ffmpeg with CUDA support from source.
+# We need ffmpeg on the system that works with the GPU.
+# Only having the python package is not enough. ---
+# To build from source we need the devel cuda image.
 FROM nvcr.io/nvidia/cuda:12.0.1-cudnn8-devel-ubuntu20.04 as builder
+# So that we are not asked for user input during the build
 ARG DEBIAN_FRONTEND=noninteractive
 
 RUN apt-get update && \
@@ -12,6 +16,7 @@ RUN apt-get update && \
         libc6-dev \
         libssl-dev \
         libtool \
+        # The next package is needed to support -libx246 for ffmpeg
         libx264-dev \
         libxcb1-dev \
         libxau-dev \
@@ -21,7 +26,7 @@ RUN apt-get update && \
     apt-get clean
 
 # --- Build ffmpeg with CUDA support from source ---
-RUN git clone --depth 1 --branch n12.0.16.0 https://git.videolan.org/git/ffmpeg/nv-codec-headers.git && \
+RUN	git clone --depth 1 --branch n12.0.16.0 https://git.videolan.org/git/ffmpeg/nv-codec-headers.git && \
     cd nv-codec-headers && \
     make install && \
     cd .. && \
@@ -34,6 +39,7 @@ RUN git clone --depth 1 --branch n12.0.16.0 https://git.videolan.org/git/ffmpeg/
         --enable-openssl \
         --disable-doc \
         --disable-ffplay \
+        # The libx246 encoder is used in the project, therefore we need to enable libx246 and gpl
         --enable-libx264 \
         --enable-gpl \
         --extra-cflags=-I/usr/local/cuda/include \
@@ -42,8 +48,12 @@ RUN git clone --depth 1 --branch n12.0.16.0 https://git.videolan.org/git/ffmpeg/
     make install
 
 # Start over from the docker image with cuda 12.0
+# since we only want the final result from the previous run and we copy that.
+# Now we can use the runtime cuda image, since we do not need to build anything
+# from scratch. This is better, since the runtime image is smaller
 FROM nvcr.io/nvidia/cuda:12.0.1-cudnn8-runtime-ubuntu20.04
 COPY --from=builder /usr/local/bin/ffmpeg /usr/local/bin/ffmpeg
+# So that we are not asked for user input during the build
 ARG DEBIAN_FRONTEND=noninteractive
 
 # Install dependencies
@@ -69,23 +79,8 @@ RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -
     $HOME/miniconda/bin/conda config --set always_yes yes --set changeps1 no && \
     $HOME/miniconda/bin/conda update -q conda && \
     ln -s $HOME/miniconda/bin/conda /usr/local/bin/conda
-
-# Set environment variables for user
-ARG NB_USER=jovyan
-ARG NB_UID=1000
-ENV USER=${NB_USER} \
-    NB_UID=${NB_UID} \
-    HOME=/home/${NB_USER}
-
-RUN adduser --disabled-password \
-    --gecos "Default user" \
-    --uid ${NB_UID} \
-    ${NB_USER}
-
-# Switch to the new user
-USER ${NB_USER}
-
-# Add Conda to PATH after the user is set
+    
+# Add Conda to PATH
 ENV PATH="$HOME/miniconda/bin:$PATH"
 
 # Create a working directory
@@ -103,12 +98,16 @@ RUN apt-get update && \
         git \
         vim && \
     apt-get clean && \
+    # Install all python packages, numpy needs to be installed
+    # first to avoid the lap build error
     python3 -m pip --no-cache-dir install --upgrade pip && \
     python3 -m pip --no-cache-dir install numpy && \
     python3 -m pip --no-cache-dir install \
         -r /usr/src/app/kso/requirements.txt && \
+    # Uninstall OpenCV from pip and install it via conda
     python3 -m pip uninstall -y opencv-python opencv-contrib-python && \
     conda install -v -c conda-forge opencv && \
+    # Copy over custom autobackend file to enable use of older YOLO models
     cp \
         /usr/src/app/kso/src/autobackend.py \
         /usr/local/lib/python3.8/dist-packages/ultralytics/nn/autobackend.py && \
@@ -121,3 +120,17 @@ ENV WANDB_DIR=/mimer/NOBACKUP/groups/snic2021-6-9/ \
     DATA_DIR=/tmp \
     ARTIFACT_DIR=/tmp \
     PYTHONPATH=$PYTHONPATH:/usr/src/app/kso
+
+# Set everything up to work with the jupyter notebooks
+ARG NB_USER=jovyan
+ARG NB_UID=1000
+ENV USER=${NB_USER} \
+    NB_UID=${NB_UID} \
+    HOME=/home/${NB_USER}
+
+RUN adduser --disabled-password \
+    --gecos "Default user" \
+    --uid ${NB_UID} \
+    ${NB_USER}
+
+USER ${NB_USER}
\ No newline at end of file