How to have two services communicate with each other?

I would like to run nextcloud with postgres as a database and deploy that on my Qnap server. I have experimented with the nextcloud docker image and that works fine. Same with the postgresql docker image. Problems start when I try to connect it with a postgresql database. When I use nextcloud and postgresql in a docker-compose file I seem not to be able to have nextcloud use the postgresql database. Well, nextcloud keeps complaining I’m still using sqlite so it doesn’t see postgres. Fact is I do not understand how to organize nextcloud and postgres in docker in such a way that nextcloud ‘sees’ postgresql. Could someone help me out with this?

My docker-compose.yml:

version: '3'

volumes:
  nextcloud:

services:
  db:
    image: postgres:10.12-alpine # use version 10.12 of postgres, still works with pgadmin3
    restart: always
    ports:
      - '5433:5432' # expose 5433 on host and sent to 5432 in container
    volumes:
      - /share/files/dbms/pg-data-nextcloud:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD="Hallo Postgres!"
    
  app:
    image: nextcloud
    restart: always
    ports:
      - 8082:80
    volumes:
      - nextcloud:/var/www/html
    environment:
      - POSTGRES_HOST=db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD="Hallo Postgres!"
    depends_on:
      - db

As far as I can say this is not docker related. You’ll need to configure the use of Postgres within the config/config.php file -> https://docs.nextcloud.com/server/15/admin_manual/configuration_database/linux_database_configuration.html

As [mgabeldocker] mentioned: you have to set nextcloud to use a postgresql database instead of sqlite. You can see how on the configuration above. That’s not a docker issue.

I would just add that in nextcloud’s config, to set your postgresql server, you should use db as the server name as this is your postgresql service name.

Thanks @mgabeldocker and rodrigosevero,

It took me another day but I solved the problem. Trouble is that both nextcloud and postgres work in different services and what I did not realise (I am really a Docker noob) is that both services act as different hosts: one at IP 172.incrementing number.0.2 and the other at IP 172.same incrementing number.0.3. I had to set the POSTGRESQL_HOST variable, but as the number increments at each docker-composer up I was unable to use the IP address.

However, when both instances are running there are two services: nextcloud_db_1 and nextcloud_app_1. These names can be used as host names so by setting POSTGRESQL_HOST=nextcloud_db_1 nextcloud was able to find the postgresql service.

That was a hefty introduction to Docker networking to me. The docker-compose.yml:

version: '3'

volumes:
  nextcloud:

services:
  db:
    image: postgres:10.12-alpine # use version 10.12 of postgres, still works with pgadmin3
    restart: always
    
    # Postgres port 5432 is open for access, meaning that nextcloud (and any other
    # service in this docker) should access postgres over port 5432.
    # However, this port is *exposed* to the outside world as 5433 and is mapped 
    # to 5432.
    ports:
      - 5433:5432 # expose 5433 on host and sent to 5432 in container
    volumes:
    # /share/files/dbms/pg-data-nextcloud
      - /var/lib/postgresql/data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=nextcloud_db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=<Password>
    
  app:
    image: nextcloud
    restart: always
    ports:
      - 8082:80
    volumes:
      - nextcloud:/var/www/html
    environment:
      - POSTGRES_HOST=nextcloud_db_1 # service name for postgres as assigned by Docker
      - POSTGRES_DB=nextcloud_db
      - POSTGRES_USER=postgres # will access postgres over 5432
      - POSTGRES_PASSWORD=<password>
    depends_on:
      - db