Issue
I'm using macOS, building a Django app with Postgres and I want to containerise it with Docker.
I have following files:
Dockerfile:
FROM python:3.11
WORKDIR /medicin_api
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python3", "./manage.py", "runserver", "8001"]
docker-compose.yaml:
version: latest
services:
db_api:
image: postgres:16
restart: always
container_name: db_api
ports:
- "5433:5433"
environment:
- POSTGRES_USER=api_user
- POSTGRES_DB=medicin_patients
- POSTGRES_PASSWORD=Baz#Dan&Api!
volumes:
- postgres:/data/postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U api_user -d medicin_patients"]
interval: 5s
timeout: 2s
retries: 10
api:
container_name: api
build: .
command: python manage.py runserver 0.0.0.0:8001
volumes:
- .:/api_volume
ports:
- "8001:8001"
depends_on:
db_api:
condition: service_healthy
volumes:
postgres:
PostgreSQL setup in django settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'medicin_patients',
'USER': 'api_user',
'PASSWORD': 'Baz#Dan&Api!',
'HOST': 'db_api',
'PORT': '5433'
}
}
When I run the command: docker-compose up -d --build
I'm getting two images & containers, where:
db_api
container is running properly with PostgreSQL database and I can access it via CLIapi
container is running however it throws me below error which I cannot solve:
2023-12-10 12:32:25 Watching for file changes with StatReloader
2023-12-10 12:32:25 Exception in thread django-main-thread:
2023-12-10 12:32:25 Traceback (most recent call last):
2023-12-10 12:32:25 File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 275, in ensure_connection
2023-12-10 12:32:25 self.connect()
2023-12-10 12:32:25 File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
2023-12-10 12:32:25 return func(*args, **kwargs)
2023-12-10 12:32:25 ^^^^^^^^^^^^^^^^^^^^^
2023-12-10 12:32:25 File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 256, in connect
2023-12-10 12:32:25 self.connection = self.get_new_connection(conn_params)
2023-12-10 12:32:25 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-12-10 12:32:25 File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
2023-12-10 12:32:25 return func(*args, **kwargs)
2023-12-10 12:32:25 ^^^^^^^^^^^^^^^^^^^^^
2023-12-10 12:32:25 File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
2023-12-10 12:32:25 connection = self.Database.connect(**conn_params)
2023-12-10 12:32:25 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-12-10 12:32:25 File "/usr/local/lib/python3.11/site-packages/psycopg/connection.py", line 745, in connect
2023-12-10 12:32:25 raise last_ex.with_traceback(None)
2023-12-10 12:32:25 psycopg.OperationalError: connection failed: Connection refused
2023-12-10 12:32:25 Is the server running on that host and accepting TCP/IP connections?
What am I missing here or have wrongly setup that my Django app is not connecting with PostgreSQL?
Solution
It looks like the problem you described is caused by wrong port that you exposed from the Postgresql container. As mentioned above the Postgresql operates by default on port 5432
. So, have two options:
- Change the default port by adding a new environment variable to
db_api
container:PGPORT: 5433
. - Since, the port
5432
is occupied on your host machine, you can change the mapping without changing the configuration of Posgresql instance, by changing the port mapping:- "5433:5432"
Answered By - Ilya Khrustalev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.