skip to Main Content

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 CLI
  • api 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?

2

Answers


  1. It is failing to connect to PostgreSQL because it uses port 5432 by default to listen for connections but your are using 5433 in your config files.

    Change this line in your yaml file:

        ports:
          - "5433:5433"
    

    to

        ports:
          - "5432:5432"
    

    And change this line in your settings.py
    'PORT': '5433' to 'PORT': '5432'

    Login or Signup to reply.
  2. 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:

    1. Change the default port by adding a new environment variable to db_api container: PGPORT: 5433.
    2. 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"
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search