Docker compose install fails, cannot connect to any DB type

Hello,

I am trying to host nextcloud using docker but I have encountered issues with the initial setup already.
Using the provided docker-compose file (without the volumes and actual env values) from the nextcloud GitHub page fails to connect to DB using Postgres and MariaDB as well.
docker-compose.yml

version: '3.5'

services:
  db:
    image: postgres
    restart: always
    container_name: db
    environment:
      POSTGRES_USER: 'postgres'
      POSTGRES_PASSWORD: 'secret'
  nextcloud:
    image: nextcloud
    ports:
      - 8000:80
    depends_on:
      - db
    restart: always
    environment:
      POSTGRES_DB: 'nextcloud'
      POSTGRES_USER: 'postgres'
      POSTGRES_PASSWORD: 'secret'
      POSTGRES_HOST: 'db'
      NEXTCLOUD_ADMIN_USER: 'admin'
      NEXTCLOUD_ADMIN_PASSWORD: 'admin'

Postgres logs:

db | The files belonging to this database system will be owned by user “postgres”.
db | This user must also own the server process.
db |
db | The database cluster will be initialized with locale “en_US.utf8”.
db | The default database encoding has accordingly been set to “UTF8”.
db | The default text search configuration will be set to “english”.
db |
db | Data page checksums are disabled.
db |
db | fixing permissions on existing directory /var/lib/postgresql/data … ok
db | creating subdirectories … ok
db | selecting dynamic shared memory implementation … posix
db | selecting default max_connections … 100
db | selecting default shared_buffers … 128MB
db | selecting default time zone … Etc/UTC
db | creating configuration files … ok
db | running bootstrap script … ok
db | performing post-bootstrap initialization … ok
db | syncing data to disk … ok
db |
db |
db | Success. You can now start the database server using:
db |
db | pg_ctl -D /var/lib/postgresql/data -l logfile start
db |
db | initdb: warning: enabling “trust” authentication for local connections
db | You can change this by editing pg_hba.conf or using the option -A, or
db | --auth-local and --auth-host, the next time you run initdb.
db | waiting for server to start…2020-08-27 18:21:27.139 UTC [46] LOG: starting PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db | 2020-08-27 18:21:27.142 UTC [46] LOG: listening on Unix socket “/var/run/postgresql/.s.PGSQL.5432”
db | 2020-08-27 18:21:27.158 UTC [47] LOG: database system was shut down at 2020-08-27 18:21:26 UTC
db | 2020-08-27 18:21:27.163 UTC [46] LOG: database system is ready to accept connections
db | done
db | server started
db |
db | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db |
db | 2020-08-27 18:21:27.231 UTC [46] LOG: received fast shutdown request
db | waiting for server to shut down…2020-08-27 18:21:27.234 UTC [46] LOG: aborting any active transactions
db | 2020-08-27 18:21:27.237 UTC [46] LOG: background worker “logical replication launcher” (PID 53) exited with exit code 1
db | 2020-08-27 18:21:27.237 UTC [48] LOG: shutting down
db | 2020-08-27 18:21:27.253 UTC [46] LOG: database system is shut down
db | done
db | server stopped
db |
db | PostgreSQL init process complete; ready for start up.
db |
db | 2020-08-27 18:21:27.344 UTC [1] LOG: starting PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db | 2020-08-27 18:21:27.344 UTC [1] LOG: listening on IPv4 address “0.0.0.0”, port 5432
db | 2020-08-27 18:21:27.344 UTC [1] LOG: listening on IPv6 address “::”, port 5432
db | 2020-08-27 18:21:27.348 UTC [1] LOG: listening on Unix socket “/var/run/postgresql/.s.PGSQL.5432”
db | 2020-08-27 18:21:27.364 UTC [55] LOG: database system was shut down at 2020-08-27 18:21:27 UTC
db | 2020-08-27 18:21:27.369 UTC [1] LOG: database system is ready to accept connections

Nextcloud logs:

nextcloud_1 | Initializing nextcloud 19.0.1.1 …
nextcloud_1 | Initializing finished
nextcloud_1 | New nextcloud instance
nextcloud_1 | Installing with PostgreSQL database
nextcloud_1 | starting nextcloud installation
nextcloud_1 | PostgreSQL username and/or password not valid
nextcloud_1 | -> You need to enter details of an existing account.
nextcloud_1 | retrying install…
nextcloud_1 | An unhandled exception has been thrown:
nextcloud_1 | OC\DatabaseException: An exception occurred while executing ‘SHOW SERVER_VERSION’:
nextcloud_1 |
nextcloud_1 | Failed to connect to the database: An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: No route to host
nextcloud_1 | Is the server running on host “db” (172.30.0.2) and accepting
nextcloud_1 | TCP/IP connections on port 5432? in /var/www/html/lib/private/legacy/OC_DB.php:73
nextcloud_1 | Stack trace:
nextcloud_1 | #0 /var/www/html/lib/private/legacy/OC_DB.php(139): OC_DB::prepare(‘SHOW SERVER_VER…’, NULL, NULL)
nextcloud_1 | #1 /var/www/html/lib/private/legacy/OC_Util.php(971): OC_DB::executeAudited(Array)
nextcloud_1 | #2 /var/www/html/lib/private/legacy/OC_Util.php(951): OC_Util::checkDatabaseVersion()
nextcloud_1 | #3 /var/www/html/lib/private/Console/Application.php(161): OC_Util::checkServer(Object(OC\SystemConfig))
nextcloud_1 | #4 /var/www/html/console.php(99): OC\Console\Application->loadCommands(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
nextcloud_1 | #5 /var/www/html/occ(11): require_once(’/var/www/html/c…’)
nextcloud_1 | #6 {main}retrying install…

The retrying goes on until it dies.

Starting the nextcloud with Mariadb connection results in the same issues of not being able to connect to the DB.

nextcloud_1 | Initializing nextcloud 19.0.1.1 …
nextcloud_1 | Initializing finished
nextcloud_1 | New nextcloud instance
nextcloud_1 | Installing with MySQL database
nextcloud_1 | starting nextcloud installation
nextcloud_1 | Error while trying to create admin user: Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2002] No route to host
nextcloud_1 | ->
nextcloud_1 | retrying install…
nextcloud_1 | Error while trying to create admin user: Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2002] No route to host
nextcloud_1 | ->
nextcloud_1 | retrying install…

No networks here, just a plain setup.

The problem here is you aren’t connecting your database to nextcloud. Docker by default won’t share files from one container to another. You need to tell both containers to read and write from a specific directory. In your database, add:

  volumes:
    - db:/var/lib/postgresql/data

and then in nextcloud add:

  volumes:
    - nextcloud:/var/www/html
  environment:
    - POSTGRES_HOST=db

Then add, in it’s own section at the bottom,

volumes:
  db:
  nextcloud:

check out the official examples for docker-compose files here: https://github.com/nextcloud/docker/tree/master/.examples/docker-compose

For the basic compose file for PostgreSQL, see: https://github.com/nextcloud/docker/tree/master/.examples/docker-compose/insecure/postgres/apache

Hi,
Actually it turned out that the issue was that the masquerade option wasn’t enabled under firewalld so they couldn’t really “find” each other.
All the containers were in the same network created by docker-compose, the files do not need to be shared, they both manage their own data and no need for shared database raw data

You can also connect them via TCP inside the defined docker network. The compose file service name will resolve to the correct backend IP on the container. This avoids having to cross-mount volumes.