2

For example I Use defoult settings in Django

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

When dockerize my db with

depends_on:
          - db

Should I change change my 'HOST' parameter '127.0.0.1' for 'db'

could you explain this process

thank you in advance

0

1 Answer 1

1

Yes you need to change host to db. You can also use environment variables in your setting like below.

DATABASES = {
"default": {
    "ENGINE": os.environ.get("SQL_ENGINE"),
    "NAME": os.environ.get("SQL_DATABASE"),
    "USER": os.environ.get("SQL_USER"),
    "PASSWORD": os.environ.get("SQL_PASSWORD"),
    "HOST": os.environ.get("SQL_HOST"),
    "PORT": os.environ.get("SQL_PORT"),
}

}

and add environment variables to docker-compose or add env_file. Below is docker-compose.

services:
  web:
    restart: always
    build:
      context: ./mmd_backend
      dockerfile: Dockerfile.prod
    command: gunicorn mmd_backend.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
    expose:
      - 8000
    env_file:
      - ./.env.prod
    depends_on:
      - db
  db:
    build: ./pg12-3.0
    ports:
      - 5436:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db
  nginx:
    build: ./nginx
    restart: always
    volumes:
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
    ports:
      - 8000:8000
    depends_on:
      - web

volumes:
  postgres_data:
  static_volume:
  media_volume:

and env file is as bellow

#.env

DEBUG=1
SECRET_KEY=7bhlblY8pY
SQL_ENGINE=django.contrib.gis.db.backends.postgis
SQL_DATABASE=db_name
SQL_USER=db_user
SQL_PASSWORD=db_password
SQL_HOST=db
SQL_PORT=5432
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.