Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

django

I'm relatively new to Django and Docker and am following a tutorial to build a mini application. However, I'm getting stuck with the following error:

django.db.utils.OperationalError: could not translate host name "db" to address: nodename nor servname provided, or not known

My docker-compose file looks as follows:

 version: '3'
 services:
  db:
     image: 'postgres'
     ports:
       - '5432'
  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

My settings.py file contains the database:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'postgres',
    'USER': 'postgres',
    'HOST': 'db',
    'PORT': 5432,
}
}

I've seen the post here and here however both have not fixed the issue.

Would appreciate some guidance. Thanks.

like image 773
shbfy Avatar asked Mar 09 '19 20:03

shbfy


4 Answers

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.

like image 172
Rocckk Avatar answered Nov 19 '22 15:11

Rocckk


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

like image 28
dfrankow Avatar answered Nov 19 '22 15:11

dfrankow


Instead of redefining the networks, use

docker-compose down -v

to stop all containers and remove all cached data.

Then

docker-compose up

to restart from scratch.

like image 4
John Johnson Avatar answered Nov 19 '22 14:11

John Johnson


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

like image 3
Luis Angel Mejia Poot Avatar answered Nov 19 '22 14:11

Luis Angel Mejia Poot