linux - 无法从 docker 的容器内连接到 postgres 容器
问题描述
我用不同的图像制作了两个不同的容器,它们都以相同的 ip 结束,因为我在删除以前的一个后创建了一个,都使用了 postgresql 图像,我无法连接到持有该特定 ip 192.168.3.2 的容器,但从内部我可以使用带有 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 的课程。
推荐阅读
- python - 试图从网站上抓取视频帧。获取 403:禁止和“错误的 cookie”
- selenium - Selenium Web 驱动程序无法检查/找到“选择”html 标记(组合框)
- python - App Engine 本地环境显示不正确的数据
- mysql - 如何批量更新未更新产品的库存
- sql - 如何减去同一表同一列中配置单元中的计数值
- javascript - 如何有效且高效地将项目分组到成对的存储桶中(如果存在)
- mongodb - 日期范围在聚合管道中不起作用,但在 find() 中起作用
- python - SqlAlchemy“contains_eager”似乎没有加载嵌套关系
- google-cloud-platform - Sqoop 作业通过 Dataproc 失败
- node.js - 无法在 express api 和反应应用程序中使用 http-proxy-middleware 进行代理