【问题标题】:How to connect the postgres database in docker如何在docker中连接postgres数据库
【发布时间】:2021-08-05 08:55:59
【问题描述】:

我创建了一个 Rasa Chatbot,它询问用户信息并将其存储在 postgres 数据库中。在本地它有效。我一直在尝试在 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,postgres 也在运行。

    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.

【问题讨论】:

    标签: postgresql docker docker-compose rasa rasa-core


    【解决方案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:无法将主机名“db”转换为地址:名称解析暂时失败
    • @Shan 'action server' 分配了一个自定义网络。您需要使操作服务器与数据库存在于同一网络中。将default 添加到网络的操作服务器列表中,然后重试。
    • 您能否详细解释一下,或者如果可能的话,您能否分享代码
    • 现在它可以工作了,非常感谢lotttt...你能告诉我如何查看存储的数据吗?
    • @Shan 我可以,但这不是一个简短的答案。您可以将pgadmin 添加到堆栈中,或者通过数据库容器中的shell,或者使用psycopg2 进行查询,或者我猜是使用您的应用程序。
    猜你喜欢
    • 2022-11-12
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多