Search code examples
postgresqldocker-composepodman

unable to connect to postgresql when deployed with docker compose


I have a docker-compose file that starts a postgres service as:


services:
  web:
    build:
      context: .
      dockerfile: flask_app/Containerfile
    ports:
      - "5000:80"
    environment:
      DATABASE_URL: postgresql://postgres:1234@db:5432/planning_powerbi_progress
    depends_on:
      - db


  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: 1234
      POSTGRES_DB: planning_powerbi_progress
    ports:
      - "5432:5432"
    volumes:
      -  "./data:/var/lib/postgresql/data:Z"

I start the services with podman-compose up and I can see the containers running:

(powerbi_planning) [lpuggini@eu012vm2055 flask-podman]$ podman ps
CONTAINER ID  IMAGE                              COMMAND     CREATED        STATUS        PORTS                   NAMES
76fd94b8d581  docker.io/library/postgres:latest  postgres    3 minutes ago  Up 3 minutes  0.0.0.0:5432->5432/tcp  flask-podman_db_1
e2b79bf92088  localhost/flask-podman_web:latest  /start.sh   3 minutes ago  Up 3 minutes  0.0.0.0:5000->80/tcp    flask-podman_web_1
(powerbi_planning) [lpuggini@eu012vm2055 flask-podman]$
─

Now I can't connect to the postgresql db in any way. The flask webservice tries to connect with:

import logging
from logging.handlers import RotatingFileHandler
from flask import Flask
from flask import request
from flask import Flask, request, render_template
from pathlib import Path
import shutil
from powerbi_planning.transform_data import gen_df_from_in_memory_file
from sqlalchemy import create_engine, text
import pandas as pd
import os


logger = logging.getLogger(__name__)

app = Flask(__name__)

scriptdir = Path(__file__).resolve().parent
savedir = scriptdir / 'uploaded_files'

#db_engine = create_engine('postgresql://postgres:1234@localhost:5432/powerbi')                                                                                                                                  db_engine = create_engine(os.environ['DATABASE_URL'])

but it fails with error: sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "db" to address: Temporary failure in name resolution.

I am not even able to connect to the db from bash in the host machine as:

(powerbi_planning) [lpuggini@eu012vm2055 flask-podman]$ psql -U postgres
psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
(powerbi_planning) [lpuggini@eu012vm2055 flask-podman]$

How can I solve?


Solution

  • In your code there is a line (but it is commented-out!) that uses localhost:5432. In Docker localhost refers to the current container, not to the current host, unless you are using the Host network driver that has some limitations.

    You are using docker-compose to run your container: it creates automatically a bridge network that connects the services listed in docker-compose.yml, so you can use "db" as domain name in all containers that are connected to that bridge network.

    Since you have exposed port 5432 on the host machine, to connect to the DB from the Docker host machine you can use localhost or the host IP. To connect to the DB from any other container outside of that bridge network you can use the host IP.