Menno ō
Menno ō

Reputation: 11

django OperationalError when trying to migrate with docker

django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known That is the error I get when running docker-compose exec web python manage.py migrate my docker-compose.yml contains:

version: '3.8'

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db
  db:
    image: postgres:11

This Is what I put for DATABASE in settings.py:

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

I have tried running docker-compose up -d --build and then docker-compose exec web python manage.py migrate But that doesn't work.

Upvotes: 0

Views: 246

Answers (1)

user2390182
user2390182

Reputation: 73460

You will need to add an environment for your db service:

services:
  # ...
  db:
    # ...
    environment:
      POSTGRES_PASSWORD: postgres

as POSTGRES_PASSWORD is the only non-optional env variable needed to run the image as stated in the docs.

Upvotes: 1

Related Questions