【问题标题】:How to connect to the database in Docker in my case?在我的情况下,如何连接到 Docker 中的数据库?
【发布时间】:2017-08-15 11:46:42
【问题描述】:

我正在尝试通过 docker 容器连接到我的 postgres。我读过了 很多帖子,但似乎仍然无法解决。

我运行了以下命令。

docker run --name some-postgres -e POSTGRES_PASSWORD=mypw -d -p 5432:5432 postgres

在那之后,我做docker inspect $(containerID)

"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "773de825919cb75482af47b85b1804f96a32bc6e9951cc0ea80752f880789717",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5432/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "5432"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/773de825919c",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "29c872cb1487519a8f605f7ba371e5ce84c2b163140babbb981ace8a12a273cd",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9d69c3e1dfa70572306fbe6dd14ed91d7ee96796deb27057b1c2992f573875f9",
                    "EndpointID": "29c872cb1487519a8f605f7ba371e5ce84c2b163140babbb981ace8a12a273cd",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02"
                }
            }
        }

我的理解是我可以使用"IPAddress": "172.17.0.2" 连接到数据库

我是这样的

psql -h 172.17.0.2 -p 5432 -U postgres

但是,我收到操作超时错误,无法登录到 postgres。

错误

psql: could not connect to server: Operation timed out
    Is the server running on host "172.17.0.2" and accepting
    TCP/IP connections on port 5432?

谁能帮我解决这个问题?非常感谢!

【问题讨论】:

    标签: node.js postgresql docker docker-compose


    【解决方案1】:

    你没有提到你是如何运行 docker 的。如果你在你的工作站上使用它,比如 OSx 的 Docker,你必须使用“localhost”进行连接:

    psql -h localhost -p 5432 -U postgres
    

    如果您在 Docker 虚拟机或远程主机上运行容器,则需要本地网络中的外部接口 IP。

    psql -h 192.168.99.100 -p 5432 -U postgres
    

    您还必须验证,如果 PostgreSQL 配置为允许远程连接,请检查“pg_hba.conf”并添加

    host    all             all             0.0.0.0/0               md5
    

    并在 /etc/postgresql/9.1/main/postgresql.conf 中将 listen_addresses 更改为 listen_addresses='*'

    请注意:此设置仅用于测试,并会在您的数据库配置中打开一个漏洞。

    【讨论】:

      猜你喜欢
      • 2018-11-06
      • 2015-09-06
      • 2012-07-11
      • 1970-01-01
      • 2011-11-15
      • 2011-05-27
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多