0

Can't connect to Postgres from the docker container. I do not want user docker-compose and create a Postgres container, already got Postgres app running. I think it is a bad idea to container postgres and better use system.

OSX, Postgres10, Flask

I already made

postgresql.conf

listen_addresses = '*'  
port = 5432

pg_hba.conf

host    all             all             0.0.0.0/0               trust

I Used trust for any result, but no effect.

Dockerfile

FROM python:3.8-alpine
RUN apk update \
    && apk add --virtual build-deps gcc musl-dev \
    && apk add python3-dev \
    && apk add postgresql-dev \
    && apk add jpeg-dev zlib-dev libjpeg \
    && pip install --upgrade pip
ENV PYTHONUNBUFFERED 1 \
    && FLASK_APP app.py
#EXPOSE 5000 5432
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
CMD ["./bin/run.sh"]

./bin/run.sh

#!/bin/sh
#python run.py
source venv/bin/activate
set -e
flask db upgrade
exec gunicorn -b --workers 4 --access-logfile --error-logfile run:app :5000

The "docker run" command I try to use:

docker run --rm -e SQLALCHEMY_DATABASE_URI=postgresql://postgres:postgres@0.0.0.0:5432/my_db --net=host -p 5000:5000 my_container:v0.1

the command leads to error

...
psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host "0.0.0.0" and accepting
        TCP/IP connections on port 5432?

command connect me to Postgres

psql -U postgres -h 0.0.0.0
Andrew Louis
  • 360
  • 6
  • 17

1 Answers1

0

Trying to connect to host "0.0.0.0" is technically wrong. (the address 0.0.0.0 is used to other purposes no to connect to...)

Did you tried connect using the postgres container ip address instead of "0.0.0.0"?

You can get the postgres container ip address throught the following command:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' name_of_postgres_container
matiferrigno
  • 901
  • 7
  • 8
  • I do not use Postgres container. I want to connect to the system Postgres. – Andrew Louis Feb 26 '20 at 23:31
  • and yes, I guess system Postgres running on localhost, I can connect to Postgres by command `psql -U postgres -h 0.0.0.0` – Andrew Louis Feb 26 '20 at 23:33
  • Sorry, I didn't read that you didn't docker for postgres. Everyway, so I mean use the postgres system ip address instead connect to 0.0.0.0 ip address. If it didn't work, please give more information about your setup thus we could be helpful to you. – matiferrigno Feb 26 '20 at 23:39
  • You are running psql command into app container or in the same level that postgres is running? – matiferrigno Feb 26 '20 at 23:42
  • @matiferringo yeah, it is much easier easy to create another container I could use docker-compose for that. – Andrew Louis Feb 26 '20 at 23:43