Home > Net >  Docker not recognising psycopg2-binary as psycopg2
Docker not recognising psycopg2-binary as psycopg2

Time:01-13

Using pipenv I have the following pipfile:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
flask = "*"
uuid = "*"
gunicorn = "*"
psycopg2-binary = "*"

[dev-packages]

[requires]
python_version = "3.8"

And then my Dockerfile is set up as follows:

FROM python:3.8.3-slim-buster

RUN useradd deploy_trial

WORKDIR /home/deploy_trial

RUN pip install pipenv

COPY . /home/deploy_trial/

RUN pipenv install --deploy

CMD ["python","./app/text.py"]

However, although the build seems to go successfully, when I run the image I get an error message saying:

Traceback (most recent call last):
  File "./app/text.py", line 1, in <module>
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

So it obviously thinks that psycopg2 hasn't been installed... This is really strange because when I have used psycopg2-binary on my local machine and all of my programs there recognise the installation as psycopg2 when the code is run. Does anyone know how to fix this?

CodePudding user response:

My dockerfile:

FROM python:3.8.3-slim-buster

RUN python -m pip install pipenv

COPY script.py script.py
COPY Pipfile Pipfile
RUN pipenv install

CMD ["pipenv", "run", "python", "script.py"]

script.py:

import psycopg2

print('it is ok')

Pipfile:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
psycopg2-binary = "*"

[dev-packages]

[requires]
python_version = "3.8"

It works with such configuration.

  •  Tags:  
  • Related