如何在 docker 中连接 postgres 数据库

How to connect the postgres database in docker

我创建了一个 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 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.

将堆栈中的容器视为不同的物理机或虚拟机。您的数据库在一台主机上,而聊天机器人在另一台主机上。聊天机器人自然无法在本地找到 /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 键。