2017-09-05 141 views
1

我一直在嘗試碼頭容器postgres。到目前爲止,我還沒有能夠連接到容器內的數據庫。碼頭集裝箱Postgres連接錯誤

我的步驟重新創建下面的問題。

dockerfile開始

FROM postgres 
ENV POSTGRES_DB db 
ENV POSTGRES_USER postgres 
ENV POSTGRES_PASSWORD postgres 
COPY db_schema.sql /docker-entrypoint-initdb.d/ 

我建的,像這樣

$ docker build -t db_con . 

泊塢窗文件並開創了容器查看運行的容器如下

$ docker run -it -p 5432:5432 --name test_db db_con /bin/bash 

$ docker ps -a 

CONTAINER ID IMAGE COMMAND     CREATED  STATUS  PORTS     NAMES 
82347f1114c4 db "docker-entrypoint..."  3 hours ago Up 2 sec 0.0.0.0:5432->5432/tcp test_db 

我檢查的容器的地址信息..

$ docker inspect test_db 

--extract start-- 
"Networks": { 
       "bridge": { 
        "Gateway": "172.17.0.1", 
        "IPAddress": "172.17.0.2", 

       } 
      } 
--extract end-- 

現在,我已經在容器內嘗試過,但我一直沒成功。 我已經嘗試了下面的錯誤與下面的錯誤。

[email protected]:/# psql -U postgres -h 0.0.0.0 -p 5432 -d db 
[email protected]:/# psql -U postgres -h 172.17.0.1 -p 5432 -d db 
[email protected]:/# psql -U postgres -h 172.17.0.2 -p 5432 -d db 
**response for all the above** 
psql: could not connect to server: Connection refused 
    Is the server running on host "0.0.0.0" and accepting 
    TCP/IP connections on port 5432? 

如果有人能指出我正確的方向,我會很高興。我在這裏碰壁了,任何幫助都非常感謝。

+0

嘗試所有這些東西,例如創建模式,從外部連接到數據庫實例(即在容器生命週期之外)。這是更有用的方法。 –

回答

1

它看起來像你覆蓋默認postgres cmd到/bin/bash。 爲什麼你把/bin/bash放在命令結尾?

docker run -it -p 5432:5432 --name test_db db_con /bin/bash 

嘗試執行

docker run -it -p 5432:5432 --name test_db db_con 

另外,Postgres將只有當數據庫轉儲恢復可用。

+0

正確回答問題,使用您的建議完美運作。 – BlowMan

0

您需要在您的pg_hba.conf添加新的規則:

nano /etc/postgresql/9.3/main/pg_hba.conf 

地址:

host all    all    [Docker Server IP]/16   md5 

接下來,你需要取消對後續行中postgres.conf:

listen_addresses = '*'     # what IP address(es) to listen on; 

現在重新啓動您的postgres服務,然後重試。