GenAIExamples icon indicating copy to clipboard operation
GenAIExamples copied to clipboard

Why containers use hundreds of MBs for Vim/Perl/OpenGL?

Open eero-t opened this issue 1 year ago • 7 comments

Many of the Dockerfiles install Vim and/or Mesa OpenGL/X packages:

$ git grep -l -B1 -e mesa-glx -e '\bvim\b'
AudioQnA/langchain/docker/Dockerfile
ChatQnA/deprecated/langchain/docker/Dockerfile
ChatQnA/docker/Dockerfile
CodeGen/deprecated/codegen/Dockerfile
CodeGen/docker/Dockerfile
CodeTrans/deprecated/langchain/docker/Dockerfile
CodeTrans/docker/Dockerfile
DocSum/deprecated/langchain/docker/Dockerfile
DocSum/docker/Dockerfile
Translation/langchain/docker/Dockerfile

Why?

They take lot of space in the containers; Mesa's LLVM dependency alone adds >100MB, Vim adds 40MB, and I suspect they're reason why full Perl gets installed:

$ docker images | grep chatqna
<MY_REGISTRY>/dgpu-enabling/opea-chatqna       latest      4b71cbea8ab6   36 minutes ago   727MB

$ docker run -it --rm --entrypoint /bin/sh opea/chatqna -c "du -ks /usr/*/*/* | sort -nr"
214284	/usr/local/lib/python3.11
114564	/usr/lib/x86_64-linux-gnu/libLLVM-15.so.1
40520	/usr/share/vim/vim90
30532	/usr/lib/x86_64-linux-gnu/libicudata.so.72.1
25936	/usr/lib/x86_64-linux-gnu/perl
25164	/usr/lib/x86_64-linux-gnu/dri
22736	/usr/lib/x86_64-linux-gnu/libz3.so.4
20732	/usr/share/perl/5.36.0
...

If containers really need text-editor, e.g. nano would be user-friendlier and much smaller (1MB) than vim.

eero-t avatar May 30 '24 14:05 eero-t

"GenAIComps" repo Dockerfiles have the same issue:

$ git grep -l -B1 -e mesa-glx -e '\bvim\b'
.github/workflows/docker/ut.dockerfile
comps/dataprep/qdrant/docker/Dockerfile
comps/dataprep/redis/docker/Dockerfile
comps/embeddings/langchain/docker/Dockerfile
comps/guardrails/langchain/docker/Dockerfile
comps/llms/summarization/tgi/Dockerfile
comps/llms/text-generation/tgi/Dockerfile
comps/reranks/langchain/docker/Dockerfile
comps/retrievers/langchain/docker/Dockerfile

eero-t avatar May 30 '24 14:05 eero-t

Full Perl version gets added as git package dependency (minimal Python image already included few MB minimal Perl, as that's POSIX requirement).

There are so many dependencies between "GenAIComps" and "GenAIExamples" repos that I think it would make sense to merge them. Then git, and therefore also Perl, could be dropped from (almost) all images.

Another alternative would be having separate "fetch" phase in the Dockerfile which would install Git, do git pull (using -depth 1 option to speed it), and remove .git dir afterwards, so that its not left there when final Dockerfile phase copies the GenAIComps dir content from "fetch" phase.

eero-t avatar May 30 '24 14:05 eero-t

Dropping libgl1-mesa-glx and replacing vim with nano in Dockerfile, reduces chatqna container size by 253MB i.e. 35%:

$ docker images|grep chatqna
opea/chatqna             latest       4b71cbea8ab6   About an hour ago   727MB
opea/chatqna-test        latest       9aadb869edaf   11 minutes ago      474MB

eero-t avatar May 30 '24 14:05 eero-t

Another alternative would be having separate "fetch" phase in the Dockerfile which would install Git, do git pull (using -depth 1 option to speed it), and remove .git dir afterwards, so that its not left there when final Dockerfile phase copies the GenAIComps dir content from "fetch" phase.

Tried doing Git cloning in separate step and copying just repo content to final image:

FROM python:3.11-slim AS base
RUN useradd -m -s /bin/bash user && mkdir -p /home/user && chown -R user /home/user/

FROM base AS fetch
RUN apt-get install -y --no-install-recommends git
RUN cd /home/user/ &&  git clone --depth 1 https://github.com/opea-project/GenAIComps.git
RUN rm -r /home/user/GenAIComps/.git

FROM base AS final
COPY --from=fetch /home/user/GenAIComps /home/user/GenAIComps
...

=> It reduced final image size by additional 108MB, to 366MB, which is half of the original 727MB size.

eero-t avatar May 30 '24 15:05 eero-t

Will validate for all the examples and then incorporate this.

srinarayan-srikanthan avatar Jun 11 '24 21:06 srinarayan-srikanthan

All common dependencies should be on a shared base layer, see: https://github.com/opea-project/GenAIComps/issues/265

That way these optimizations need to be done only once.

eero-t avatar Jun 28 '24 08:06 eero-t

will improve it in the future

kevinintel avatar Aug 07 '24 06:08 kevinintel

Once the base images have been cleaned of extra content, it's easy to generate additional, separate "devel" images where those (Vim, Perl, Git etc) tools are added back.

All it needs is:

  • Dockerfile taking the base image as variable, and adding those tools on top of it
  • Script that loops over desired base images, using that Dockerfile to build tool versions of them, and pushing generated images to repository

Which both are pretty trivial...

eero-t avatar Aug 28 '24 15:08 eero-t

Another alternative would be having separate "fetch" phase in the Dockerfile which would install Git, do git pull (using -depth 1 option to speed it), and remove .git dir afterwards, so that its not left there when final Dockerfile phase copies the GenAIComps dir content from "fetch" phase.

Tried doing Git cloning in separate step and copying just repo content to final image:

FROM python:3.11-slim AS base
RUN useradd -m -s /bin/bash user && mkdir -p /home/user && chown -R user /home/user/

FROM base AS fetch
RUN apt-get install -y --no-install-recommends git
RUN cd /home/user/ &&  git clone --depth 1 https://github.com/opea-project/GenAIComps.git
RUN rm -r /home/user/GenAIComps/.git

FROM base AS final
COPY --from=fetch /home/user/GenAIComps /home/user/GenAIComps
...

=> It reduced final image size by additional 108MB, to 366MB, which is half of the original 727MB size.

Instead of removing .git dir in fetch phase, final image could copy just needed pieces, for example:

ENV HOME=/home/user
COPY --from=fetch $HOME/GenAIComps/comps $HOME/GenAIComps/comps
COPY --from=fetch $HOME/GenAIComps/*.* $HOME/GenAIComps/

(git could be better name for the intermediate container stage/image rather than fetch.)

eero-t avatar Oct 21 '24 13:10 eero-t

please submit pr

kevinintel avatar Oct 25 '24 07:10 kevinintel

please submit pr

Ok, here's an example of doing that for GenAIExamples repo containers: https://github.com/opea-project/GenAIExamples/pull/1031

@kevinintel Do you want me to write example PR also for GenAIComps repo containers?

eero-t avatar Oct 25 '24 17:10 eero-t