How do I add an external dockerized database to a docker-compose install of nextcloud?

Hello I am fairly new. I have been trying to link nextcloud to a separate database. I am trying to centralize all of my docker apps to have one database instead of spinning up a separate database for each app. I keep getting error internal server issue when I try to run the compose file.
This is the docker compose file.

  nextcloud:
    image: nextcloud
    restart: always
    ports:
      - 8910:80
    volumes:
      - /appdata/nextcloud/main:/var/www/html
      - /appdata/nextcloud/data:/var/www/html/data
      - /appdata/nextcloud/config:/var/www/html/config
      - /appdata/nextcloud/apps:/var/www/html/custom_apps
      - /appdata/nextcloud/theme:/var/www/html/themes/<YOUR_CUSTOM_THEME>
    networks:
      - db
    environment:
      - REDIS_HOST= 192.168.xxx.xxx:6379
      - MARIADB_HOST= 192.168.xxx.xxx:3306
      - MARIADB_DATABASE= xxx
      - MARIADB_USER= xxx
      - MARIADB_PASSWORD= xxx
      - PUID= xxx
      - GUID= xxx
networks:
  db:
    external: true```

Thanks for the help.

Try this:

version: '3'
services:
  db:
    image: mariadb
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - ./db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=
      - MYSQL_PASSWORD=
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=

  nextcloud:
    image: nextcloud
    ports:
      - 8910:80
    links:
      - db
    volumes:
      - /appdata/nextcloud/main:/var/www/html
      - /appdata/nextcloud/data:/var/www/html/data
      - /appdata/nextcloud/config:/var/www/html/config
      - /appdata/nextcloud/apps:/var/www/html/custom_apps
      - /appdata/nextcloud/theme:/var/www/html/themes/<YOUR_CUSTOM_THEME>
    restart: always

hi @tonlaermai welcome to the forum :handshake:

in general exactly the opposite approach is used in Docker. Having multiple DB images running might consume little more RAM but it allows much better isolation and for this reason flexibility. Depending on the applications you might need run different DB server version which is hard(er) in centralized setup.

By default all containers from one docker compose can connect to each other using “internal” (default) network, which is silently created by docker compose. as described in GitHub - nextcloud/docker: ⛴ Docker image of Nextcloud

if you still want external/centralized DB define an “external” network and connect both database and application container to this network. If DB and application are defined in different compose files you need to create an “external” network and explicitly connect the service with this network:

version: '3.9'
services:
  nextcloud:
    networks:
      db:
networks:
  db:
    external: true

Your config looks right for nextcloud service, but the other side is not shown.

Did you connect the database to the db network as well? Is there a reason why you configure db connection with an IP and not DNS? upon recreating DB container the IP will likely change.

remark

I’m not 100% sure but I’m under impression links: directive doesn’t allow application and database on different networks: Services top-level element | Docker Documentation

If services do declare networks they are attached to, links should not override the network configuration and services not attached to a shared network should not be able to communicate.

Reference

You are right, I totally missed that

I did connect the database to the same network. I wasn’t really sure how to link an external service without directly defining it. I figured it would be fine since I set up the databases to have the port open. I also could reach them with adminer. Here is the database stack I made.

services: 
  mariadb:
    image: mariadb
    container_name: mariadb
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    privileged: true
    environment:
      MARIADB_USER: xxx
      MARIADB_PASSWORD: xxx
      MARIADB_DATABASE: xxx
      PUID: xxx
      PGID: xxx
    ports:
      - "3306:3306"
    volumes:
      - /database/MariaDB:/var/lib/mysql:rw
    networks:
      - db

  postgresdb:
    image: postgres
    container_name: postgres
    privileged: true
    environment:
      POSTGRES_USER: xxx
      POSTGRES_PASSWORD: xxx
      POSTGRES_DB: xxx
      PUID: xxx
      PGID: xxx
    ports:
      - "5432:5432"
    volumes:
      - /database/PostgresDB:/var/lib/postgresql/data:rw
    networks:
      - db
      
  redisdb:
    image: redis
    container_name: redis
    restart: always
    command: redis-server --save 20 1 --loglevel warning 
    privileged: true
    ports:
      - "6379:6379"
    volumes:
      - /database/RedisDB:/data:rw
    networks:
      - db
      
  adminerdb:
    container_name: adminer
    image: adminer
    restart: always
    ports:
      - "8080:8080"
    networks:
      - db

networks:
  db:
    external: true```

I also tried to use the `links:` but I got a service not defined in the compose file error and when i tried to use `external-links:` I got service unsupported. I wanted to reduce the system resources so that I can run multiple game servers on the machine more comfortably. Is there a resource on how to manage databases for apps and best practices? I appreciate the help.

form this configuration you should see all the container and their IP addresses within db network if you run docker inspect network db… all the containers connected to this network are expected to reach each other using their service names e.g. ‘postgres’ or ‘mariadb’ no need to use IP addresses.