Skip to content

Commit 0eae391

Browse files
authored
Use staged builds to minimize final image sizes (#1031)
Staged image builds so that final images do not have redundant things like: - Git tool and its deps - Git repo history - Test directories Fixes: #225 Signed-off-by: Eero Tamminen <eero.t.tamminen@intel.com>
1 parent 23d885b commit 0eae391

File tree

20 files changed

+649
-340
lines changed

20 files changed

+649
-340
lines changed

AudioQnA/Dockerfile

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
1-
2-
31
# Copyright (C) 2024 Intel Corporation
42
# SPDX-License-Identifier: Apache-2.0
53

6-
FROM python:3.11-slim
4+
# Stage 1: base setup used by other stages
5+
FROM python:3.11-slim AS base
76

8-
RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
9-
libgl1-mesa-glx \
10-
libjemalloc-dev \
11-
git
7+
# get security updates
8+
RUN apt-get update && apt-get upgrade -y && \
9+
apt-get clean && rm -rf /var/lib/apt/lists/*
10+
11+
ENV HOME=/home/user
1212

1313
RUN useradd -m -s /bin/bash user && \
14-
mkdir -p /home/user && \
15-
chown -R user /home/user/
14+
mkdir -p $HOME && \
15+
chown -R user $HOME
1616

17-
WORKDIR /home/user/
18-
RUN git clone https://github.com/opea-project/GenAIComps.git
19-
WORKDIR /home/user/GenAIComps
20-
RUN pip install --no-cache-dir --upgrade pip setuptools && \
21-
pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
17+
WORKDIR $HOME
18+
19+
20+
# Stage 2: latest GenAIComps sources
21+
FROM base AS git
22+
23+
RUN apt-get update && apt-get install -y --no-install-recommends git
24+
RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
2225

23-
COPY ./audioqna.py /home/user/audioqna.py
2426

25-
ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
27+
# Stage 3: common layer shared by services using GenAIComps
28+
FROM base AS comps-base
29+
30+
# copy just relevant parts
31+
COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
32+
COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
33+
34+
WORKDIR $HOME/GenAIComps
35+
RUN pip install --no-cache-dir --upgrade pip setuptools && \
36+
pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
37+
WORKDIR $HOME
38+
39+
ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
2640

2741
USER user
2842

29-
WORKDIR /home/user
43+
44+
# Stage 4: unique part
45+
FROM comps-base
46+
47+
COPY ./audioqna.py $HOME/audioqna.py
3048

3149
ENTRYPOINT ["python", "audioqna.py"]

AudioQnA/Dockerfile.multilang

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,49 @@
1-
2-
31
# Copyright (C) 2024 Intel Corporation
42
# SPDX-License-Identifier: Apache-2.0
53

6-
FROM python:3.11-slim
4+
# Stage 1: base setup used by other stages
5+
FROM python:3.11-slim AS base
6+
7+
# get security updates
8+
RUN apt-get update && apt-get upgrade -y && \
9+
apt-get clean && rm -rf /var/lib/apt/lists/*
710

8-
RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
9-
libgl1-mesa-glx \
10-
libjemalloc-dev \
11-
git
11+
ENV HOME=/home/user
1212

1313
RUN useradd -m -s /bin/bash user && \
14-
mkdir -p /home/user && \
15-
chown -R user /home/user/
14+
mkdir -p $HOME && \
15+
chown -R user $HOME
1616

17-
WORKDIR /home/user/
18-
RUN git clone https://github.com/opea-project/GenAIComps.git
17+
WORKDIR $HOME
1918

20-
WORKDIR /home/user/GenAIComps
21-
RUN pip install --no-cache-dir --upgrade pip setuptools && \
22-
pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
2319

24-
COPY ./audioqna_multilang.py /home/user/audioqna_multilang.py
20+
# Stage 2: latest GenAIComps sources
21+
FROM base AS git
22+
23+
RUN apt-get update && apt-get install -y --no-install-recommends git
24+
RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
25+
2526

26-
ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
27+
# Stage 3: common layer shared by services using GenAIComps
28+
FROM base AS comps-base
29+
30+
# copy just relevant parts
31+
COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
32+
COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
33+
34+
WORKDIR $HOME/GenAIComps
35+
RUN pip install --no-cache-dir --upgrade pip setuptools && \
36+
pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
37+
WORKDIR $HOME
38+
39+
ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
2740

2841
USER user
2942

30-
WORKDIR /home/user
43+
44+
# Stage 4: unique part
45+
FROM comps-base
46+
47+
COPY ./audioqna_multilang.py $HOME/audioqna_multilang.py
3148

3249
ENTRYPOINT ["python", "audioqna_multilang.py"]

AvatarChatbot/Dockerfile

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
1-
2-
31
# Copyright (C) 2024 Intel Corporation
42
# SPDX-License-Identifier: Apache-2.0
53

6-
FROM python:3.11-slim
4+
# Stage 1: base setup used by other stages
5+
FROM python:3.11-slim AS base
6+
7+
# get security updates
8+
RUN apt-get update && apt-get upgrade -y && \
9+
apt-get clean && rm -rf /var/lib/apt/lists/*
710

8-
RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
9-
libgl1-mesa-glx \
10-
libjemalloc-dev \
11-
vim \
12-
git
11+
ENV HOME=/home/user
1312

1413
RUN useradd -m -s /bin/bash user && \
15-
mkdir -p /home/user && \
16-
chown -R user /home/user/
14+
mkdir -p $HOME && \
15+
chown -R user $HOME
1716

18-
WORKDIR /home/user/
19-
RUN git clone https://github.com/opea-project/GenAIComps.git
20-
WORKDIR /home/user/GenAIComps
17+
WORKDIR $HOME
2118

22-
RUN pip install --no-cache-dir --upgrade pip && \
23-
pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt
2419

25-
COPY ./avatarchatbot.py /home/user/avatarchatbot.py
20+
# Stage 2: latest GenAIComps sources
21+
FROM base AS git
22+
23+
RUN apt-get update && apt-get install -y --no-install-recommends git
24+
RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
25+
2626

27-
ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
27+
# Stage 3: common layer shared by services using GenAIComps
28+
FROM base AS comps-base
29+
30+
# copy just relevant parts
31+
COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
32+
COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
33+
34+
WORKDIR $HOME/GenAIComps
35+
RUN pip install --no-cache-dir --upgrade pip && \
36+
pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
37+
WORKDIR $HOME
38+
39+
ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
2840

2941
USER user
3042

31-
WORKDIR /home/user
43+
44+
# Stage 4: unique part
45+
FROM comps-base
46+
47+
COPY ./avatarchatbot.py $HOME/avatarchatbot.py
3248

3349
ENTRYPOINT ["python", "avatarchatbot.py"]

ChatQnA/Dockerfile

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
1-
2-
31
# Copyright (C) 2024 Intel Corporation
42
# SPDX-License-Identifier: Apache-2.0
53

6-
FROM python:3.11-slim
4+
# Stage 1: base setup used by other stages
5+
FROM python:3.11-slim AS base
6+
7+
# get security updates
8+
RUN apt-get update && apt-get upgrade -y && \
9+
apt-get clean && rm -rf /var/lib/apt/lists/*
710

8-
RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
9-
libgl1-mesa-glx \
10-
libjemalloc-dev \
11-
git
11+
ENV HOME=/home/user
1212

1313
RUN useradd -m -s /bin/bash user && \
14-
mkdir -p /home/user && \
15-
chown -R user /home/user/
14+
mkdir -p $HOME && \
15+
chown -R user $HOME
1616

17-
WORKDIR /home/user/
18-
RUN git clone https://github.com/opea-project/GenAIComps.git
17+
WORKDIR $HOME
1918

20-
WORKDIR /home/user/GenAIComps
21-
RUN pip install --no-cache-dir --upgrade pip setuptools && \
22-
pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt && \
23-
pip install --no-cache-dir langchain_core
2419

25-
COPY ./chatqna.py /home/user/chatqna.py
20+
# Stage 2: latest GenAIComps sources
21+
FROM base AS git
22+
23+
RUN apt-get update && apt-get install -y --no-install-recommends git
24+
RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
25+
2626

27-
ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
27+
# Stage 3: common layer shared by services using GenAIComps
28+
FROM base AS comps-base
29+
30+
# copy just relevant parts
31+
COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
32+
COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
33+
34+
WORKDIR $HOME/GenAIComps
35+
RUN pip install --no-cache-dir --upgrade pip setuptools && \
36+
pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
37+
WORKDIR $HOME
38+
39+
ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
2840

2941
USER user
3042

31-
WORKDIR /home/user
3243

33-
RUN echo 'ulimit -S -n 999999' >> ~/.bashrc
44+
# Stage 4: unique part
45+
FROM comps-base
46+
47+
COPY ./chatqna.py $HOME/chatqna.py
3448

3549
ENTRYPOINT ["python", "chatqna.py"]

ChatQnA/Dockerfile.guardrails

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
1-
2-
31
# Copyright (C) 2024 Intel Corporation
42
# SPDX-License-Identifier: Apache-2.0
53

6-
FROM python:3.11-slim
4+
# Stage 1: base setup used by other stages
5+
FROM python:3.11-slim AS base
6+
7+
# get security updates
8+
RUN apt-get update && apt-get upgrade -y && \
9+
apt-get clean && rm -rf /var/lib/apt/lists/*
710

8-
RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
9-
libgl1-mesa-glx \
10-
libjemalloc-dev \
11-
git
11+
ENV HOME=/home/user
1212

1313
RUN useradd -m -s /bin/bash user && \
14-
mkdir -p /home/user && \
15-
chown -R user /home/user/
14+
mkdir -p $HOME && \
15+
chown -R user $HOME
1616

17-
WORKDIR /home/user/
18-
RUN git clone https://github.com/opea-project/GenAIComps.git
17+
WORKDIR $HOME
1918

20-
WORKDIR /home/user/GenAIComps
21-
RUN pip install --no-cache-dir --upgrade pip setuptools && \
22-
pip install --no-cache-dir -r /home/user/GenAIComps/requirements.txt && \
23-
pip install --no-cache-dir langchain_core
2419

25-
COPY ./chatqna.py /home/user/chatqna.py
20+
# Stage 2: latest GenAIComps sources
21+
FROM base AS git
22+
23+
RUN apt-get update && apt-get install -y --no-install-recommends git
24+
RUN git clone --depth 1 https://github.com/opea-project/GenAIComps.git
25+
2626

27-
ENV PYTHONPATH=$PYTHONPATH:/home/user/GenAIComps
27+
# Stage 3: common layer shared by services using GenAIComps
28+
FROM base AS comps-base
29+
30+
# copy just relevant parts
31+
COPY --from=git $HOME/GenAIComps/comps $HOME/GenAIComps/comps
32+
COPY --from=git $HOME/GenAIComps/*.* $HOME/GenAIComps/LICENSE $HOME/GenAIComps/
33+
34+
WORKDIR $HOME/GenAIComps
35+
RUN pip install --no-cache-dir --upgrade pip setuptools && \
36+
pip install --no-cache-dir -r $HOME/GenAIComps/requirements.txt
37+
WORKDIR $HOME
38+
39+
ENV PYTHONPATH=$PYTHONPATH:$HOME/GenAIComps
2840

2941
USER user
3042

31-
WORKDIR /home/user
3243

33-
RUN echo 'ulimit -S -n 999999' >> ~/.bashrc
44+
# Stage 4: unique part
45+
FROM comps-base
46+
47+
COPY ./chatqna.py $HOME/chatqna.py
3448

3549
ENTRYPOINT ["python", "chatqna.py", "--with-guardrails"]

0 commit comments

Comments
 (0)