Docker镜像中无法使用本地.egg-info包的问题求助
问题背景
我通过setup.py创建了后缀为.egg-info的Python本地包power_manager,执行pip install -e .在本地安装成功。但因为包含公司代码,无法上传至PyPI。
现有配置文件如下:
setup.py
from setuptools import setup, find_packages setup( name='power_manager', version='0.1', packages=find_packages(), install_requires=[ "paho-mqtt==1.6.1" ], )
初始requirements.txt
paho-mqtt==1.6.1 git+https://github.deere.com/EPVV/ODS.git@c3ba6d5289fe05d5a60c8eab26b23ad01ff71ed6#egg=power_manager&subdirectory=power_management
Dockerfile
FROM python:3.8-alpine WORKDIR /app COPY ./requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY ./ /app CMD ["python","-u", "./main_controller.py"]
构建错误1
使用初始配置构建镜像时,因容器内未安装git报错:
❯ docker build -t ods_main . Sending build context to Docker daemon 9.148MB Step 1/6 : FROM docker.deere.com/python:3.8-alpine ---> f36c9f755d28 Step 2/6 : WORKDIR /app ---> Using cache ---> 44a76dc3b4a9 Step 3/6 : COPY ./requirements.txt . ---> 972c55049f3c Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in 073cabb46b7a Collecting power_manager Cloning https://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:****@github.deere.com/EPVV/ODS.git (to revision c3ba6d5289fe05d5a60c8eab26b23ad01ff71ed6) to /tmp/pip-install-zmr7tyb7/power-manager_c5bb17f18b8440729603b00733c66ec6 ERROR: Error [Errno 2] No such file or directory: 'git' while executing command git version ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH? [notice] A new release of pip is available: 23.0.1 -> 24.0 [notice] To update, run: pip install --upgrade pip The command '/bin/sh -c pip install --no-cache-dir -r requirements.txt' returned a non-zero code: 1
构建错误2
修改requirements.txt为本地路径后,因容器内不存在该路径报错:
❯ docker build -t testing_power_manager . Sending build context to Docker daemon 7.413MB Step 1/8 : FROM docker.deere.com/python:3.8-alpine ---> f36c9f755d28 Step 2/8 : WORKDIR /app ---> Using cache ---> 44a76dc3b4a9 Step 3/8 : COPY ./requirements.txt . ---> 6319e22ee559 Step 4/8 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in 53da6ad9984f ERROR: /home/y3qucfm/workspace/power_management is not a valid editable requirement. It should either be a path to a local project or a VCS URL (beginning with bzr+http, bzr+https, bzr+ssh, bzr+sftp, bzr+ftp, bzr+lp, bzr+file, git+http, git+https, git+ssh, git+git, git+file, hg+file, hg+http, hg+https, hg+ssh, hg+static-http, svn+ssh, svn+http, svn+https, svn+svn, svn+file). [notice] A new release of pip is available: 23.0.1 -> 24.0 [notice] To update, run: pip install --upgrade pip The command '/bin/sh -c pip install --no-cache-dir -r requirements.txt' returned a non-zero code: 1
可行解决方案
方案1:直接在容器内安装本地包(推荐)
无需依赖git或外部路径,直接将本地包代码复制到容器内执行安装:
- 修改requirements.txt:仅保留第三方依赖,移除
power_manager相关条目:
paho-mqtt==1.6.1
- 调整Dockerfile:先复制本地包的核心文件,再安装包和依赖:
FROM python:3.8-alpine # 可选:若包需要编译依赖,安装系统编译工具 RUN apk add --no-cache gcc musl-dev WORKDIR /app # 复制本地包的必要文件 COPY ./setup.py . COPY ./power_manager/ ./power_manager/ COPY ./power_manager.egg-info/ ./power_manager.egg-info/ # 安装本地包 RUN pip install --no-cache-dir . # 安装第三方依赖 COPY ./requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # 复制主程序代码 COPY ./main_controller.py . CMD ["python","-u", "./main_controller.py"]
注意:若
power_manager包含资源文件等其他内容,需同步复制到容器对应路径。
方案2:预构建wheel包后安装
本地先构建wheel格式的包文件,再复制到容器内安装:
- 本地构建wheel包:在项目根目录执行:
pip install wheel python setup.py sdist bdist_wheel
执行后会生成dist/目录,包含.whl格式的包文件(如power_manager-0.1-py3-none-any.whl)。
2. 修改Dockerfile:
FROM python:3.8-alpine WORKDIR /app # 复制wheel包和依赖文件 COPY ./dist/power_manager-0.1-py3-none-any.whl . COPY ./requirements.txt . # 安装wheel包和第三方依赖 RUN pip install --no-cache-dir power_manager-0.1-py3-none-any.whl -r requirements.txt # 复制主程序 COPY ./main_controller.py . CMD ["python","-u", "./main_controller.py"]
方案3:修复git依赖问题(不推荐)
若坚持使用git方式安装,需在Alpine镜像中先安装git:
FROM python:3.8-alpine WORKDIR /app # 安装git RUN apk add --no-cache git COPY ./requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY ./ /app CMD ["python","-u", "./main_controller.py"]
缺点:会增加镜像体积,且需确保容器能访问公司内部git仓库(可能需配置凭证)。
内容的提问来源于stack exchange,提问作者Adriana Treviño
相关产品推荐
相关产品推荐

