Search code examples
pythonpython-3.xpostgresqldockerpg-dump

pg_dump error in docker but still executes successfully


I have containerized a python script that is meant to run pg_dump on a database and upload the resulting file to a server. The problem I am running into is that I see the message could not find a "pg_dump" to execute in my terminal but it appears to run successfully anyway. For example, if I run the code below I get the output:

could not find a "pg_dump" to execute
pg_dump (PostgreSQL) 11.10

I'd like to figure out why this message is being printed on my terminal and if its indicative of a larger issue that I'm not seeing. I still clearly get my output, as seen above, so I'm pretty confused at what this message means.

Dockerfile:

FROM postgres:11.10-alpine

LABEL maintainer="Foo"

COPY *.py /

ADD requirements.txt /

RUN apk add postgresql-client

# Install python 3
RUN apk add --no-cache python3 \
  && python3 -m ensurepip \
  && pip3 install --upgrade pip setuptools \
  && rm -r /usr/lib/python*/ensurepip && \
  if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
  if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
  rm -r /root/.cache

RUN pip install --no-cache-dir -r /requirements.txt

ENTRYPOINT ["python3", "./main.py"]

Python Code:

# main method ...
command = "pg_dump --version"

process = Popen(command, shell=True, env={
    "PGPASSWORD": password
})
process.wait()

My Command:

docker run -v ~/.aws/:/home/.aws/ -v /tmp/:/tmp/ -e HOME=/home/ <my-image>

Solution

  • Just as you observed, no large issue here.

    The problem is next:

    process = Popen(command, shell=True, env={
        "PGPASSWORD": password
    })
    

    env override the default environment variables in system which make you have issue. In fact, there are 2 pd_dump in your system, one in /usr/local/bin/pg_dump & another in /usr/bin/pg_dump installed by apk add postgresql-client.

    To make your python code's behavior same as the login shell behavior (I mean run the pg_dump with docker exec -it ...), you should change code as next to include default environment variable:

    import os
    process = Popen(command, shell=True, env={**os.environ, "PGPASSWORD": password})