services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "5432:5432"
backend:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
This is the docker config. The issue is backend(Django) is not able to connect to the postgres DB. I tried with DB host as localhost and it was not able to connect.
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Then I changed host to the ip address of the db container but still no success, this time it was a timeout error.
could not connect to server: Connection timed out
Is the server running on host "172.11.0.2" and accepting
TCP/IP connections on port 5432?
This is how I'm connecting to postgres.
from sqlalchemy import create_engine
engine = create_engine("postgresql+psycopg2://postgres:postgres@172.11.0.2:5432/postgres",
poolclass=QueuePool,
pool_size=5,
max_overflow=10,
pool_timeout=1)
I think this has definitely something to do with the two containers being different but could someone help me on how to connect to Postgres container from Django's container?