docker-composenextcloud

Nextcloud unknown host when trying to connect to Postgres with Docker’s IP


I tried creating Docker Compose Nextcloud and the db (Postgres), but upon creating a new user, the db is not recognized, even though on the “config/config.php” file the configuration is there. I’m assuming the problem is with the Docker’s local IP since I’m using Docker’s network to access the site locally (correct me if I’m wrong).

This is my docker-compose config, (I’m using Portainer).

version: '3'

services:
  db:
    image: postgres:alpine
    hostname: db
    restart: always
    network_mode: bridge
    volumes:
      - /var/lib/docker/volumes/nextcloud/postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=nextcloud_db
      - POSTGRES_USER=nextcloud
      - POSTGRES_PASSWORD=admin

  redis:
    image: redis:alpine
    restart: always
    network_mode: bridge

  app:
    image: nextcloud:apache
    restart: always
    network_mode: bridge
    ports:
      - 172.17.0.1:8080:80
    volumes:
      - /var/lib/docker/volumes/nextcloud/nextcloud:/var/www/html
    environment:
      - POSTGRES_HOST=db
      - POSTGRES_DB=nextcloud_db
      - POSTGRES_USER=nextcloud
      - POSTGRES_PASSWORD=admin
      - REDIS_HOST=redis

    depends_on:
      - db
      - redis

I tried opening port for the postgres using

ports:
  - 172.17.0.1:5432:5432

And changing POSTGRES_HOST=172.17.0.1:5432.

But upon creating admin user, the server instead returned internal error.


Solution

  • It took me a few hours to figure this out. It turns out the problem is indeed caused by the Docker's network. The issue is with the network mode itself, which is bridge (the default bridge network). This mode doesn't really support DNS resolution by name (CMIIW). A possible solution to my current problem is to either change REDIS_HOST=172.17.0.x and POSTGRES_HOST=172.17.0.x (with the ip assigned to the container itself), or to create a custom network and assign it to the containers.

    version: '1'
    services:
      app: 
        image: nextcloud
        container_name: nextcloud-app
        restart: always
        depends_on: 
          - postgres
          - redis
        networks:
          - default_network
        ports: 
          - 172.18.0.1:8080:80
        environment: 
          - POSTGRES_HOST=postgres
          - POSTGRES_DB=nextcloud_db
          - POSTGRES_USER=postgres 
          - POSTGRES_PASSWORD=secretpassword
          - REDIS_HOST=redis
          - REDIS_PORT=6379
        volumes: 
          - ./nextcloud/nextcloud:/var/www/html
          - ./nextcloud/apps:/var/www/html/custom_apps
          - ./nextcloud/config:/var/www/html/config
          - ./nextcloud/data:/var/www/html/data
          - ./nextcloud/themes:/var/www/html/themes
    
      postgres:
          image: postgres
          container_name: nextcloud-postgres
          restart: always
          networks:
            - default_network
          environment:
            - POSTGRES_DB=nextcloud_db
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=secretpassword
          volumes:
            - ./nextcloud/postgres:/var/lib/postgresql/data
      redis:
        image: redis:latest
        container_name: nextcloud-redis
        restart: always
        command: redis-server
        networks:
          - default_network
    
    networks:
      default_network:
         name: default_network
         external: true