Can't use N1QL on docker-compose


#1

I created docker-compose file for couchbase cluster.
But N1QL don’t work.
HELP ME !!

couchbase docker version: couchbase:community

/Applications/Couchbase\ Server.app/Contents/Resources/couchbase-core/bin/cbq -engine=http://localhost:8091/

Couchbase query shell connected to http://localhost:8091/ . Type Ctrl-D to exit.
cbq> SELECT DISTINCT type FROM `beer-sample`;
Requested resource not found.

/Applications/Couchbase\ Server.app/Contents/Resources/couchbase-core/bin/cbq -engine=http://localhost:8093/

Couchbase query shell connected to http://localhost:8093/ . Type Ctrl-D to exit.
cbq> SELECT DISTINCT type FROM `beer-sample`;
 ERROR 5000 : Post http://localhost:8093/query: EOF 

docker-compose.yml

services:
  couchbase1:
    container_name: couchbase1
    extends:
      file: common.yml
      service: couchbase
    volumes:
      - ./export/var/couchbase/node1:/opt/couchbase/var
    ports:
      - 8091:8091
      - 8092:8092
      - 8093:8093
      - 8094:8094
      - 11210:11210
  couchbase2:
    container_name: couchbase2
    extends:
      file: common.yml
      service: couchbase
    volumes:
      - ./export/var/couchbase/node2:/opt/couchbase/var
  couchbase3:
    container_name: couchbase3
    extends:
      file: common.yml
      service: couchbase
    volumes:
      - ./export/var/couchbase/node3:/opt/couchbase/var

  intializer:
    container_name: cbclusterinit
    depends_on:
      - couchbase1
      - couchbase2
      - couchbase3
    environment:
      COUCHBASE_HOSTS: "couchbase1,couchbase2,couchbase3"
      COUCHBASE_PORT: 8091
      COUCHBASE_BUCKET: mybucket
    build: images/cbclusterinit
    image: avcbinit:custom

shell in intializer

#!/bin/bash

# Initialize a couchbase cluster.  The comma-separated list of hosts can be
# set in $COUCHBASE_HOSTS in the docker-compose.yml file.

echo hosts ${COUCHBASE_HOSTS}

: ${COUCHBASE_HOSTS:=couchbase1}
: ${COUCHBASE_PORT:=8091}
: ${COUCHBASE_BUCKET:=default}
: ${USER:=couchbase}
: ${PASSWORD:=couchbase}
: ${CLUSTER_RAMSIZE:=512}
: ${BUCKET_RAMSIZE:=100}
: ${BUCKET_REPLICAS:=1}
: ${BUCKET_TYPE:=couchbase}

IFS=', ' read -r -a array <<< "${COUCHBASE_HOSTS}"
first_node=${array[0]}

s=1
i=0
while ! nc -w 1 $first_node $COUCHBASE_PORT 2>/dev/null
do
  i=$((i+1))
  if [ $i -gt 5 ]; then
    exit 1
  fi
  echo "Couchbase is not yet available at ${first_node}:${COUCHBASE_PORT}, sleeping $s seconds..."
  sleep $s
  s=$((s*2))
done

echo "Initializing Couchbase first node ${first_node}..."
/opt/couchbase/bin/couchbase-cli bucket-create \
  -c ${first_node}:${COUCHBASE_PORT} \
  -u ${USER} -p ${PASSWORD} \
  --bucket=${COUCHBASE_BUCKET} \
  --bucket-type=${BUCKET_TYPE} \
  --bucket-ramsize=${BUCKET_RAMSIZE} \
  --bucket-replica=${BUCKET_REPLICAS}
/opt/couchbase/bin/couchbase-cli cluster-init \
  -c ${first_node}:${COUCHBASE_PORT} \
  --cluster-init-username=${USER} --cluster-init-password=${PASSWORD} \
  --cluster-init-port=${COUCHBASE_PORT} \
  --cluster-init-ramsize=${CLUSTER_RAMSIZE}
	# --services=data,index,query
/opt/couchbase/bin/couchbase-cli cluster-init \
  -c ${first_node}:${COUCHBASE_PORT} \
  -u ${USER} -p ${PASSWORD} \
  --cluster-init-port=${COUCHBASE_PORT} \
  --cluster-init-ramsize=${CLUSTER_RAMSIZE}
	# --services=data,index,query

echo "Initializing couchbase cluster"
for h in "${array[@]:1}"
do
    echo "Adding $h to cluster..."
    IP=$(getent hosts ${h} | awk '{print $1}')
    s=1
    i=0
    while ! nc -w 1 $h $COUCHBASE_PORT 2>/dev/null
    do
      i=$((i+1))
      if [ $i -gt 5 ]; then
        exit 2
      fi
      echo "Couchbase is not yet available at ${h}:${COUCHBASE_PORT}, sleeping $s seconds..."
      sleep $s
      s=$((s*2))
    done
    /opt/couchbase/bin/couchbase-cli server-add \
          --cluster=${first_node}:${COUCHBASE_PORT} \
          -u ${USER} -p ${PASSWORD} \
          --server-add=${IP}:${COUCHBASE_PORT} \
          --server-add-username=${USER} \
          --server-add-password=${PASSWORD}
done

echo "Reblancing cluster"
/opt/couchbase/bin/couchbase-cli rebalance -c ${first_node}:${COUCHBASE_PORT} -u ${USER} -p ${PASSWORD}

# Disable runnability so we only run once...
chmod a-x /cbinit-entrypoint.sh

echo "Cluster ready"

Return connection refused and panic error occurs on Couchbase server
#2

Copying @arungupta, our Docker expert.


#3

what does common.xml look like? Its not clear which Docker image is being used.

depends_on only ensures that the container is started. It does not mean that the application inside the container is started as well. It takes a while for all the services (query, index and data) services to be ready before the container is ready to serve requests. Here is an example of how I make check that the services are ready to use:

Have you tried invoking N1QL query using a single Couchbase container using docker run -d -p 8091:8094-8091-8094 -p 11210:11210 couchbase/server:sandbox?


#4

Thank you, I try.

Sorry, I didn’t write common.xml

common.xml

version: "2"

services:
  couchbase:
    image: couchbase:community

#5

@usk I created a simple Docker Compose file, connected to it using CBQ and run N1QL query as well.

More details in a blog for you: http://blog.couchbase.com/couchbase-using-docker-compose/

Let me know how it goes.


#6

I could used N1QL.

@arungupta
Thank you. This blog was very helpful for me.

change some settings:

  • change container from couchbase:latest to couchbase:community
  • change storageMode from memory_optimized to forestdb (memory_optimized is not supported community edition)
  • change memory size from 300 to 512
  • add volumes setting in docker-compose.yml

My Dockerfile and docker-compose.yml:


#7

Cool, glad to know this worked out.