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
Asked Answered
C

4

5

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..!!
Calumnious answered 30/12, 2022 at 7:35 Comment(3)
what's the full error you're seeing?Overstep
A way to check and debug this is to do the pip install step manually. Temporarily comment it out from your Dockerfile, let it build the image, then try running a container and doing the pip install manually. You'll be able to see the errors more easily. Also, it may also be that it's failing to install one of the packages, so you could try installing them one by one to see which package is failing to install.Ylla
In any case, "returned a non-zero exit code" is the generic error when most command fails, not specific to pip. There's probably more info in either the Docker or the pip logs... which you need to find and post here.Ylla
S
7

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
Stuck answered 30/12, 2022 at 8:43 Comment(2)
Thanks a lot, it worked..!! it will be very helpful if you can share how you got to know this error. it will be a great helpCalumnious
@ShivamKumar if it can help I updated my answer with the steps I followed for investigationStuck
V
1

I have also faced this error. It occurs when the version you have defined is out of the package context. For example: if the latest version of package transformers is 4.43.3 but you mistyped it as 4.443.3. In this case, the error will occur. So check the versions carefully before listing them in the requirements.txt file.

Vinnievinnitsa answered 3/8 at 15:37 Comment(0)
I
0

In my case, I put wrong version for some packages in the requirements.txt file! Just take a look deeply in the error message, you will find the package with mismatch version.

Infinitive answered 25/11, 2023 at 16:22 Comment(0)
I
0

Make sure that you are running the Docker build command from the correct directory where the requirements.txt file is located. You can use the cd command to change the directory before running the Docker build.

cd /path/to/your/project docker build -t your_image_name .

Intolerance answered 31/12, 2023 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.