简体   繁体   English

通过 Flask Z05B6053C41A2130AFD6FC3B158B 容器连接时,连接被 postgresql docker 容器拒绝

[英]Connection refused by postgresql docker container when connecting via Flask docker container

I have flask application that connects to PostgreSQL and works well in a single installation.我有 flask 应用程序连接到 PostgreSQL 并且在单个安装中运行良好。 However when I containerise it using docker into separate containers, flask app is unable to connect to PostgreSQL and the error received is psycopg2.OperationalError: could not connect to server: Connection refused但是,当我使用 docker 将其容器化到单独的容器中时,flask 应用程序无法连接到 PostgreSQL 并且收到的错误是 psycopg2.OperationalError: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server: could not connect to server.

provide below is the code for app.py下面提供的是 app.py 的代码

app = Flask(__name__)

app.config['basedir'] = os.path.abspath(os.path.dirname(__file__))
app.config['DEVELOPMENT'] = True
app.config['DEBUG'] = True

POSTGRES = {
    'user': 'postgres',
    'pw': 'postgres',
    'db': 'postgres',
    'host': 'localhost',
    'port': '5432',
}
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

api = Api(app)

from database.postgresql import db
db.init_app(app)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

the app is deployed using app server uwsgi and provided below is the app.ini file contents该应用程序是使用应用程序服务器 uwsgi 部署的,下面提供的是 app.ini 文件内容

[uwsgi]
module=wsgi:app
wsgi-file = app.py
callable = app
socket = :8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true

the error stack is as follows错误堆栈如下

  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2336, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 364, in connect
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 495, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 140, in _do_get
    self._dec_overflow()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.raise_(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 137, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 309, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 440, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.raise_(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 656, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 493, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, 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 "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?

There is a problem with your postgres configuration in app.py.你的 app.py 中的 postgres 配置有问题。

You're not trying to connect to your postgres container, rather your Flask container's localhost.您不是在尝试连接到您的 postgres 容器,而是您的 Flask 容器的本地主机。 You'll need to specify the address of the postgres container instead.您需要改为指定 postgres 容器的地址。 Then it will work perfectly.然后它会完美地工作。

You can read more about how network communication in Docker works here .您可以在此处阅读有关 Docker 中网络通信如何工作的更多信息。

This is too late but i have the same error.这为时已晚,但我有同样的错误。 It took me hours to find out the actual problem.我花了几个小时才发现真正的问题。 The problem is you need to change host name.问题是您需要更改主机名。 The host must be the hostname instead of localhost.主机必须是主机名而不是 localhost。 You can see the hostname in psql cmd when you type键入时可以在 psql cmd 中看到主机名

   env

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM