簡體   English   中英

無法從 docker 的容器內連接到 postgres 容器

[英]Unable to connect to postgres container from within container in docker

I made two different container with different images, both ended with the same ip as i create one afte deleting the previus one, both used a postgresql image and i was unable to connect to the container holding that specific ip 192.168.3.2, but from within我可以使用帶有 psql 的 ip 連接帶有 postgres 的容器,但嘗試從另一個容器導致

root@23c9e6ee234c:/# psql -h 192.168.3.2 -U odoo -W -d postgres -p 5432
Password for user odoo: 
psql: could not connect to server: Connection refused
    Is the server running on host "192.168.3.2" and accepting
    TCP/IP connections on port 5432?

但同樣的命令會在另一個 postgres 容器上成功。 無法訪問的容器的配置是:

listen_addresses = '*'

                                    # comma-separated list of addresses;
                                    # defaults to 'localhost'; use '*' for all
                                    # (change requires restart)
#port = 5432                            # (change requires restart)

我認為還可以,並且 ip 表(由 docker 生成且未經我修改)

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            

Chain FORWARD (policy DROP)
target     prot opt source               destination         
DOCKER-ISOLATION  all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain DOCKER (1 references)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             192.168.3.11         tcp dpt:8069
ACCEPT     tcp  --  anywhere             192.168.3.15         tcp dpt:8069
ACCEPT     tcp  --  anywhere             192.168.3.10         tcp dpt:8072
ACCEPT     tcp  --  anywhere             192.168.3.10         tcp dpt:8069
ACCEPT     tcp  --  anywhere             192.168.3.10         tcp dpt:ssh
ACCEPT     tcp  --  anywhere             192.168.3.14         tcp dpt:8072
ACCEPT     tcp  --  anywhere             192.168.3.14         tcp dpt:8069
ACCEPT     tcp  --  anywhere             192.168.3.14         tcp dpt:ssh
ACCEPT     tcp  --  anywhere             192.168.3.7          tcp dpt:8072
ACCEPT     tcp  --  anywhere             192.168.3.7          tcp dpt:8069
ACCEPT     tcp  --  anywhere             192.168.3.7          tcp dpt:ssh
ACCEPT     tcp  --  anywhere             192.168.3.9          tcp dpt:8072
ACCEPT     tcp  --  anywhere             192.168.3.9          tcp dpt:ssh
ACCEPT     tcp  --  anywhere             192.168.3.9          tcp dpt:8069
ACCEPT     tcp  --  anywhere             192.168.3.13         tcp dpt:hello-port
ACCEPT     tcp  --  anywhere             192.168.3.13         tcp dpt:mysql
ACCEPT     tcp  --  anywhere             192.168.3.13         tcp dpt:https
ACCEPT     tcp  --  anywhere             192.168.3.13         tcp dpt:http
ACCEPT     tcp  --  anywhere             192.168.3.13         tcp dpt:ssh
ACCEPT     tcp  --  anywhere             192.168.3.6          tcp dpt:postgres
ACCEPT     tcp  --  anywhere             192.168.3.3          tcp dpt:8071
ACCEPT     tcp  --  anywhere             192.168.3.3          tcp dpt:8069
ACCEPT     tcp  --  anywhere             192.168.3.3          tcp dpt:ipp
ACCEPT     tcp  --  anywhere             192.168.3.3          tcp dpt:ssh
ACCEPT     tcp  --  anywhere             192.168.3.4          tcp dpt:8071
ACCEPT     tcp  --  anywhere             192.168.3.4          tcp dpt:8069
ACCEPT     tcp  --  anywhere             192.168.3.4          tcp dpt:ipp
ACCEPT     tcp  --  anywhere             192.168.3.4          tcp dpt:ssh
ACCEPT     tcp  --  anywhere             192.168.3.2          tcp dpt:postgres
ACCEPT     tcp  --  anywhere             192.168.3.2          tcp dpt:ssh

Chain DOCKER-ISOLATION (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere            

例如,我無法連接到 192.168.3.6,但無法連接到 3.2,而我的 docker 版本是:

Client:
 Version:      17.04.0-ce
 API version:  1.28
 Go version:   go1.7.5
 Git commit:   4845c56
 Built:        Mon Apr  3 18:01:50 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.04.0-ce
 API version:  1.28 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   4845c56
 Built:        Mon Apr  3 18:01:50 2017
 OS/Arch:      linux/amd64
 Experimental: false

和網絡 ls:

NETWORK ID          NAME                DRIVER              SCOPE
496515e2ff28        bridge              bridge              local
50b61710b459        host                host                local
e061da58f729        none                null                local

我必須指出,直到最近,在我使用 systemctl stop docker 重新啟動 docker 服務並隨后啟動之后,該容器才可以訪問。

還有容器

3198ba01cb43        postgres:9.4            "docker-entrypoint..."   3 years ago         Up 8 minutes        0.0.0.0:8122->22/tcp, 0.0.0.0:8132->5432/tcp                                                                                 educosal_db
23c9e6ee234c        ubuntu                  "/bin/bash"              3 years ago         Up 3 hours          0.0.0.0:8069->8069/tcp, 0.0.0.0:2022->22/tcp, 0.0.0.0:2631->631/tcp, 0.0.0.0:2071->8071/tcp                                  odoo10

從 odoo10 我可以到達其他 postgres 容器,但不是 3.2 的容器,並且兩者的 ip 在同一范圍內。

可能是什么問題呢? iptables 不好(我的知識是空的)嗎? 這可能是 docker 錯誤嗎?

Postgres 9.4 使用不同的方式添加數據庫訪問變量,然后是 9.5。

也許您的 Docker 引擎版本意識到它是 9.4,而 Postgres 9.5 有另一種格式來添加數據庫訪問變量。

在閱讀您的問題后,此鏈接包含有關我的思路的更多信息。 Docker 集線器文檔

披露:我只上過 Docker 的課程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM