Django docker - could not translate host name "db" to address: nodename nor servname provided, or not known

So you are trying to reach the db which is running in one container from another container? If yes - the following could potentially help, at least it helped me when I had similar issues.

Try to define networks config in addition to links in your compose file, create a network with some name and define it in both services. Like described here, as the docks on links config recommend to do that.

Something like this for your case:

version: '3'
 services:
  db:
     image: 'postgres'
     ports:
       - '5432'
     networks:
      some_network:
  core:
    build:
      context: .
      dockerfile: Dockerfile
    command: python3 manage.py runserver 0.0.0.0:8000
    ports:
      - '8000:8000'
    volumes:
      - .:/code
    depends_on:
      - db
    links:
      - db:db
    networks:
      some_network:
  networks:
   some_network:

It helped me to resolve the host name to connect to the db.


I had to use docker-compose up to make sure my db container was running.


I've been searching for several days for the solution to this issue. Here's what I did:

1 - I copied the postgresql.conf.sample file from my postgres container to my project folder you must get in to cd usr/share/postgresql with docker exec -it yourcontainer bash

2 - I changed its name to postgresql.conf

3 - I modified these variables in postgresql.conf

    listen_addresses = '*'
    port = 5432             
    max_connections = 100           
    superuser_reserved_connections = 3
    unix_socket_directories = '/tmp'

4 - I added it to my Dockerfile

COPY postgresql.conf      /tmp/postgresql.conf

5 - settings.py

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

6 - I did docker-compose up -build again

Tags:

Docker

Django