简体   繁体   中英

psycopg2 import error SSL_check_private_key: symbol not found

I am using Docker along with psycopg2 to connect with another database service in my docker-compose. However, I get an import error with psycopg2:

Python 3.8.2 (default, Feb 29 2020, 17:03:31)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/site-packages/psycopg2/__init__.py", line 50, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: Error relocating /usr/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-x86_64-linux-gnu.so: SSL_check_private_key: symbol not found

Here is my Dockerfile I am trying to use psycopg2 with:

FROM osgeo/gdal:alpine-normal-latest

ENV PYTHONUNBUFFERED 1

RUN apk update && apk add \
    --virtual build-deps gcc python-dev musl-dev \
    postgresql-dev \
    libpq \
    python3-dev \
    curl \
    libffi-dev

ADD . /app
WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt

CMD ["sh", "./entry.sh"]

I discovered that the missing symbol should be loaded from libssl.so . This library is not loaded because psycopg2 does not declare it as a dependency. We can force python to load it using the LD_PRELOAD environment variable.

In my case libssl.so is located at /lib/libssl.so.1.1 so I can use this as a workaround:

/sunstrider # LD_PRELOAD=/lib/libssl.so.1.1 python3 manage.py shell
Python 3.8.5 (default, Jul 20 2020, 23:11:29) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import psycopg2
>>>

The issue seems to be that recent alpine images do not have openssl installed automatically. I had the same issue with a custom built Docker image based on alpine and fixed it by installing the openssl package before building/installing our Python dependencies.

It seems as though there was an issue with the docker image I was using. I ended up using thinkwhere/gdal-python:3.7-shippable and I am able to import psycopg2 without any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM