0

I'm currently trying to setup a docker environment for GNUHealth, involving containers running Tryton and PostgreSQL. However, I'm facing a problem where when I start up the containers and enter Tryton's web interface on localhost:8000, I'm unable to login as there are no databases to select from. The Tryton container throws an error saying: "could not translate host name "postgres" to address: No address associated with hostname"

The PostgreSQL container logs:

PostgreSQL Database directory appears to contain a database; Skipping initialization


2023-08-29 08:16:50.504 UTC [1] LOG:  starting PostgreSQL 15.4 (Debian 15.4-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit

2023-08-29 08:16:50.504 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432

2023-08-29 08:16:50.504 UTC [1] LOG:  listening on IPv6 address "::", port 5432

2023-08-29 08:16:50.531 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

2023-08-29 08:16:50.574 UTC [30] LOG:  database system was shut down at 2023-08-29 08:16:21 UTC

2023-08-29 08:16:50.589 UTC [1] LOG:  database system is ready to accept connections

And the Tryton container logs indicating the error:

1 139767745062656 [2023-08-29 08:16:55,773] ERROR trytond.backend.postgresql.database connection to "template1" failed

Traceback (most recent call last):

  File "/usr/local/lib/python3.9/dist-packages/trytond/backend/postgresql/database.py", line 264, in get_connection

    conn = self._connpool.getconn()

  File "/usr/lib/python3/dist-packages/psycopg2/pool.py", line 169, in getconn

    return self._getconn(key)

  File "/usr/lib/python3/dist-packages/psycopg2/pool.py", line 93, in _getconn

    return self._connect(key)

  File "/usr/lib/python3/dist-packages/psycopg2/pool.py", line 63, in _connect

    conn = psycopg2.connect(*self._args, **self._kwargs)

  File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 127, in connect

    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

psycopg2.OperationalError: could not translate host name "postgres" to address: No address associated with hostname


[pid: 1|app: 0|req: 7/7] 172.27.0.1 () {52 vars in 937 bytes} [Tue Aug 29 08:16:55 2023] POST / => generated 2542 bytes in 9 msecs (HTTP/1.1 200) 5 headers in 170 bytes (1 switches on core 1)

Tryton interface

I'm using docker-compose with the following files:

Dockerfile:

FROM python:3.9

# Install required dependencies and PostgreSQL client tools
RUN apt-get update && \
    apt-get install -y postgresql-client && \
    pip install trytond psycopg2

# Copy the Tryton server configuration file
COPY trytond.conf /etc/trytond.conf

# Expose the Tryton server port
EXPOSE 8000

# Set the entrypoint command
ENTRYPOINT ["trytond", "-c", "/etc/trytond.conf"]

docker-compose.yml:

version: '3'

services:
  postgresql:
    image: postgres:latest
    environment:
     - DB_USER=postgres
     - DB_PASS=postgres
     - DB_NAME=template1
     - "POSTGRES_HOST_AUTH_METHOD=trust"
    ports:
      - "5433:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - gnunet

  tryton:
    restart: always
    image: tryton/tryton:latest
    environment:
      - TRYTON_DATABASE_URI=postgresql://postgres:postgres@postgresql:5432/template1
    ports:
      - "8000:8000"
    depends_on:
      - postgresql
    volumes:
      - tryton_data:/var/lib/tryton
    links:
      - postgresql
    networks:
      - gnunet

networks:
    gnunet:
        driver: bridge

volumes:
  postgres_data:
  tryton_data:

trytond.conf:

[database]
uri = postgresql://tryton:tryton@postgres:5433/trytondb

[web]
listen = *:8000

[jsonrpc]
listen = *:8000

[webdav]
listen = *:8000

[session]
timeout = 3600

[database_name]
name = trytondb

[password]
admin = tryton

As you can see, I added a bunch of stuff suggested in other related topics, such as the "POSTGRES_HOST_AUTH_METHOD=trust" line (which I know is not safe in production) or the restart: always behaviour, but none of these seemed to work yet.

Any help is greatly appreciated :)

Matthias
  • 13
  • 2

1 Answers1

0

Your database URI environment variable is missing a D at the end of TRYTOND so it is being ignored and the Tryton server falls back to the URI configured in trytond.conf, which is configured to a non-existent "postgres" server while the docker-compose service is actually named "postgresql".

Ideally you'll want to configure the URI (or any other tryton setting) only once. Use either an environment variable or a configuration file but not both so you don't have to reason about what is taking precedence nor reach this kind of confusing situations.

Also, using POSTGRES_HOST_AUTH_METHOD=trust means you can skip username and password in the URI. Also the default port is implicit and the database name is ignored by Tryton (you should see a warning about that). So you can reduce the configuration to:

# docker-compose.yml

services:

  postgresql:
    # ...
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust

  tryton:
    # ...
    environment:
      - TRYTOND_DATABASE_URI=postgresql://postgresql
N1ngu
  • 2,862
  • 17
  • 35
  • How could I be so blind? Thank you very much for pointing out the missing D, I was about to lose my last bit of sanity on this. The error is gone now, but I still don't see any databases... Guess I'll have to read more about working with PostgreSQL. – Matthias Aug 29 '23 at 15:05
  • You don't see the database because it is empty. It must be first populated with `trytond-admin --all -d dbname`. That will prompt you for the admin password and more. – N1ngu Aug 29 '23 at 15:51