Search code examples
pythondockerdockerfileazure-pipelinesrequirements.txt

Getting "The command '/bin/sh -c pip install --no-cache-dir -r requirements.txt' returned a non-zero code: 1" while building an image from dockerfile


here is my 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

here is my 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"]
  • done all the versions upgrades and downgrades of the installed modules
  • tried with python 3.8.2.final.0 && 3.10 python interpreter
  • what to do? any leads would be appreciated..!!

Solution

  • I tried to install your Python dependencies in a Docker environment and I identified an error while installing the psycopg2 package.

    The reason is this package relies on two core dependencies:

    • libpq-dev
    • gcc

    But the Docker base image you use python:3.10-slim does not contain these core dependencies natively. You must declare their installation from your Dockerfile like so:

    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"]
    

    UPDATE: Investigation steps

    1. Connect to Docker container running python:3.10-slim image:
    docker run -it --rm python:3.10-slim /bin/bash
    
    1. Write requirements.txt file with adapted content:
    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
    
    1. Run pip command:
    pip install --no-cache-dir -r requirements.txt
    
    1. Catch first error related to missing libpq-dev package
    2. Installing libpq-dev:
    apt update -y && apt-get install -y libpq-dev
    
    1. Run pip command again
    2. Catch second error related to missing gcc package