Troubling while deploying my django app on render with dockerized dataabase

I am having trouble while deploying my Django application on render. I used the render Postgres database which expired and deleted. So, that's why I am trying to use a dockerized database so that it doesn't have any time limit and database accessibility for a lifetime. After containerizing my Django application, it works well on my local machine and I could push it on the docker hub. Whatever, as my application was pre-deployed and configured with the main branch. I am having this error:

OperationalError at /

could not translate host name "db_social" to address: Name or service not known

Request Method:     GET
Request URL:    https://network-project-5q7j.onrender.com/
Django Version:     5.0
Exception Type:     OperationalError
Exception Value:    

could not translate host name "db_social" to address: Name or service not known

Exception Location:     /opt/render/project/src/.venv/lib/python3.11/site-packages/psycopg2/__init__.py, line 122, in connect
Raised during:  network.views.index
Python Executable:  /opt/render/project/src/.venv/bin/python3.11
Python Version:     3.11.3
Python Path:    

['/opt/render/project/src',
 '/opt/render/project/src/.venv/bin',
 '/opt/render/project/python/Python-3.11.3/lib/python311.zip',
 '/opt/render/project/python/Python-3.11.3/lib/python3.11',
 '/opt/render/project/python/Python-3.11.3/lib/python3.11/lib-dynload',
 '/opt/render/project/src/.venv/lib/python3.11/site-packages']

Server time:    Fri, 23 Aug 2024 10:12:12 +0000

This is what I got on render.

I would like to share my docker-compose.yml

version: "3.11"

services:
  db_social:
    image: postgres
    volumes:
      - ./data/db_social:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=social 
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=qnr63363

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db_social

My dockerfile here:

FROM python:3.11
ENV PYTHONUNBUFFERED=1
WORKDIR /code/
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

Here's my settings.py file's database configuration.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'social',
        'USER': 'postgres',
        'PASSWORD': 'qnr63363',
        'HOST': 'db_social',  
        'PORT': '5432',
    }
    # {
    #     'ENGINE': 'django.db.backends.postgresql_psycopg2',
    #     'NAME': 'postgres',
    #     'USER': 'postgres',
    #     'HOST': '127.0.0.1',
    #     'PORT': '5432',
    #     'PASSWORD': 'qnr63363'
    # }
}

Did I skip something? What's the problem?

You should set container_name to your services, especially your database service. If you don't, it will create a derivative name and you can't expect it to be what you expect it to be.

...
services:
  db_social:
    image: postgres
    container_name: db_social
...
Back to Top