Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

490
Visualizações
Unable to Connect from Flask to Postgres Docker Container

I'm trying to connect a python flask application to a database inside a postgres docker container.

I have the below database.conf:

POSTGRES_USER=testdbuser
POSTGRES_PASSWORD=testdbpass
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=testdb

and config.py:

import os

user = os.environ['POSTGRES_USER']
password = os.environ['POSTGRES_PASSWORD']
host = os.environ['POSTGRES_HOST']
database = os.environ['POSTGRES_DB']
port = os.environ['POSTGRES_PORT']

DATABASE_CONNECTION_URI = f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}'

and finally docker-compose.yml:

version: '3.5'
services:
  database:
    container_name: postgres-test
    image: postgres:latest
    env_file: database.conf
    ports:
      - 5432:5432
    volumes:
      - postgres-data:/var/lib/postgresql/data

volumes:
  postgres-data:

The error I'm getting after docker-compose up --build -d -> export $(xargs < database.conf) -> export FLASK_APP=app.py -> flask run is:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "postgres" to address: nodename nor servname provided, or not known
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The error itself gives insight:

could not translate host name "postgres" to address: nodename nor servname provided, or not known

The host postgres does not exist in DNS or your local network. SQLAlchemy is unable to connect to it. This logically the case, since you created a docker container and forwarded the port 5432 to your local machine.

There are 2 choices you can take - either use the localhost address to connect from the flask app or run the flask app as a docker-compose service.

Option 1 - use localhost

The POSTGRES_HOST environment variable does not seem to be used by the postgres docker image. So you can just change this in the environment file:

POSTGRES_USER=testdbuser
POSTGRES_PASSWORD=testdbpass
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=testdb

Option 2 - run flask using docker-compose

For this, you will need to create a simple Dockerfile and add the flask app to the docker-compose.yml file.

I suggest something simple for the Dockerfile like so:

FROM python

COPY . /app
# How you install your python packages may differ
RUN pip install -r /app/requirements.txt

# Ensure the path here is correct
ENV FLASK_APP /app/app.py

CMD flask run

Then you will need to add this to the docker-compose.yml file:

version: '3.5'
services:
  database:
    container_name: postgres-test
    image: postgres:latest
    env_file: database.conf
    ports:
      - 5432:5432
    volumes:
      - postgres-data:/var/lib/postgresql/data

  flask:
    build:
      context: .
      dockerfile: Dockerfile
    environment:  # or use env_file as you did above
      POSTGRES_USER: testdbuser
      POSTGRES_PASSWORD: testdbpass
      POSTGRES_HOST: database     # This is the name of the database service in this file above
      POSTGRES_PORT: 5432
      POSTGRES_DB: testdb
    depends_on:
      - database

volumes:
  postgres-data:

Hope this gets you going.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda