How to avoid reinstalling packages when building Docker image for Python projects?

How to avoid reinstalling packages when building Docker image for Python projects?

To avoid reinstalling packages when building a Docker image for a Python project, you can create a requirements.txt file that lists all of the packages that your project depends on, and then use the pip install command with the -r flag to install the packages listed in the requirements file during the image build process. This way, when you build the image, the packages will be installed from the cache and not downloaded again.


# Build the requirements file
pip freeze > requirements.txt

# Build your Dockerfile
FROM python:3.8-slim-buster

COPY . /app
WORKDIR /app
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", "main.py"]?
        

You can also use multi-stage build to avoid including unnecessary files in the final image.

# Dockerfile
FROM python:3.8-slim-buster as builder

COPY . /app
WORKDIR /app
RUN pip install --no-cache-dir -r requirements.txt

FROM python:3.8-slim-buster

COPY --from=builder /app /app
WORKDIR /app
CMD ["python", "main.py"]
        

Multi-stage docker build a is a very much beneficial if you are optimising the size of the docker image. Thus, you can leverage the caching of the docker layers.

Comment your thoughts/experiences/issue/concerns. Thank you.

#docker #devops #kubernetes #linux #aws #python #jenkins #containership #ansible

要查看或添加评论,请登录

Prabhat Kumar的更多文章

社区洞察

其他会员也浏览了