S
S
Sergey Alekseev2019-01-31 10:33:03
Django
Sergey Alekseev, 2019-01-31 10:33:03

Python-Django-Docker, how to connect to docker via localhost?

Good afternoon, in the docker there is a postgresql database with forwarded data

postgres:
   image: postgres
   ports:
     - "5001:5432"
   environment:
     POSTGRES_DB: provizorro
     POSTGRES_USER: provizorro
     POSTGRES_PASSWORD: Provizorro666
   volumes:
     - /home/sergey/Projects/data-pg:/var/lib/postgresql/data

How to connect to database from djanga
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'provizorro',
        'USER': 'provizorro',
        'PASSWORD': 'Provizorro666',
        'HOST': 'postgres',
        'PORT': 5432,
    }
}

How to set up a container so that you can connect to the local host?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2019-01-31
@serj2000

docker-compose.yml:

web: 
    ...
    links:
      - postgres
    env_file:  .env

.env
...
DB_HOST=postgres
DB_PORT=5432

then
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
         ...
        'HOST': os.environ.get('DB_HOST'),
        'PORT': os.environ.get('DB_PORT'),
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question