首页 > 解决方案 > 在复制整个项目之前安装 setup.py 依赖项

问题描述

我的 Dockerfile 曾经是这样的:

...
COPY . /source/
RUN pip install -e .
...

但是,我想通过COPYsetup.py在复制其余代码之前完成 docker 构建来最小化完成 docker 构建所需的时间。这样 pip install 可以在两者之间完成,因此不需要在每次代码更改时都进行安装,只有 setup.py 才需要。

...
COPY setup.py /source/
RUN pip install -e .
COPY . /source/
...

您是否发现pip install -e .在生产 docker 映像中使用在效率方面有任何问题?有更好的方法吗?我想知道,因为我只看到-e在开发机器上使用过,从未在生产中使用过。

标签: pythondockerpip

解决方案


Why would you prefer installing with pip install -e . rather than pip install . in production? If you want to ability to modify code without restarting container, you will do that in development only I guess. In production, it is safer to allow code to be changed only after container restart.

Concerning the way you build your images, if you want to speed up build and have a clean final image you can try with multi-stage build:

Use multi-stage build to create wheels for each dependancy in first stage, and install package and dependencies in second step.

FROM python:3-alpine as wheels_builder

RUN mkdir /wheels

COPY setup.py ./setup.py

# Filter the packages not needed to build your sources 
RUN apk add --update --no-cache gcc g++ gfortran openblas-dev musl-dev linux-headers libffi-dev openssl-dev sqlite-dev tk-dev readline-dev ncurses-dev

RUN pip install -U pip wheel setuptools && pip wheel . --wheel-dir=/wheels


# Second step of build

FROM python:3-alpine as prod

COPY --from=base /wheels /tmp/wheels

COPY  . /source

RUN pip install --no-index --find-links=/tmp/wheels/. . 

This way no dependency is installed on final container.

Moreover if you don't change your setup.py file, I think that docker will use cache and you won't have to rebuild the wheels_builder image.

It should speed up the process of recreating docker images for you project. Installation from wheel is a matter of milliseconds, compared to tens of seconds needed to build from source.

I don't know if it will help you but at least it's a start, you can change it to fit your needs.


推荐阅读