J
J
Jekson2020-01-28 11:05:14
Django
Jekson, 2020-01-28 11:05:14

Postgresql and using virtual environment and docker on the same project?

Locally setting up a project to work with docker and pipenv. Changed the database settings in settings.py

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


and adding the db

docker-compose.yml section to the yml file

version: '3.7'
services:
  web:
    build: .
    command: python /profi/manage.py runserver 0.0.0.0:8000
    environment:
      - SECRET_KEY=dy)zvq+sf07^^456t$$6+mv*tj6#5iwyo896-z!v=h^njl9^&@q
      - DEBUG=1
    volumes:
      - .:/profi
    ports:
      - 8000:8000
    depends_on:
      - db
  db:
    image: postgres:11
    volumes:
      - postgres_data:/var/lib/posgresql/data/

volumes:
  postgres_data:


If I start the server through docker-compose upthen everything starts, only every time I need to create an admin. The database does not store data. Did you not finish something in volumes?
But the main question is when I start the server without using docker, but through pipenv shell python manage.py runserverthat I get an error

django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known


Is there something wrong in docker-compose.yml? Please tell me what to fix.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jekson, 2020-01-28
@Lepilov

As a result, the working configuration is this
docker-compose.yml

version: '3.7'
services:
  web:
    build: .
    command: python /profi/manage.py runserver 0.0.0.0:8000
    environment:
#      DJANGO_SETTINGS_MODULE: "project.settings.development"
      DEBUG: 1
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
      POSTGRES_HOST: db

    volumes:
      - .:/profi
    ports:
      - "${HTTP_PORT:-8000}:8000"
    depends_on:
      - db
  db:
    image: postgres:11
    volumes:
      - postgres_data:/var/lib/posgresql/data/
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
      POSTGRES_HOST: db
    ports:
      - "${DB_PORT:-5432}:5432"

volumes:
  postgres_data:

settings.py
DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_DB', default='postgres'),
        'USER': os.environ.get('POSTGRES_USER', default='postgres'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD', default='postgres'),
        'HOST': os.environ.get('POSTGRES_HOST', default='localhost'),
        'PORT': "5432"
        }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question