From 35cd37058d43aacf2858a3cfad59c59afc934051 Mon Sep 17 00:00:00 2001
From: Eero Tamminen <eero.t.tamminen@intel.com>
Date: Fri, 25 Oct 2024 18:54:52 +0300
Subject: [PATCH] Use staged builds to minimize final image size

So that redundant things do not end in final image:
- Git repo history
- Test directories
- Git tool and its deps

And drop explicit installation of:
- jemalloc & GLX: nothing uses them (in ChatQnA at least), and
  for testing it's trivial to create image adding those on top:
  https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html#switch-memory-allocator
- langchain_core: GenAIComps install langchain which already depends on that

This demonstrates that only 2-3 lines in the Dockerfiles are unique,
and everything before those can be removed with a common base image.

Signed-off-by: Eero Tamminen <eero.t.tamminen@intel.com>
---
 AudioQnA/Dockerfile               | 52 +++++++++++++++--------
 AudioQnA/Dockerfile.multilang     | 51 +++++++++++++++--------
 AvatarChatbot/Dockerfile          | 52 +++++++++++++++--------
 ChatQnA/Dockerfile                | 52 ++++++++++++++---------
 ChatQnA/Dockerfile.guardrails     | 52 ++++++++++++++---------
 ChatQnA/Dockerfile.without_rerank | 52 ++++++++++++++---------
 ChatQnA/Dockerfile.wrapper        | 49 +++++++++++++++-------
 CodeGen/Dockerfile                | 53 ++++++++++++++++--------
 CodeTrans/Dockerfile              | 51 +++++++++++++++--------
 DocIndexRetriever/Dockerfile      | 49 +++++++++++++++-------
 DocSum/Dockerfile                 | 56 ++++++++++++++++++-------
 EdgeCraftRAG/Dockerfile           | 47 ++++++++++++++-------
 EdgeCraftRAG/Dockerfile.server    |  3 +-
 FaqGen/Dockerfile                 | 50 ++++++++++++++--------
 GraphRAG/Dockerfile               | 50 ++++++++++++++--------
 MultimodalQnA/Dockerfile          | 49 +++++++++++++++-------
 SearchQnA/Dockerfile              | 51 +++++++++++++++--------
 Translation/Dockerfile            | 69 +++++++++++++++++--------------
 VideoQnA/Dockerfile               | 50 ++++++++++++++--------
 VisualQnA/Dockerfile              | 51 +++++++++++++++--------
 20 files changed, 649 insertions(+), 340 deletions(-)

diff --git a/AudioQnA/Dockerfile b/AudioQnA/Dockerfile
index 5797aee9f4..07245de371 100644
--- a/AudioQnA/Dockerfile
+++ b/AudioQnA/Dockerfile
@@ -1,31 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
+
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
 
-COPY ./audioqna.py /home/user/audioqna.py
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./audioqna.py $HOME/audioqna.py
 
 ENTRYPOINT ["python", "audioqna.py"]
diff --git a/AudioQnA/Dockerfile.multilang b/AudioQnA/Dockerfile.multilang
index ef7c926975..1d0573d217 100644
--- a/AudioQnA/Dockerfile.multilang
+++ b/AudioQnA/Dockerfile.multilang
@@ -1,32 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./audioqna_multilang.py /home/user/audioqna_multilang.py
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./audioqna_multilang.py $HOME/audioqna_multilang.py
 
 ENTRYPOINT ["python", "audioqna_multilang.py"]
diff --git a/AvatarChatbot/Dockerfile b/AvatarChatbot/Dockerfile
index b845296f70..3266bc296a 100644
--- a/AvatarChatbot/Dockerfile
+++ b/AvatarChatbot/Dockerfile
@@ -1,33 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    vim \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
-WORKDIR /home/user/GenAIComps
+WORKDIR $HOME
 
-RUN pip install --no-cache-dir --upgrade pip && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./avatarchatbot.py /home/user/avatarchatbot.py
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./avatarchatbot.py $HOME/avatarchatbot.py
 
 ENTRYPOINT ["python", "avatarchatbot.py"]
diff --git a/ChatQnA/Dockerfile b/ChatQnA/Dockerfile
index 4e431ac773..fb7f5e14ec 100644
--- a/ChatQnA/Dockerfile
+++ b/ChatQnA/Dockerfile
@@ -1,35 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt && \
-    pip install --no-cache-dir langchain_core
 
-COPY ./chatqna.py /home/user/chatqna.py
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
 
-RUN echo 'ulimit -S -n 999999' >> ~/.bashrc
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./chatqna.py $HOME/chatqna.py
 
 ENTRYPOINT ["python", "chatqna.py"]
diff --git a/ChatQnA/Dockerfile.guardrails b/ChatQnA/Dockerfile.guardrails
index ed811148c0..4fe5fd2087 100644
--- a/ChatQnA/Dockerfile.guardrails
+++ b/ChatQnA/Dockerfile.guardrails
@@ -1,35 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt && \
-    pip install --no-cache-dir langchain_core
 
-COPY ./chatqna.py /home/user/chatqna.py
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
 
-RUN echo 'ulimit -S -n 999999' >> ~/.bashrc
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./chatqna.py $HOME/chatqna.py
 
 ENTRYPOINT ["python", "chatqna.py", "--with-guardrails"]
diff --git a/ChatQnA/Dockerfile.without_rerank b/ChatQnA/Dockerfile.without_rerank
index 7d3a94c5de..9e6740e9b8 100644
--- a/ChatQnA/Dockerfile.without_rerank
+++ b/ChatQnA/Dockerfile.without_rerank
@@ -1,35 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    git \
-    libgl1-mesa-glx \
-    libjemalloc-dev
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt && \
-    pip install --no-cache-dir langchain_core
 
-COPY ./chatqna.py /home/user/chatqna.py
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
 
-RUN echo 'ulimit -S -n 999999' >> ~/.bashrc
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./chatqna.py $HOME/chatqna.py
 
 ENTRYPOINT ["python", "chatqna.py", "--without-rerank"]
diff --git a/ChatQnA/Dockerfile.wrapper b/ChatQnA/Dockerfile.wrapper
index a9e4fb5444..40ac309246 100644
--- a/ChatQnA/Dockerfile.wrapper
+++ b/ChatQnA/Dockerfile.wrapper
@@ -1,32 +1,49 @@
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
+
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
+
+WORKDIR $HOME
+
+
+# Stage 2: latest GenAIComps sources
+FROM base AS git
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./chatqna_wrapper.py /home/user/chatqna.py
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
 
-RUN echo 'ulimit -S -n 999999' >> ~/.bashrc
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./chatqna_wrapper.py $HOME/chatqna.py
 
 ENTRYPOINT ["python", "chatqna.py"]
diff --git a/CodeGen/Dockerfile b/CodeGen/Dockerfile
index e0aa7d13f0..ab059bbe49 100644
--- a/CodeGen/Dockerfile
+++ b/CodeGen/Dockerfile
@@ -1,34 +1,51 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
 
-ENV LANG=C.UTF-8
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
+
+
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./codegen.py /home/user/codegen.py
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+ENV LANG=C.UTF-8
+
+COPY ./codegen.py $HOME/codegen.py
 
 ENTRYPOINT ["python", "codegen.py"]
diff --git a/CodeTrans/Dockerfile b/CodeTrans/Dockerfile
index 918d936c96..55014a5825 100644
--- a/CodeTrans/Dockerfile
+++ b/CodeTrans/Dockerfile
@@ -1,32 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./code_translation.py /home/user/code_translation.py
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./code_translation.py $HOME/code_translation.py
 
 ENTRYPOINT ["python", "code_translation.py"]
diff --git a/DocIndexRetriever/Dockerfile b/DocIndexRetriever/Dockerfile
index c8794f3efc..dcfd665f74 100644
--- a/DocIndexRetriever/Dockerfile
+++ b/DocIndexRetriever/Dockerfile
@@ -1,30 +1,49 @@
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
+
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
+
+
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./retrieval_tool.py /home/user/retrieval_tool.py
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./retrieval_tool.py $HOME/retrieval_tool.py
 
 ENTRYPOINT ["python", "retrieval_tool.py"]
diff --git a/DocSum/Dockerfile b/DocSum/Dockerfile
index 27e08ee7a3..fd01f3bca0 100644
--- a/DocSum/Dockerfile
+++ b/DocSum/Dockerfile
@@ -1,32 +1,56 @@
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git \
-    ffmpeg
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
+
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
+
+WORKDIR $HOME
+
+
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-WORKDIR /home/user
-RUN git clone https://github.com/opea-project/GenAIComps.git
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
 
-WORKDIR /home/user/GenAIComps
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
 RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
-COPY ./docsum.py /home/user/docsum.py
+USER user
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
 
+# Stage 4: unique part
+FROM comps-base
+
+USER root
+# FFmpeg needed for media processing
+RUN apt-get update && \
+    apt-get install -y --no-install-recommends ffmpeg && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 USER user
 
-WORKDIR /home/user
+COPY ./docsum.py $HOME/docsum.py
 
 ENTRYPOINT ["python", "docsum.py"]
-
diff --git a/EdgeCraftRAG/Dockerfile b/EdgeCraftRAG/Dockerfile
index b2a9984a8e..fb7f5e14ec 100644
--- a/EdgeCraftRAG/Dockerfile
+++ b/EdgeCraftRAG/Dockerfile
@@ -1,34 +1,49 @@
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
 
-SHELL ["/bin/bash", "-o", "pipefail", "-c"]
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-COPY ./chatqna.py /home/user/chatqna.py
+WORKDIR $HOME
 
-WORKDIR /home/user
-RUN git clone https://github.com/opea-project/GenAIComps.git
 
-WORKDIR /home/user/GenAIComps
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
+
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
 RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
 
-RUN echo 'ulimit -S -n 999999' >> ~/.bashrc
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./chatqna.py $HOME/chatqna.py
 
 ENTRYPOINT ["python", "chatqna.py"]
diff --git a/EdgeCraftRAG/Dockerfile.server b/EdgeCraftRAG/Dockerfile.server
index 363807777d..3bb572f116 100644
--- a/EdgeCraftRAG/Dockerfile.server
+++ b/EdgeCraftRAG/Dockerfile.server
@@ -15,8 +15,7 @@ RUN wget -qO - https://repositories.intel.com/gpu/intel-graphics.key | \
     gpg --yes --dearmor --output /usr/share/keyrings/intel-graphics.gpg
 RUN echo "deb [arch=amd64,i386 signed-by=/usr/share/keyrings/intel-graphics.gpg] https://repositories.intel.com/gpu/ubuntu jammy client" | \
     tee /etc/apt/sources.list.d/intel-gpu-jammy.list
-RUN apt-get update
-RUN apt-get install -y \
+RUN apt-get update && apt-get install -y \
     intel-opencl-icd intel-level-zero-gpu level-zero intel-level-zero-gpu-raytracing \
     intel-media-va-driver-non-free libmfx1 libmfxgen1 libvpl2 \
     libegl-mesa0 libegl1-mesa libegl1-mesa-dev libgbm1 libgl1-mesa-dev libgl1-mesa-dri \
diff --git a/FaqGen/Dockerfile b/FaqGen/Dockerfile
index 4018b44d1f..2d1afd002a 100644
--- a/FaqGen/Dockerfile
+++ b/FaqGen/Dockerfile
@@ -1,33 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
 
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
+
+
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./faqgen.py /home/user/faqgen.py
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./faqgen.py $HOME/faqgen.py
 
 ENTRYPOINT ["python", "faqgen.py"]
diff --git a/GraphRAG/Dockerfile b/GraphRAG/Dockerfile
index bf01c01b2e..1e50649dd5 100644
--- a/GraphRAG/Dockerfile
+++ b/GraphRAG/Dockerfile
@@ -1,33 +1,49 @@
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    git \
-    libgl1-mesa-glx \
-    libjemalloc-dev
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
+
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
+
+WORKDIR $HOME
+
+
+# Stage 2: latest GenAIComps sources
+FROM base AS git
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt && \
-    pip install --no-cache-dir langchain_core
 
-COPY ./graphrag.py /home/user/graphrag.py
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
 
-RUN echo 'ulimit -S -n 999999' >> ~/.bashrc
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./graphrag.py $HOME/graphrag.py
 
 ENTRYPOINT ["python", "graphrag.py"]
diff --git a/MultimodalQnA/Dockerfile b/MultimodalQnA/Dockerfile
index 534203c96e..f38b305e42 100644
--- a/MultimodalQnA/Dockerfile
+++ b/MultimodalQnA/Dockerfile
@@ -1,31 +1,50 @@
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
+
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
+
+
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./multimodalqna.py /home/user/multimodalqna.py
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./multimodalqna.py $HOME/multimodalqna.py
 
 ENTRYPOINT ["python", "multimodalqna.py"]
 # ENTRYPOINT ["/usr/bin/sleep", "infinity"]
diff --git a/SearchQnA/Dockerfile b/SearchQnA/Dockerfile
index 2d8e59f6b5..df8d536b08 100644
--- a/SearchQnA/Dockerfile
+++ b/SearchQnA/Dockerfile
@@ -1,32 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./searchqna.py /home/user/searchqna.py
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./searchqna.py $HOME/searchqna.py
 
 ENTRYPOINT ["python", "searchqna.py"]
diff --git a/Translation/Dockerfile b/Translation/Dockerfile
index 33931689c1..70266c9b87 100644
--- a/Translation/Dockerfile
+++ b/Translation/Dockerfile
@@ -1,42 +1,49 @@
-# Copyright (c) 2024 Intel Corporation
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#    http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-
-FROM python:3.11-slim
-
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+# Copyright (C) 2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
+
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./translation.py /home/user/translation.py
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./translation.py $HOME/translation.py
 
 ENTRYPOINT ["python", "translation.py"]
diff --git a/VideoQnA/Dockerfile b/VideoQnA/Dockerfile
index bd1ff121f5..0504a71881 100644
--- a/VideoQnA/Dockerfile
+++ b/VideoQnA/Dockerfile
@@ -1,33 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
+WORKDIR $HOME
 
-RUN git clone https://github.com/opea-project/GenAIComps.git
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-COPY ./videoqna.py /home/user/videoqna.py
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
 
-ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./videoqna.py $HOME/videoqna.py
 
 ENTRYPOINT ["python", "videoqna.py"]
diff --git a/VisualQnA/Dockerfile b/VisualQnA/Dockerfile
index ef6a2e2536..257b39df89 100644
--- a/VisualQnA/Dockerfile
+++ b/VisualQnA/Dockerfile
@@ -1,32 +1,49 @@
-
-
 # Copyright (C) 2024 Intel Corporation
 # SPDX-License-Identifier: Apache-2.0
 
-FROM python:3.11-slim
+# Stage 1: base setup used by other stages
+FROM python:3.11-slim AS base
+
+# get security updates
+RUN apt-get update && apt-get upgrade -y && \
+    apt-get clean && rm -rf /var/lib/apt/lists/*
 
-RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
-    libgl1-mesa-glx \
-    libjemalloc-dev \
-    git
+ENV HOME=/home/user
 
 RUN useradd -m -s /bin/bash user && \
-    mkdir -p /home/user && \
-    chown -R user /home/user/
+    mkdir -p $HOME && \
+    chown -R user $HOME
 
-WORKDIR /home/user/
-RUN git clone https://github.com/opea-project/GenAIComps.git
+WORKDIR $HOME
 
-WORKDIR /home/user/GenAIComps
-RUN pip install --no-cache-dir --upgrade pip setuptools && \
-    pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
 
-COPY ./visualqna.py /home/user/visualqna.py
+# Stage 2: latest GenAIComps sources
+FROM base AS git
+
+RUN apt-get update && apt-get install -y --no-install-recommends git
+RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
+
 
-ENV PYTHONPATH=/home/user/GenAIComps
+# Stage 3: common layer shared by services using GenAIComps
+FROM base AS comps-base
+
+# copy just relevant parts
+COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
+COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
+
+WORKDIR $HOME/GenAIComps
+RUN pip install --no-cache-dir --upgrade pip setuptools && \
+    pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
+WORKDIR $HOME
+
+ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
 
 USER user
 
-WORKDIR /home/user
+
+# Stage 4: unique part
+FROM comps-base
+
+COPY ./visualqna.py $HOME/visualqna.py
 
 ENTRYPOINT ["python", "visualqna.py"]
