Create a couchbase bucket during build of the container

We are using docker to automate the develop environments through docker compose. We use couchbase heavily in our applications. One problem we faced is that if you boot up the docker image of couchbase, you will have to manually perform the setup process, create buckets and so on. Performing all of these steps manually and everytime is time consuming and not at all ideal.

I will show you how we solved this problem and automated these steps through Dockerfile

The key thing here is that Couchbase exposes a set of services to perform each of the operation which can be invoked just after the server start.

We are using docker-compose and hence our docker-compose.yml is as follows:

version: '2'
    build: couchbase/
     - 8091:8091
     - 8092:8092 
     - 8093:8093 
     - 11210:11210

Through build: couchbase/, I am pointing to a couchbase directory which contains our Dockerfile and a script which invokes the REST API endpoints of couchbase.

Here is the very simplistic Dockerfile

FROM couchbase:community-3.1.3

COPY /opt/couchbase

CMD ["/opt/couchbase/"]

We use the following script to configure the node.

All the files are given in this github repo

set -m

/ couchbase-server &

sleep 60

# Setup index and memory quota
curl -v -X POST -d memoryQuota=300 -d indexMemoryQuota=300

# Setup services
curl -v -d services=kv%2Cn1ql%2Cindex

# Setup credentials
curl -v -d port=8091 -d username=Administrator -d password=password

# Load travel-sample bucket
#curl -v -u Administrator:password -X POST -d '["travel-sample"]'
curl -X POST -u Administrator:password -d name=default -d ramQuotaMB=100 -d authType=none -d replicaNumber=2 -d proxyPort=11215

curl -X POST -u Administrator:password -d name=feed -d ramQuotaMB=100 -d authType=none -d replicaNumber=2 -d proxyPort=11215

# Setup Memory Optimized Indexes
curl -i -u Administrator:password -X POST -d 'storageMode=memory_optimized'

echo "Type: $TYPE, Master: $COUCHBASE_MASTER"

if [ "$TYPE" = "worker" ]; then
  sleep 15
  set IP=`hostname -I`
  couchbase-cli server-add --cluster=$COUCHBASE_MASTER:8091 --user Administrator --password password --server-add=$IP
  # TODO: Hack with the cuts, use jq may be better.
  #KNOWN_NODES=`curl -X POST -u Administrator:password http://$COUCHBASE_MASTER:8091/controller/addNode \
  #  -d hostname=$IP -d user=Administrator -d password=password -d services=kv,n1ql,index | cut -d: -f2 | cut -d\" -f 2 | sed -e   's/@/%40/g'`

  if [ "$AUTO_REBALANCE" = "true" ]; then
    echo "Auto Rebalance: $AUTO_REBALANCE"
    sleep 10
    couchbase-cli rebalance -c $COUCHBASE_MASTER:8091 -u Administrator -p password --server-add=$IP
    #curl -v -X POST -u Administrator:password http://$COUCHBASE_MASTER:8091/controller/rebalance --data "knownNodes=$KNOWN_NODES&ejectedNodes="

fg 1