I am trying to run a simple Django app with PostgreSQL using docker-compose. The PostgreSQL container runs fine and is accessible but I am unable to reach it from inside the Django container using python.
Here is my docker-compose.yaml
:
version: "3"
services:
db:
image: "postgres"
restart: always
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql/data
django:
build: ./qc_django
ports:
- "8080:8080"
environment:
- DB_NAME=django
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_HOST=db
- DB_PORT=5432
- DEBUG=True
depends_on:
- db
volumes:
pgdata:
Here are the database settings inside of the Django settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': env("DB_NAME"),
'USER': env("DB_USER"),
'PASSWORD': env("DB_PASSWORD"),
'HOST': env("DB_HOST"),
'PORT': env("DB_PORT"),
}
}
The error it spits out after a minute or so:
django.db.utils.OperationalError: could not connect to server: Connection timed out
Is the server running on host "db" (172.21.0.2) and accepting
TCP/IP connections on port 5432?