前言
昨晚优化完组件,准备今天重新打包部署上去,边上课边打包,结果快一个上午都没成功,在pip install
上摔倒无数次🙃,比如超时、Hash值校验错误。哎,早之前之前先把依赖环境单独打一个镜像了。搜报错信息基本都可以归因到网络问题。整理了一些小tips来提高打包的质量。
Tips
- 使用
--upgrade
来安装最新版本 - 使用
--default-timeout
设置超时时间 - 使用
--no-cache-dir
下载时不使用本地缓存,且可以减小Docker镜像大小
可以看一看Stack Overflow以及Github上的讨论。
- 修改镜像
我之前一直用清华镜像,有一个包下载到一半就报Hash错误。改用阿里云镜像后成功。
清华源镜像:https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云镜像:https://mirrors.aliyun.com/pypi/simple/
- 其他
参见Stack Overflow上另一个帖子,汇总了一些方法。
示例
FROM python:3.8
COPY ./ ./
RUN pip --default-timeout=100 install --upgrade -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir
RUN pip --default-timeout=100 install torch==1.8.1+cpu -i https://mirrors.aliyun.com/pypi/simple/ -f https://download.pytorch.org/whl/torch_stable.html --no-cache-dir