2017-03-03 76 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

    相關問題