postgresqldockerdockerfilepg-dumplibpq

Error loading shared library libldap.so.2: No such file or directory (needed by /usr/local/lib/libpq.so.5)


I am trying to build a docker image of a flask app that works on my laptop. This app is used to generate PostgreSQL dumps with Python. So I need to add pg_dump and libpq.so.5. See below my Dockerfile :

FROM XXX/postgres:13.9-alpine as postgres
FROM XXX/python:3.8-alpine

USER root

WORKDIR /dump_generator_api

COPY requirements.txt ./
RUN python3 -m pip install --upgrade pip
RUN pip3 install -r requirements.txt

COPY --from=postgres /usr/local/bin/pg_dump /usr/local/bin/pg_dump
COPY --from=postgres /usr/local/lib/libpq.so.5 /usr/local/lib/libpq.so.5

ADD . /dump_generator_api
EXPOSE 5000
CMD ["python", "/dump_generator_api/app.py"]

Once I have build my image, I try to apply a pg_dump command (/usr/local/bin # pg_dump -U pgsqladmin -h XXX -p XXX XXX> backup.sql) and I have the following error I don't understand :

Error loading shared library libldap.so.2: No such file or directory (needed by /usr/local/lib/libpq.so.5)
Error relocating /usr/local/lib/libpq.so.5: ldap_err2string: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_result: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_simple_bind: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_count_entries: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_unbind: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_set_option: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_search_st: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_value_free_len: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_get_values_len: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_first_entry: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_init: symbol not found
Error relocating /usr/local/lib/libpq.so.5: ldap_msgfree: symbol not found

Do you know how I can solve it ? I tried different docker images and same errors occur. I don't know where and how I should add this libldap.so.2.


Solution

  • Copying pg_dump and libpq.so.5 from the postgres image will not work, each binary depends on other packages.

    You should just install postgres on your image using "apk add postgresql" it would be installed along with everything that it needs.

    The file would look like:

    FROM XXX/python:3.8-alpine
    
    USER root
    
    WORKDIR /dump_generator_api
    
    COPY requirements.txt ./
    RUN python3 -m pip install --upgrade pip
    RUN pip3 install -r requirements.txt
    
    RUN apk update && apk add postgresql
    
    ADD . /dump_generator_api
    EXPOSE 5000
    CMD ["python", "/dump_generator_api/app.py"]