0

I have a postgres server on my host machine and I want to make a docker container that connects to this postgres server.

So I guess I need to expose the postgres server on a connection IP:5432 to docker. Expose 5432 on the docker and specify the correct connection information inside the docker something like:

SQLALCHEMY_DATABASE_URI = "postgresql+psycopg2://username:password@IP/db_name"

The host docker IP's are:

docker0   Link encap:Ethernet  HWaddr 02:42:b3:d9:eb:e2  
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:b3ff:fed9:ebe2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:213512 errors:0 dropped:0 overruns:0 frame:0
          TX packets:351284 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:9157933 (9.1 MB)  TX bytes:826914241 (826.9 MB)

docker_gwbridge Link encap:Ethernet  HWaddr 02:42:5c:b9:3b:0a  
          inet addr:172.18.0.1  Bcast:172.18.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:5cff:feb9:3b0a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:436 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:64397 (64.3 KB)

What am I missing and how does I expose the postgres server to the relavant ports both on host side.

Peter Mølgaard Pallesen
  • 1,470
  • 1
  • 15
  • 26
  • try changing DATBASE_URL to local host like following , SQLALCHEMY_DATABASE_URI = "postgresql+psycopg2://username:password@localhost/db_name" Then start the container with --network="host". This will bind host network to guest – IndikaM Jun 13 '21 at 00:13

1 Answers1

2

Maybe you can consider use network mode: host for your container.

It means that container is not really isolated from your host network and can use localhost:5432 to connect to your database.

Jakub Bujny
  • 4,400
  • 13
  • 27