Docker容器到本地osx postgres

我试图从docker容器连接到本地postgres数据库(在osx中​​运行)。 我运行mac的docker。 我试过使用--add-host选项,但还没有得到它的工作。 我是否缺less一个configuration来build立连接? 下面是我正在尝试的一个例子。

 docker run --add-host=localbox:192.168.59.3 -it postgres /bin/bash 

连接尝试

 root@1893226613e9:/# psql -h localbox -U test_user psql: could not connect to server: Connection timed out Is the server running on host "localbox" (192.168.59.3) and accepting TCP/IP connections on port 5432? root@1893226613e9:/# ping localbox PING localbox (192.168.59.3): 56 data bytes ^C--- localbox ping statistics --- 7 packets transmitted, 0 packets received, 100% packet loss 

好吧,来自@warmoverflow的评论引导我走向正确的方向。 这是我必须做的事情,让所有的谈话。

最初我使用的是错误的ip –add-host。 我不得不使用以下。

 docker run --add-host=localbox:192.168.99.1 -it postgres /bin/bash 

我通过查找virtualbox正在使用的地址得到了192.168.99.1。

 $ ifconfig vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500 ether 0a:00:27:00:00:00 inet 192.168.99.1 netmask 0xffffff00 broadcast 192.168.99.255 

这让Ping能够工作,但是接到拒绝的快速连接。

 psql: could not connect to server: Connection refused Is the server running on host "192.168.99.1" and accepting TCP/IP connections on port 5432? 

然后,我不得不启用postgres接受远程连接。 我通过添加到下面的configuration来做到这一点。

/usr/local/var/postgres/postgresql.conf

 listen_addresses = '*' 

/usr/local/var/postgres/pg_hba.conf

 host all all 192.168.99.100/32 trust