Hello I am new to docker, I have an existing python Django todo app which is connected to localhost PostgreSQL database, Now I want to run Django todo app inside container while connecting with the existing database on localhost.
here are some details:
OS = macOS Catalina
docker = 20+
python = 3.9.2
Django = 3.0
postgres /psql = 13.2
container_name = django
image_name = django-apps
have updated following files
postgresql.conf: updated #listen_addresses = “localhost” to listen_addresses = “*”
pg_hba.conf: added host MyDB all 172.17.0.0/16 md5
Inside Django project mysite->settings.py have added the database connection details
I am able to make build of the image, but don’t know how to connect with the database, I have tried following commands but aren’t working for me :
docker run -p 8000:80 --add-host=host.docker.internal --name <container name> <image_name>
docker run -p 8000:80 --add-host=host.docker.internal --name django django-apps
It gives me
bad argument
error
docker run -p 8000:80 --name <container name> <image_name>
then I do get error →
psycopg2.operationError: could not connect to server: Connection refused
Is the server running on the host “127.0.0.1” and accepting TCP/IP connections on port 5432?
docker run -p 8000:80 -e "DB_HOST=127.0.0.1" -e "DB_NAME=MyDB" -e "DB_USER=djangopostgres"-e "DB_PASS=<password>" -e "DB_PORT=5432" --name <container name> <image_name>
Even above command didn’t helped don’t know where I am missing something.
Please Note: I don’t want to create new container for postgres as of now
please let me know if anyother details are needed