skip to Main Content

I am currently on a project based around react in the frontend, django in the backend, postgresql for the database and nginx for the reverse proxy. Everything is linked up with a Docker-compose file.

Right now, I try to link my postgresql database to django.

However every time I try to launch my network with the docker-compose CLI, it fails with the migration of data in the django backend container with the following error:

#0 0.697 django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known

I do not understand why I have this error. When I try to check if the database is up, it works without any problem:

docker-compose up -d db     
Creating network "docker_nginx_django_react_template_appNetwork" with driver "bridge"
Creating db ... done
docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS         PORTS                                       NAMES
_   postgres:latest   "docker-entrypoint.s…"   5 seconds ago   Up 4 seconds   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   db

Here is my docker-compose file :

version: '3.8'

services:
  nginx:
    container_name: app_server_conf
    build:
      context: ./
      dockerfile: ./Dockerfile
    ports:
      - "80:80"
    environment:
      - PUBLIC_URL=127.0.0.1
    volumes:
      - staticfiles:/staticfiles
    networks:
      - appNetwork
    depends_on:
      - backend    
  db:
    image: postgres:latest
    container_name: db
    restart: always
    ports:
      - "5432:5432"
    networks:
      - appNetwork
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_USER: test
      POSTGRES_PASSWORD: test
    volumes:
      - postgresql:/var/lib/postgresql/data
    healthcheck:
      test: pg_isready -U postgres

  backend:
    container_name: backend
    ports:
      - "8000:8000"
    build:
      context: backend
      dockerfile: ./Dockerfile
    command: "make run"
    networks:
      - appNetwork
    volumes:
      - staticfiles:/app/static/
    depends_on:
      db:
        condition: service_healthy
    links:
      - db:db

volumes:
  postgresql:
  staticfiles:

networks:
  appNetwork:
    driver: bridge

And here is my part of settings.py :

DATABASES = {
    'default': {
        'HOST':'db',
        "ENGINE": "django.db.backends.postgresql",
        'NAME': 'backend_db',
        'USER': 'test',
        'PASSWORD': 'test',
        'PORT': '5432',
    }
}

Unfortunately, after many researches on the internet, I am completely lost. What seems weird to me, is I can see the database in the network, like the result below. But even with a hard clean with docker system prune --force, it did nothing.

docker network inspect docker_nginx_django_react_template_appNetwork
[
    {
        "Name": "docker_nginx_django_react_template_appNetwork",
        ...,
        "Containers": {
            "cb7fdbfc4efe91254e95c02ca680315d488b0c6426ea90b2f682b74bf18eef62": {
                "Name": "db",
                "EndpointID": "endpointid",
                "MacAddress": "mac_address",
                "IPv4Address": "ip_address/20",
                "IPv6Address": ""
            }
        },
        ...
    }
]

2

Answers


  1. Inside of your settings.py of the django code you have to refine HOST and NAME.

    I am no expert on that but I’ve had similar struggles. Maybe this helps you? Read about HOST and NAME.

    Sorry for the vague answer but I am not able to provide the plug and play answer. Hope the resources and the hint where the problem most probably lays helps anyways.

    Login or Signup to reply.
  2. Another try to help:

      db:
        image: postgres:latest
        container_name: db
        restart: always
        ports:
          - "5432:5432"
        networks:
          - appNetwork
        environment:
          POSTGRES_HOST_AUTH_METHOD: trust
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: backend_db     <--- put NAME of settings.py
        volumes:
          - postgresql:/var/lib/postgresql/data
        healthcheck:
          test: pg_isready -U postgres
    

    Another option I would try is to set the container_name to backend_db. I do not know how the container_name interferes with the environment variable POSTGRES_DB.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search