简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM