如何在Docker中连接PostgreSQL数据库

3
我已经创建了一个 Rasa Chatbot,可以询问用户信息并将其存储在 postgres 数据库中。 在本地运行正常。 我一直在尝试在 Docker 中实现它,但是无法工作。我是 Docker 的新手,请有谁能帮帮我。 提前感谢。
docker-compose.yml
version: "3.0"
services:
    rasa:
      container_name: rasa
      image: rasa/rasa:2.8.1-full
      ports:
        - 5005:5005
      volumes:
        - ./:/app
      command:
        -  run
        -  -m
        -  models
        -  --enable-api
        -  --cors
        -  "*"
      depends_on:
        - action-server1
        - db
    action-server1:
      container_name: "action-server1"
      build:
        context: actions
      volumes:
        - ./actions:/app/actions
      ports:
        - "5055:5055"
      networks:
        - shan_network
    db:
      image: "postgres"
      environment:
        POSTGRESQL_USERNAME: "postgres"
        POSTGRESQL_PASSWORD: ""
        POSTGRESQL_DATABASE: "postgres"
        POSTGRES_HOST_AUTH_METHOD: "trust"
      volumes:
        - db-data:/var/lib/postgresql/data
      ports:
        - "5432:5432"
volumes:
  db-data:

日志:所有服务都在日志中运行,我还检查了docker,postgresql也在运行。

    db_1              |
    db_1              | PostgreSQL Database directory appears to contain a database; Skipping initialization
    db_1              |
    db_1              | 2021-08-05 08:21:45.685 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debia
    n 8.3.0-6) 8.3.0, 64-bit
    db_1              | 2021-08-05 08:21:45.686 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
    db_1              | 2021-08-05 08:21:45.686 UTC [1] LOG:  listening on IPv6 address "::", port 5432
    db_1              | 2021-08-05 08:21:45.699 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    db_1              | 2021-08-05 08:21:45.712 UTC [26] LOG:  database system was shut down at 2021-08-05 08:21:25 UTC
    db_1              | 2021-08-05 08:21:45.722 UTC [1] LOG:  database system is ready to accept connections

错误:
action-server1    |   warnings.warn(
action-server1    | Exception occurred while handling uri: 'http://action-server1:5055/webhook'
action-server1    | Traceback (most recent call last):
action-server1    |   File "/opt/venv/lib/python3.8/site-packages/sanic/app.py", line 973, in handle_request
action-server1    |     response = await response
action-server1    |   File "/opt/venv/lib/python3.8/site-packages/rasa_sdk/endpoint.py", line 104, in webhook
action-server1    |     result = await executor.run(action_call)
action-server1    |   File "/opt/venv/lib/python3.8/site-packages/rasa_sdk/executor.py", line 398, in run
action-server1    |     action(dispatcher, tracker, domain)
**action-server1    |   File "/app/actions/actions.py", line 148, in run
action-server1    |     connection = psycopg2.connect(database="postgres", user='postgres', password='password',port='5432'
action-server1    |   File "/opt/venv/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
action-server1    |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
action-server1    | psycopg2.OperationalError: could not connect to server: No such file or directory
action-server1    |     Is the server running locally and accepting
action-server1    |     connections on Unix domain  socket "/var/run/postgresql/.s.PGSQL.5432"?**

rasa              | 2021-08-05 08:25:13 ERROR    rasa.core.processor  - Encountered an exception while running action 'action_save'.Bot will continue, but
the actions events are lost. Please check the logs of your action server for more information.
rasa              | Traceback (most recent call last):
rasa              |   File "/opt/venv/lib/python3.8/site-packages/rasa/core/actions/action.py", line 685, in run
rasa              |     response = await self.action_endpoint.request(
rasa              |   File "/opt/venv/lib/python3.8/site-packages/rasa/utils/endpoints.py", line 172, in request
rasa              |     raise ClientResponseError(
rasa              | rasa.utils.endpoints.ClientResponseError: 500, Internal Server Error, body='b'<!DOCTYPE html><meta charset=UTF-8><title>500 \xe2\x80\x9
4 Internal Server Error</title><style>html { font-family: sans-serif }</style>\n<h1>\xe2\x9a\xa0\xef\xb8\x8f 500 \xe2\x80\x94 Internal Server Error</h1><p>
The server encountered an internal error and cannot complete your request.\n''
rasa              |
rasa              | The above exception was the direct cause of the following exception:
rasa              |
rasa              | Traceback (most recent call last):
rasa              |   File "/opt/venv/lib/python3.8/site-packages/rasa/core/processor.py", line 772, in _run_action
rasa              |     events = await action.run(
rasa              |   File "/opt/venv/lib/python3.8/site-packages/rasa/core/actions/action.py", line 709, in run
rasa              |     raise RasaException("Failed to execute custom action.") from e
rasa              | rasa.shared.exceptions.RasaException: Failed to execute custom action.
1个回答

1

将堆栈中的容器视为不同的物理或虚拟机。您的数据库在一个主机上,聊天机器人在另一个主机上。自然地,聊天机器人无法在本地找到/var/run/postgresql/.s.PGSQL.5432,因为它位于另一个容器中(就像在另一台计算机上),所以您需要使用网络连接才能访问它:

# If host is not given it uses unix socket which you appear to have locally,
# thus add it here:
connection = psycopg2.connect(database="postgres", 
                              user='postgres', 
                              password='password',
                              host='db', #  name of the service in the stack
                              port='5432')

此外,您的action-server1服务被配置为在shan_network中。
    action-server1:
      networks:
        - shan_network

因此,目前的action-server1无法访问此堆栈中的其他服务。 dbrasa没有配置网络,因此它们使用default网络,该网络由docker-compose自动创建。这就好像您将这些服务配置为以下内容:
    db:
      image: "postgres"
      networks:
        - default

如果您希望action-server1出现在多个网络中,并能够访问此堆栈中的服务以及shan_network中的任何内容,则需要将该服务添加到default网络中。
    action-server1:
      networks:
        - shan_network
        - default

或者,如果您不确定为什么要使用shan_network,您可以从action-server1服务中简单地移除network关键字。


我遇到了这个错误 action-server1 | psycopg2.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution。 - Shan
@Shan,“action server”有一个自定义网络分配给它。您需要使动作服务器与数据库在同一网络中。将“default”添加到动作服务器的网络列表中,然后再试一次。 - anemyte
你能否详细解释一下,或者如果可能的话,能否分享代码? - Shan
现在它可以工作了,非常感谢...你能告诉我如何查看存储的数据吗? - Shan
@Shan 我可以,但这不是一个简短的答案。你可以将 pgadmin 添加到堆栈中,或通过数据库容器中的 shell,或使用 psycopg2 查询,或者使用你的应用程序。 - anemyte

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接