0

i've been trying to use my local postgresql database but for some reason, everytime that i try to get some data with the flask app in a docker container, i get an error connection .

docker-compose.yaml:

version: '3.3'
services:
  web:
    build: .
    ports:
      - 5000:5000

Dockerfile:

FROM python:3.8-alpine
COPY . /app

WORKDIR /app

RUN \
 apk add  postgresql-libs && \
 apk add  --virtual .build-deps gcc musl-dev postgresql-dev

RUN apk add build-base

ENV VIRTUAL_ENV=/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN pip install -r requirements.txt

ENV FLASK_APP=project/app.py

EXPOSE 5000
CMD ["flask", "run", "--host", "0.0.0.0"]

On my flask app configuration, i defined SQLALCHEMY_DATABASE_URI as

pg_user = "postgres"
pg_pwd = "1404"
pg_port = "5433"
SQLALCHEMY_DATABASE_URI = f"postgresql://{pg_user}:{pg_pwd}@localhost:{pg_port}/flaskproject"

And i started the postgresql service:

sudo service postgresql start

However, when i try to get to any endpoint that to access the database, i get an error:

flask-project-web-1  | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5433 failed: Connection refused
flask-project-web-1  |  Is the server running on that host and accepting TCP/IP connections?
flask-project-web-1  | connection to server at "localhost" (::1), port 5433 failed: Address not available      
flask-project-web-1  |  Is the server running on that host and accepting TCP/IP connections?

The strange behaviour is that, if i try to run my server without docker, it works. I also tried connecting with postgree with ubuntu cmd and it worked:

psql -h localhost -p 5433 -d flaskproject -U postgres

I have changed the posgresql.conf file in the following way:

listen_addresses = '*'
port = 5433

I also have changed the pg_gba.conf file in the following way:

local   all             postgres                                trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

Despite all of that, it's still not working.

Leonardo
  • 49
  • 7
  • Presumably, your app tries to connect to PostgreSQL on *its* localhost (which is the Docker container). You need to provide the name / IP address of the host instead. – Frank Schmitt Nov 18 '22 at 11:52
  • Thanks for the reply. How could i get the name of the host? – Leonardo Nov 18 '22 at 11:55
  • 1
    if someone get to this question, just change localhost on SQLALCHEMY_DATABASE_URI to host.docker.internal. – Leonardo Nov 18 '22 at 13:47

0 Answers0