2

I have a Django project running in multiple Docker containers with help of docker-compose. The source code is attached from directory on my local machine. Here's the compose configuration file:

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

Although the application starts as it should, I can't run migrations, because every time I do manage.py makemigrations ... I receive:

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

Obviously I can open bash inside the core container and run makemigrations from there, but then the migration files are created inside the container which is very uncomfortable.

In my project's settings the database is configured as:

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

As docker postgres image is accessible at localhost:5432 I tried changing database host in settings to:

'HOST': '0.0.0.0'

But then when firing up the containers with docker-compose up I'm receiving:

...
django.db.utils.OperationalError: could not connect to server: 
Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
...

How should I configure database in settings.py so that Django can access it to create migrations?

Stan Reduta
  • 3,292
  • 5
  • 31
  • 55

1 Answers1

2

Your docker-compose configurations are not correct. You forgot to link services

version: '3'
services:
  db:
    image: 'postgres'
    ports:
      - '5432:5432'
  core:
    build:
      context: .
      dockerfile: Dockerfile
    command: python3 manage.py runserver 0.0.0.0:8000
    ports:
      - '8001:8000'
    volumes:
      - .:/code
    depends_on:
      - db
    links: # <- here
      - db
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
  • 1
    I'm getting the same error as earlier: `django.db.utils.OperationalError: could not translate host name "db" to address: nodename nor servname provided, or not known` – Stan Reduta May 28 '18 at 04:12
  • @StanRedoute Have you restarted your containers after updating configuration? `docker-compose down && docker compose up`. This should work, if it is still not working, we need more info – Sardorbek Imomaliev May 28 '18 at 04:16
  • I have rebuilt compose with `--no-cache` to make a clean build and during firing up the `compose up` I received: `django.db.utils.OperationalError: could not connect to server: Connection refused Is the server running on host "db" (172.18.0.2) and accepting TCP/IP connections on port 5432?` Looks like `links` did the trick but Django itself can't access the link to db? – Stan Reduta May 28 '18 at 04:26
  • 1
    @StanRedoute this means your backend have started before `db` container was ready read more about this issue in https://docs.docker.com/compose/startup-order/ – Sardorbek Imomaliev May 28 '18 at 04:30