To use Docker-compose with Django to connect to a local instance of Postgresql, you need to follow these steps:
Create a Dockerfile for Django and include psycopg2
in the requirements.txt file.
Create a Docker-compose.yml file which will include the following services:
In the Django settings.py file, set the database engine to django.db.backends.postgresql_psycopg2
. You will also need to specify the database name, username, and password.
In the Docker-compose.yml file, specify the database environment variables (POSTGRESDB, POSTGRESUSER, and POSTGRES_PASSWORD) for the Postgresql service.
Map the Postgresql port to the local machine port in the Docker-compose.yml file.
Run docker-compose up
to start the services.
In the Django application, update the database settings to connect to Postgresql on port 5432
.
Run the Django application to connect to the local instance of Postgresql.
Asked: 2023-02-13 11:00:00 +0000
Seen: 11 times
Last updated: Nov 17 '21