从 dockerfile 构建映像时获取“命令 '/bin/sh -c pip install --no-cache-dir -rrequirements.txt' 返回非零代码:1”

Shi*_*mar 3 python requirements.txt docker dockerfile azure-pipelines

这是我的要求.txt

beautifulsoup4==4.11.1
cachetools==5.2.0
certifi==2022.12.7
charset-normalizer==2.1.1
click==8.1.3
colorama==0.4.6
Flask==2.2.2
Flask-SQLAlchemy==3.0.2
google==3.0.0
google-api-core==2.10.2
google-auth==2.14.1
google-cloud-pubsub==2.13.11
googleapis-common-protos==1.57.0
greenlet==2.0.1
grpc-google-iam-v1==0.12.4
grpcio==1.51.1
grpcio-status==1.51.1
idna==3.4
importlib-metadata==5.2.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
NotFound==1.0.2
proto-plus==1.22.1
protobuf==4.21.12
psycopg2==2.9.5
pyasn1==0.4.8
pyasn1-modules==0.2.8
requests==2.28.1
rsa==4.9
six==1.16.0
soupsieve==2.3.2.post1
SQLAlchemy==1.4.45
urllib3==1.26.13
Werkzeug==2.2.2
zipp==3.11.0

Run Code Online (Sandbox Code Playgroud)

这是我的 Dockerfile

FROM python:3.10-slim

# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./


# Install production dependencies.
RUN pip install --no-cache-dir -r requirements.txt

CMD ["python", "-u", "main.py"]
Run Code Online (Sandbox Code Playgroud)
  • 完成已安装模块的所有版本升级和降级
  • 尝试使用 python 3.8.2.final.0 && 3.10 python 解释器
  • 该怎么办?任何线索将不胜感激..!!

Mar*_*ian 5

我尝试在 Docker 环境中安装 Python 依赖项,但在安装包时发现了错误psycopg2

原因是这个包依赖于两个核心依赖项:

  • libpq-dev
  • gcc

但您使用的 Docker 基础镜像python:3.10-slim本身并不包含这些核心依赖项。您必须从 Dockerfile 中声明它们的安装,如下所示:

FROM python:3.10-slim

# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Install core dependencies.
RUN apt-get update && apt-get install -y libpq-dev build-essential

# Install production dependencies.
RUN pip install --no-cache-dir -r requirements.txt

CMD ["python", "-u", "main.py"]
Run Code Online (Sandbox Code Playgroud)

更新:调查步骤

  1. 连接到运行python:3.10-slim镜像的 Docker 容器:
FROM python:3.10-slim

# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

# Install core dependencies.
RUN apt-get update && apt-get install -y libpq-dev build-essential

# Install production dependencies.
RUN pip install --no-cache-dir -r requirements.txt

CMD ["python", "-u", "main.py"]
Run Code Online (Sandbox Code Playgroud)
  1. 写入requirements.txt包含调整内容的文件:
docker run -it --rm python:3.10-slim /bin/bash
Run Code Online (Sandbox Code Playgroud)
  1. 运行pip命令:
cat << EOF > requirements.txt
beautifulsoup4==4.11.1
cachetools==5.2.0
certifi==2022.12.7
charset-normalizer==2.1.1
click==8.1.3
colorama==0.4.6
Flask==2.2.2
Flask-SQLAlchemy==3.0.2
google==3.0.0
google-api-core==2.10.2
google-auth==2.14.1
google-cloud-pubsub==2.13.11
googleapis-common-protos==1.57.0
greenlet==2.0.1
grpc-google-iam-v1==0.12.4
grpcio==1.51.1
grpcio-status==1.51.1
idna==3.4
importlib-metadata==5.2.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
NotFound==1.0.2
proto-plus==1.22.1
protobuf==4.21.12
psycopg2==2.9.5
pyasn1==0.4.8
pyasn1-modules==0.2.8
requests==2.28.1
rsa==4.9
six==1.16.0
soupsieve==2.3.2.post1
SQLAlchemy==1.4.45
urllib3==1.26.13
Werkzeug==2.2.2
zipp==3.11.0

EOF
Run Code Online (Sandbox Code Playgroud)
  1. 捕获与丢失libpq-dev包相关的第一个错误
  2. 安装libpq-dev
pip install --no-cache-dir -r requirements.txt
Run Code Online (Sandbox Code Playgroud)
  1. 再次运行pip命令
  2. 捕获与丢失gcc包相关的第二个错误

  • @ShivamKumar,如果它可以帮助我用我遵循的调查步骤更新了我的答案 (2认同)