19

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.

shbfy
  • 2,075
  • 3
  • 16
  • 37
  • Can you keep postgres container running and restart only django container and tell me if you still getting the same error ? – Mostafa Hussein Mar 09 '19 at 20:48
  • Thanks for your comment Mostafa. Apologies I'm still quite new to Docker so may need a hand doing that. I've stopped docker. How can I just get the Postgres container running? Thanks. – shbfy Mar 09 '19 at 20:56
  • `docker-compose restart core` this will restart the application without the database so make sure that the database is running first then execute the command – Mostafa Hussein Mar 09 '19 at 20:57
  • Thanks. Still have the issue unfortunately. – shbfy Mar 09 '19 at 21:01
  • Are you sure that the postgres container was up and have not restarted while you restarted the application? can you check the up time through `docker ps` ? – Mostafa Hussein Mar 09 '19 at 21:02
  • See uptime: `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 189666f6a2b7 vision_core "python3 manage.py r…" 4 minutes ago Up 3 minutes 0.0.0.0:8000->8000/tcp vision_core_1 af5402d6495f postgres "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:32769->5432/tcp vision_db_1` – shbfy Mar 09 '19 at 21:04
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189737/discussion-between-mostafa-hussein-and-avocet). – Mostafa Hussein Mar 09 '19 at 21:05
  • Sure. Thank you for your help :) – shbfy Mar 09 '19 at 21:06

8 Answers8

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
  • 406
  • 1
  • 5
  • 9
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
  • 543
  • 4
  • 9
5

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

dfrankow
  • 20,191
  • 41
  • 152
  • 214
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
invad0r
  • 908
  • 1
  • 7
  • 20
oseleslie
  • 31
  • 3
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
  • 172
  • 1
  • 13
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
  • 2,585
  • 1
  • 8
  • 13
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.