使用python:3.8-alpine构建Docker镜像时提示找不到git命令报错求助
报错解决方法
根本原因
你使用的python:3.8-alpine为极简基础镜像,默认未预装git工具,而你的requirements.txt中pysentimiento依赖需要从git仓库拉取安装,pip执行安装操作时找不到git命令就会触发该报错。另外requirements.txt中误写了系统依赖git==2.25.1,git属于系统工具不是PyPI的Python包,无法通过pip安装。
具体操作步骤
- 第一步:修改Dockerfile,在安装Python依赖前先安装git等必要系统依赖
修改后的完整Dockerfile如下:
# Set base image (host OS) FROM python:3.8-alpine # 安装git、编译工具等系统依赖,避免后续Python包安装失败 RUN apk add --no-cache git gcc musl-dev linux-headers # By default, listen on port 5000 EXPOSE 5000/tcp # Set the working directory in the container WORKDIR /app # Copy the dependencies file to the working directory COPY requirements.txt . # Install any dependencies RUN pip install -r requirements.txt # Copy the content of the local src directory to the working directory COPY app.py . # Specify the command to run on container start CMD [ "python", "./app.py" ]
如果后续安装PyTorch、numpy等二进制包仍有兼容性问题,可以将基础镜像替换为兼容性更好的debian系镜像python:3.8-slim,此时安装系统依赖的命令改为RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*即可。
- 第二步:修改requirements.txt,删除如下错误行
git==2.25.1
- 第三步(可选,避免运行时报错):修正app.py中的笔误
将代码中utterance = request.arge.get("utterance")改为utterance = request.args.get("utterance"),否则接口调用时会触发属性不存在的报错。
完成以上修改后重新执行docker build命令即可正常构建镜像。
内容的提问来源于stack exchange,提问作者Reza Afra
相关产品推荐
相关产品推荐

