2017-03-03 78 views
0

我可以连接到我的本地Postgres数据库在我的web应用程序中,但是不是如果我正在Docker容器中运行Web应用程序。Docker容器与Python Web应用程序 - 连接到主机上的Postgres机器(OSX)

泊坞窗容器
  • 的Postgres在主机
  • 运行的内部运行

    • 的Web应用程序,我不知道这是否是与Postgres的连接设置或泊坞窗网络设置。

      按照我的设置和命令:

      主机

      • OSX 10.11.6
      • 的PostgreSQL 9.6

      泊坞容器

      • 泊坞1.13.1
      • 多克尔机0.9.0
      • 多克尔容器OS:蟒:3.6.0高山
      • 的Python 3.6 + psycopg2 == 2.7

      postgresql.conf中

      listen_addresses = '*' 
      

      的pg_hba.conf

      host all    all    127.0.0.1/32   trust 
      host all    all    0.0.0.0/0    trust 
      host all    all    172.17.0.0/24   trust 
      host all    all    192.168.99.0/24  trust 
      

      随着HOST模式泊坞网络

      docker run -i --net=host -h 127.0.0.1 -e POSTGRES_URI=postgresql://127.0.0.1:5432/db my/image 
      

      错误:

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

      随着BRIDGE模式泊坞网络

      docker run -i --add-host=dockerhost:`docker-machine ip ${DOCKER_MACHINE}` -e POSTGRES_URI=postgresql://dockerhost:5432/db -p 8000:8000 -p 5432:5432 my/image 
      

      错误:

      server closed the connection unexpectedly
      This probably means the server terminated abnormally before or while processing the request.

      任何想法?

    +0

    有什么特别的原因,在一个容器没有运行的Postgres以及做备注? – R0MANARMY

    回答

    1

    有一个关于在docs

    I want to connect from a container to a service on the host

    The Mac has a changing IP address (or none if you have no network access). Our current recommendation is to attach an unused IP to the lo0 interface on the Mac; for example: sudo ifconfig lo0 alias 10.200.10.1/24 , and make sure that your service is listening on this address or 0.0.0.0 (ie not 127.0.0.1). Then containers can connect to this address.

    +0

    我能有这个提示连接到外部服务,谢谢!我错过了。 –

    +1

    @PauloCheque,很高兴我能帮上忙。不要忘记将解决问题的答案标记为正确。这有助于其他遇到你问题的人。 – R0MANARMY

    相关问题