1

I am building a Flask application in Python. I'm using SQLAlchemy to connect to PostgreSQL.

I used this to successfully connect my SQLALchemy in the flask app to my postgresql.

engine = create_engine('postgresql://postgres:[mypassword]@localhost:5432/employee-manager-db')

However, I run into an error when I try to do docker-compose up. This is my docker-compose.yml

version: '3.8'
services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8000:8000
    volumes:
      - .:/app

    depends_on:
      - db

  db:
    image: postgres:14.5
    restart: always
    expose:
      - '5432'
    volumes:
      - .dbdata:/var/lib/postgresql
    ports:
      - 'db:5432'

I don't know why it is not recognizing db as an object.

Edit: I had the same problem as this (Docker: Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?) and tried to fix it, but I am currently facing this error instead. Thank you so much for your help

NendoRiki
  • 51
  • 7

1 Answers1

2

The ports option for a service is a list of <host_port>:<container_port> mappings. Both the <host_port> and <container_port> parts need to be integers, which db is not.

You probably want:

  db:
    image: postgres:14.5
    restart: always
    expose:
      - '5432'
    volumes:
      - .dbdata:/var/lib/postgresql
    ports:
      - '5432:5432'
larsks
  • 277,717
  • 41
  • 399
  • 399
  • When I do that, I get an error saying "Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Address not available. (Background on this error at: https://sqlalche.me/e/14/e3q8). employee-manager_backend_1 exited with code 1. employee-manager_db_1 exited with code 0 " – NendoRiki Aug 30 '22 at 01:32
  • @NendoRiki So your original problem is solved. now you have a different problem, and question. – jjanes Aug 30 '22 at 01:48
  • I think this answer accurately identifies the problem in your question. If you have a follow-up, mark this as accepted and open a new question. – larsks Aug 30 '22 at 02:03
  • Ok I will just accept this one. But as I showed in the edit, the answer just took me back to the original problem – NendoRiki Aug 30 '22 at 02:23