docker打包本地python项目为镜像时,排错记录

docker打包本地python项目为镜像时,排错记录_第1张图片

项目的结构如下,python文件均放在py目录下,dockerfile如下:

docker打包本地python项目为镜像时,排错记录_第2张图片

(tips: 通过pip freeze> requirements.txt 命令生成的requirements文件会包含更多包,如果想让镜像精简,只需要将你项目中用到的包及其依赖包下载即可)

下面讲解

将python项目打包成docker镜像过程中的一些排错经验:

出错1:

ImportError: libGL.so.1: cannot open sharedobject file: No such file or directory

原因:docker容器缺少cv2的依赖包

解决:需要提前安装,将

RUN apt-get update

RUN apt-get install 包 -y

添加到dockerfile

出错2:

ERROR: Could not find a version thatsatisfies the requirement xxxx==xxxx

ERROR: No matching distribution found for xxxx==xxxx

ERROR: Service 'web' failed to build : Thecommand '/bin/sh -c pip install -r requirements.txt' returned a non-zero code:1

原因:无法正确下载requirements相应包

解决:

思路1:换源,提高下载速度,推荐源有:

清华源:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云源:http://mirrors.aliyun.com/pypi/simple/

中国科技大学源:https://pypi.mirrors.ustc.edu.cn/simple/

思路2:选择的源没有对应版本的包

cd到requirements文件目录,本地下载试试,执行

pip download -d pgs -r requirments.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

可以看到包的对应版本,如果你填的包在对应源上不存在,修改requirements文件为其他满足项目要求的版本(或者换源),即可

(下载成功后记得删除pgs文件夹)

思路3:

此时本地可以下载requirements包,但docker生成镜像仍报错,提示包不存在

需要重启docker:systemctlrestart docker

再试一次

出错3:

  1. Docker save保存镜像时报错:

Error response from daemon: file integritychecksum failed for "nginx-ingress-controller"

原因:docker缓存影响

解决:

深度清理缓存

(注意有风险,因为这个命令只会保留在使用的镜像,容器,数据卷,所以需谨慎)

sudo docker system prune -a

重新打包镜像,加上—no-cache

sudo docker build --no-cache -f Dockerfile -t invoke-aqm:1.0 .

你可能感兴趣的:(docker,python,容器)