postgresqldockerdocker-composerasarasa-core

How to connect the postgres database in docker


I have created a Rasa Chatbot that asks user information and store it in the postgres database. Locally it works. I have been trying to do that in the docker but it is not working. I'm new to docker. could anyone help me. Thanks in advance

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:

Logs: All services are running in logs and I checked in docker also postgres is running.

    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

Error:

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.

Solution

  • Think of containers in the stack as of different physical or virtual machines. Your database is on one host and the chatbot is on another. Naturally the chatbot cannot find /var/run/postgresql/.s.PGSQL.5432 locally because it's in another container (as if on another computer), so you need to use network connection to reach it:

    # 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')
    

    Also, your action-server1 service is configured to be in shan_network:

        action-server1:
          networks:
            - shan_network
    

    Therefore, the action-server1 currently has no network access to the other services in this stack. db and rasa have no networks configured and because of that they use the default network, which is automatically created for you by docker-compose. This is as if you would configure those services as following:

        db:
          image: "postgres"
          networks:
            - default
    

    If you wish action-server1 to appear in several networks and thus be able to reach services both in this stack and whatever is in shan_network, you need to add the service to the default network:

        action-server1:
          networks:
            - shan_network
            - default
    

    Alternatively, if you are unsure why there is a shan_network at all, you can simply remove the network key from the action-server1 service.