Couchbase Forumshas a question Can’t use N1QL ondocker-compose . This blog will show how to run Couchbase using Docker Compose and run a N1QL query .


What is Docker Compose?
Docker Compose allows you to define your multi-container application with all of its dependencies in a single file, then spin your application up in a single command.
Docker Compose introduced v3 in Docker 1.13 . How do you know what version of Docker are you running?
docker version command gives you that information:
Client: Version:1.13.1 APIversion:1.26 Goversion:go1.7.5 Gitcommit:092cba3 Built:WedFeb8 08:47:51 2017 OS/Arch:darwin/amd64 Server: Version:1.13.1 APIversion:1.26 (minimumversion 1.12) Goversion:go1.7.5 Gitcommit:092cba3 Built:WedFeb8 08:47:51 2017 OS/Arch:linux/amd64 Experimental: true Couchbase Docker Compose FileNow if you see this version of Docker, then you can use the followingCompose file:
version: "3" services: db: image: arungupta/couchbase deploy: replicas: 1 ports: - 8091:8091 - 8092:8092 - 8093:8093 - 8094:8094 - 11210:11210In this Compose file:
v3version of Compose file . If you are using an older version of Docker, then you can consider using v2 version of Compose file . arungupta/couchbase Docker image is used to start Couchbase server. This image iscreatedas explained at github.com/arun-gupta/docker-images/tree/master/couchbase . It uses Couchbase REST API to pre-configure the Couchbase server. Ports 8091, 8092, 8093, 8094, 11210 are exposed. Only a single replica of Couchbase server is started.Couchbase can be started in a couple of ways using this Compose file.
Couchbase using Docker Compose onSingle Docker HostIf you want to start Couchbase on a single host (such as provisioned by Docker for Mac or a single Docker Machine), then use the command:
docker-composeup -dThis will show the warning message but starts Couchbase server:
WARNING: Someservices (db) use the 'deploy' key, whichwillbeignored. Composedoesnot supportdeployconfiguration - use `dockerstackdeploy` to deployto a swarm. Creatingcouchbase_db_1Check the status of started service using the command docker-compose ps :
NameCommandStatePorts ----------------------------------------------------------------------------------------------------------------- couchbase_db_1/entrypoint.sh /opt/couchbUp11207/tcp, ...0.0.0.0:11210->11210/tcp, 11211/tcp, 18091/tcp, 18092/tcp, 18093/tcp, 0.0.0.0:8091->8091/tcp, 0.0.0.0:8092->8092/tcp, 0.0.0.0:8093->8093/tcp, 0.0.0.0:8094->8094/tcpAll the exposed ports are shown and Couchbase is accessible at http://localhost:8091. Use the credentialsAdministrator/password to access the web console.
Now you can create buckets and connect from CBQ and run N1QL queries. For example:
/Users/arungupta/tools/couchbase/Couchbase\ Server\ 4.5\ EE.app/Contents/Resources/couchbase-core/bin/cbq -u Administrator -p password --enginehttp://localhost:8093 Connectedto : http://localhost:8093/. Type Ctrl-D or \QUIT to exit. Pathto historyfilefor theshell : /Users/arungupta/.cbq_history cbq> selectnow_str(); { "requestID": "d28280ab-49a4-4254-9f00-06bd1d2b4695", "signature": { "$1": "string" }, "results": [ { "$1": "2017-02-13T21:36:57.248Z" } ], "status": "success", "metrics": { "elapsedTime": "2.916653ms", "executionTime": "2.829056ms", "resultCount": 1, "resultSize": 56 } } cbq> selectversion(); { "requestID": "51091fa6-dcc5-40f6-9c2b-1eb6732630bb", "signature": { "$1": "string" }, "results": [ { "$1": "1.6.0" } ], "status": "success", "metrics": { "elapsedTime": "4.599365ms", "executionTime": "4.525552ms", "resultCount": 1, "resultSize": 37 } }Typically, you may be able to scale the services started by Docker Compose using docker-compose scale command. Butthis will not be possible in our case as the ports are exposed. Scaling a service will cause port conflict.
The container can be brought down using the command docker-compose down .
Couchbase using Docker Compose on Multi-host Swarm-mode ClusterDocker allows multiple hosts to be configured in a cluster using Swarm-mode. This can be configured using the command docker swarm init .
Once the cluster is initialized, then the Compose file can be used to start the cluster:
dockerdeploy --compose-file=docker-compose.ymlcouchbaseIt shows the output:
Creatingnetworkcouchbase_default Creatingservicecouchbase_dbThis creates a Docker service and the status can be seenusing the command docker service ls :
IDNAMEMODEREPLICASIMAGE 0zls1k4mgrrycouchbase_dbreplicated1/1arungupta/couchbase:latestCheck thetasks/containers running inside the service using the command docker service ps couchbase_db :
IDNAMEIMAGENODEDESIREDSTATECURRENTSTATEERRORPORTS vf5zicu4mheicouchbase_db.1arungupta/couchbase:latestmobyRunningRunning 3 hoursagoHere again, you can connect to the Couchbase server and run N1QL queries:
/Users/arungupta/tools/couchbase/Couchbase\ Server\ 4.5\ EE.app/Contents/Resources/couchbase-core/bin/cbq -u Administrator -p password --enginehttp://localhost:8093 Connectedto : http://localhost:8093/. Type Ctrl-D or \QUIT to exit. Pathto historyfilefor