How I can optimize this Dockerfile to make the resulting Docker image smaller?
FROM python:3.8-alpine
RUN pip install langchain
RUN pip install chromadb
RUN pip install nest_asyncio
RUN pip install beautifulsoup4
RUN pip install lxml
RUN pip install InstructorEmbedding sentence_transformers
RUN pip install flask
EXPOSE 5000
COPY db /app/db
COPY main.py /app
WORKDIR /app
CMD ["python", "main.py"]
I also tried to do staging but it seems like I do not understand fully the concept.
2
Answers
If your sole purpose is to make the Docker image smaller, you can install your Python dependencies in a shell script and move your
main.py
in that shell script. So that your Dockerfile would look like;And your
start_services.sh
would look like this;Altough this approach will use the same space once run on the device, If you’d supply
pip
s cache folder as a volume to the container, then you have to download libraries only once.You can use a Dockerfile like this as a starting point. Just put your dependencies in a
requirements.txt
file. Also, I heavily suggest against using Alpine images for Python projects. Alpine usesmusl
as its libc and this will result in you needing to install some build tools and compile some code if your dependencies expectglibc
instead. It’s just not worth the hassle to deal with issues resulting from the use of Alpine in Py projects.