前言

昨晚优化完组件,准备今天重新打包部署上去,边上课边打包,结果快一个上午都没成功,在pip install上摔倒无数次🙃,比如超时、Hash值校验错误。哎,早之前之前先把依赖环境单独打一个镜像了。搜报错信息基本都可以归因到网络问题。整理了一些小tips来提高打包的质量。

Tips

  1. 使用--upgrade来安装最新版本
  2. 使用--default-timeout设置超时时间
  3. 使用--no-cache-dir下载时不使用本地缓存,且可以减小Docker镜像大小

可以看一看Stack Overflow以及Github上的讨论。

  1. 修改镜像

我之前一直用清华镜像,有一个包下载到一半就报Hash错误。改用阿里云镜像后成功。
清华源镜像:https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云镜像:https://mirrors.aliyun.com/pypi/simple/

  1. 其他

参见Stack Overflow上另一个帖子,汇总了一些方法。

示例

  1. FROM python:3.8
  2. COPY ./ ./
  3. RUN pip --default-timeout=100 install --upgrade -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir
  4. 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