【问题标题】:I have Postgres server connection error when I use docker?使用 docker 时出现 Postgres 服务器连接错误?
【发布时间】:2020-06-05 00:32:55
【问题描述】:

我的应用程序在我的系统上运行良好,但是当我通过 docker composer 构建一个 docker 映像然后运行该应用程序时,它会显示以下错误。

但是,我通过在下面的 Flask 设置中将 ip 地址从 127.0.0.1 => 0.0.0.0 更改为解决此问题,并且 docker 映像在我的系统上运行良好。但是当我在另一个 ubuntu 上运行它时,它给了我同样的错误并将 IP 更改为 127.0.0.1。

if __name__ == "__main__":
    app.run(host='**0.0.0.0**',debug=False)

错误:

Traceback (most recent call last):
  File "./main.py", line 22, in <module>
    ps.create_table()  # create the table if is not existed
  File "/prj/Modules/postgres.py", line 49, in create_table
    connection = get_connection()
  File "/prj/Modules/postgres.py", line 36, in get_connection
    return psycopg2.connect(f"dbname={db_name} user={user} password={password} host={host}")
  File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: 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?

您可以访问我的代码和我的 docker hub:

码头中心:applia65/duplicatebugreportsearchengine Github:https://github.com/ghasemieh/Duplicated-Bug-Report-Detection-System/blob/master/main.py

基本上我有 3 个容器 my-app、Postgres 和 MongoDB。

如果您能帮我解决这个问题,我将不胜感激。 谢谢

【问题讨论】:

  • 你能提供 DOCKERFILE 或 yaml 给 docker-compose 吗?同样从堆栈跟踪来看,在我看来,您在某处设置了 PostgreSQL ip 地址,但它无法连接到数据库

标签: python postgresql docker docker-compose psycopg2


【解决方案1】:

从跟踪中,无法访问 PostgreSql。您可以尝试更新您的 docker compose 并添加如下所示的公开声明:

expose:
- 5432

这将只允许访问链接的服务,除非还指定了“端口”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-08
    • 2021-08-30
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多