.netdockernetwork-programmingcontainerscouchbase

How to connect multinode Couchbase docker container from external .net app?


I have a .NET API app and Couchbase Database. Couchbase Database is running as docker container and with one host multinode configuration. My dotnet app is outside the docker environment and external client of Couchbase Docker Container. Docker-compose configuration is below and I can access web consoles of Couchbase with URLs http://localhost:8091 and http://localhost:8094. I tried to connect from .net app with connection strings couchbase://localhost, couchbase://localhost:8091,localhost:8094, couchbase://172.20.0.2,172.20.0.3, couchbase://172.20.0.2:8094,172.20.0.3:8091, couchbase://192.168.65.0 (Docker Host Default IP Address) etc. but cannot connect. But when I configure Couchbase as single node on single host, I can connect from .net app. It looks like some network configuration is missing, I published ports as mentioned below but nothing changed. Anyone who helps me, I will be appreciated.

   version: "3.0"
services:
 
   couchbase-node1:
   image: couchbase/server:community-7.6.1
   container_name: couchbase-node1-container
   ports:
     - "8091:8091"  # Couchbase Web Console
     - "8092:8092"  # Couchbase Query Service
     - "8093:8093"  # Couchbase Index Service
     - "11210:11210"  # Couchbase Data Service
   environment:
     - CLUSTER_INIT_USERNAME=Administrator
     - CLUSTER_INIT_PASSWORD=couchbase2024*
     - SERVICES=data,index,query,fts
     - USERNAME=Administrator # Couchbase Server Default Username, custom usernames is not accepted!
     - PASSWORD=couchbase2024*
   networks:
     couchbase-net:
       ipv4_address: 172.20.0.3
       aliases:
        - couchbase-node1
   volumes:
     - node1_data:/opt/couchbase/var
 couchbase-node2:
   image: couchbase/server:community-7.6.1
   container_name: couchbase-node2-container 
   ports:
     - "8094:8091"  # Couchbase Web Console
     - "8095:8092"  # Couchbase Query Service
     - "8096:8093"  # Couchbase Index Service
     - "11211:11210"  # Couchbase Data Service
   environment:
     - CLUSTER_JOIN=couchbase-node1
     - USERNAME=Administrator
     - PASSWORD=couchbase2024*
   networks:
     couchbase-net:
       ipv4_address: 172.20.0.2
       aliases:
         - couchbase-node2
   volumes:
     - node2_data:/opt/couchbase/var
   depends_on:
     - couchbase-node1
volumes:
 local_pgdata:
 pgadmin-data:
 es-data:
 node1_data:
 node2_data:
networks:
 es-net:
   driver: bridge
 couchbase-net:
   driver: bridge
   ipam:
     driver: default
     config:
       - subnet: 172.20.0.0/24

Solution

  • A couple potential issues here. First, is that you're using 8094-8096 as external ports, which is probably not the issue (see below), but all ports from 8091-8097 are used for Couchbase network traffic, so by reassigning them, you might be blocking or at least confusing the client.

    However, based on experience, I think the main issue is:

    My dotnet app is outside the docker environment and external client of Couchbase Docker Container

    Couchbase clients need to access to every node of Couchbase. As far as I know, you can't run multiple nodes inside of the Docker host and access all of them, because you can only expose a given port for a single node inside of Docker. (e.g. you can expose port 8091 for node A external to Docker host, but then node B and C can't be exposed via the same port).

    Assuming that you're doing this for local development, here's what I would recommend:

    There may be another solution for someone who is a Docker/networking ninja (that person isn't me), but I haven't seen it.