Minimizing size of OpenCV in Docker Image

Hey I am looking at reducing the size of our final docker image by using a multistage build to compile openCV:

Currently we have the following steps:


# Install G-Streamer
# https://docs.nvidia.com/jetson/l4t/index.html#page/Tegra%20Linux%20Driver%20Package%20Development%20Guide/accelerated_gstreamer.html#wwpID0E0R40HA
# Note - Keep it as two commands (?)
RUN apt-get update && apt-get install -y gstreamer1.0-tools gstreamer1.0-alsa \
  gstreamer1.0-plugins-base gstreamer1.0-plugins-good \
  gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly \
  gstreamer1.0-libav 

RUN  apt-get install -y libgstreamer1.0-dev \
  libgstreamer-plugins-base1.0-dev \
  libgstreamer-plugins-good1.0-dev \
  libgstreamer-plugins-bad1.0-dev

RUN \
  apt-get update && apt-get install -y --no-install-recommends \
  cuda-toolkit-10-2 cuda-compiler-10-2 \
  lbzip2 xorg-dev \
  cmake wget unzip \
  libgtk2.0-dev \
  libavcodec-dev \
  libgstreamer1.0-dev \
  libgstreamer-plugins-base1.0-dev \
  libjpeg-dev \
  libpng-dev \
  libtiff-dev \
  libdc1394-22-dev \
  python3 \ 
  python3-dev \
  python3-numpy \ 
  network-manager \
  avrdude

RUN \
  wget https://github.com/opencv/opencv/archive/4.5.4.zip && \
  unzip 4.5.4.zip && rm 4.5.4.zip && \
  wget https://github.com/opencv/opencv_contrib/archive/4.5.4.zip -O opencv_modules.4.5.4.zip && \
  unzip opencv_modules.4.5.4.zip && rm opencv_modules.4.5.4.zip && \
  export CUDA_HOME=/usr/local/cuda-10.2/ && \
  export LD_LIBRARY_PATH=${CUDA_HOME}/lib64 && \
  PATH=${CUDA_HOME}/bin:${PATH} && export PATH && \
  mkdir -p opencv-4.5.4/build && cd opencv-4.5.4/build && \
  cmake -D WITH_CUDA=ON \
  -D CUDA_ARCH_BIN="5.3" \
  -D BUILD_LIST=cudev,highgui,videoio,cudaimgproc,ximgproc,python3 \
  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.4/modules \
  -D CUDA_ARCH_PTX="" \
  -D WITH_GSTREAMER=ON \
  -D WITH_LIBV4L=ON \
  -D BUILD_TESTS=OFF \
  -D BUILD_PERF_TESTS=OFF \
  -D BUILD_SAMPLES=OFF \
  -D BUILD_EXAMPLES=OFF \
  -D CMAKE_BUILD_TYPE=RELEASE \
  # -D WITH_JPEG=ON \
  # -D BUILD_JPEG=OFF \
  # -D JPEG_INCLUDE_DIR=/opt/libjpeg-turbo/include/ \
  # -D JPEG_LIBRARIES=/opt/libjpeg-turbo/lib64/ \
  # -D JPEG_LIBRARY=/opt/libjpeg-turbo/lib64/libjpeg.a \
  -D WITH_GTK=OFF \
  -D BUILD_DOCS=OFF \
  -D CMAKE_INSTALL_PREFIX=/usr/local .. && \
  make -j4 && make install && \
  cp /usr/src/app/opencv-4.5.4/build/bin/opencv_version /usr/src/app/ && \
  rm -rf /usr/src/app/opencv-4.5.4 && \
  rm -rf /usr/src/app/opencv_contrib-4.5.4

Can we just copy opencv-4.5.4/build and add it to the path for it to work as expected? I’m asking before trying it myself, just because the build time is rediculously long.