python - 在复制整个项目之前安装 setup.py 依赖项
问题描述
我的 Dockerfile 曾经是这样的:
...
COPY . /source/
RUN pip install -e .
...
但是,我想通过COPY
仅setup.py
在复制其余代码之前完成 docker 构建来最小化完成 docker 构建所需的时间。这样 pip install 可以在两者之间完成,因此不需要在每次代码更改时都进行安装,只有 setup.py 才需要。
...
COPY setup.py /source/
RUN pip install -e .
COPY . /source/
...
您是否发现pip install -e .
在生产 docker 映像中使用在效率方面有任何问题?有更好的方法吗?我想知道,因为我只看到-e
在开发机器上使用过,从未在生产中使用过。
解决方案
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.
推荐阅读
- python - wsl 2 上的 pipenv 自定义脚本:shlex.py instream 对象没有读取属性
- node.js - 将 npm install 的输出复制到 docker 容器
- sql - 我得到 ORA-00928 试图插入新数据
- ember.js - 如何从 Ember 路由模型钩子传递 @tracked 对象
- python - Beautiful Soup:从 p 元素中分离出 span 元素
- maven - 使用 maven pom.xml 生成包含 WAR 文件的 Ear 文件
- php - DATETIME2 到 UNIX 时间戳
- c++ - 为什么 test.h.gcov 将内容显示为 /EOF/
- python - 什么是计算python中两个向量角度余弦的稳健方法?
- php - 根原因拥有的 Laravel Storage Cache 文件夹无法清除缓存