python - Connecting from psycopg2 on local machine to PostgreSQL db on Docker -
i have used following commands create docker image postgres running on it:
docker pull postgres docker run --name test-db -e postgres_password=my_secret_password -d postgres
i created table called test , inserted random data couple of rows. trying make connection database table through psycopg2 in python on local machine. used command docker-machine ip default
find out ip address of machine 192.168.99.100 , using following try , connect:
conn = psycopg2.connect("dbname='test-db' user='postgres' host='192.168.99.100' password='my_secret_password' port='5432'")
this not working error message of "operationalerror: not connect server: connection refused (0x0000274d/10061)"
. .
everything seems in order can't think why refused. according documentation postgres image, (at https://hub.docker.com/_/postgres/) image includes expose 5432 (the postgres port) , default username postgres.
i tried ip address of image docker inspect test-db | grep ipaddress | awk 'print{$2}' | tr -d '",'
found on sa related article, ip address didn't work either.
the expose
instruction may not doing expect. used links , inter-container communication inside docker network. when connecting container outside docker bridge network need publish port -p
. try adding -p 5432:5432
docker run
command looks like:
docker run --name test-db -e postgres_password=my_secret_password -d -p 5432:5432 postgres
here decent explanation of differences between publish , exposed ports: https://stackoverflow.com/a/22150099/684908. hope helps!
Comments
Post a Comment