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

6πŸ‘

βœ…

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.

πŸ‘€Rocckk

6πŸ‘

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.

πŸ‘€John Johnson

5πŸ‘

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

πŸ‘€dfrankow

3πŸ‘

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

2πŸ‘

I came across this issue recently, i solved it by adding postgres environment variables to db in my docker-compose.yml file like so

version: '3'
 services:
  db:
     image: 'postgres'
     ports:
       - '5432'
     environment:
            - POSTGRES_DB=booksdb
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
  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
πŸ‘€oseleslie

1πŸ‘

In my specific case, I had postgres without any version, so postgres image gets updated and the db (postgres) container won’t get up

πŸ‘€RobertPro

0πŸ‘

The following configuration works for me from official documentation

version: "3.8"

services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

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

then run those commands:

docker-compose up -d --build   
docker-compose logs    
πŸ‘€Mohamed Sabry

0πŸ‘

docker exec -it "container_name" python3 manage.py migrate

This works for me. Make sure of the path to the manage.py file is correct.

πŸ‘€Gabriel Bueno

Leave a comment