Couchbase XDCR using Docker Swarm, Machine and Compose

Cross Datacenter Replication (XDCR) in Couchbase provides an easy way to replicate data from one cluster to another. The clusters are typically set in geographically diverse data centers. This enables for disaster recovery or to bring data closer to users for faster data access.

This blog will show:

  • Setup two data centers using Docker Swarm
  • Run Couchbase containers on each node of Docker Swarm
  • Setup a Couchbase cluster on each Docker Swarm cluster
  • Configure one-way XDCR between two Couchbase clusters

For the purpose of this blog, the two data centers will be setup on a local machine using Docker Machine.


Complete code used in this blog is available at:

Create Consul Discovery Service

Each node in Docker Swarm needs to be registered with a discovery service. This blog will use Consul for that purpose. And even Consul will be running on a Docker Machine.

Typically, you’ll run a cluster of Consul but for simplicity a single instance is started in our case.

Create a Docker Machine and start Consul using this script:

Create Docker Swarm cluster

Docker Swarm allows multiple Docker hosts to be viewed as a single unit. This allows your multi-container applications to easily run on multiple hosts. Docker Swarm serves the same Remote API as served by a single host. This allows your existing tools to target a single host or a cluster of hosts.

Both the Docker Swarm clusters will be registered with a single discovery service. This is achieved by using the following value for --swarm-discovery:

Create a Docker Swarm cluster using Docker Machine using this script:

The script needs to be invoked as:

This will create two Docker Swarm clusters with one “master” and two “worker” as shown below:

Consul is running on Docker Machine with IP address And so Consul UI is accessible at


It shows two Docker Swarm clusters that have been registered.

Exact list of nodes for each cluster can also be seen. Nodes in clusterA are shown:


Nodes in clusterB are shown:


Run Couchbase containers

Run Couchbase container on each node of Docker Swarm cluster using this Compose file.

Configure Docker CLI for the first cluster and run 3 containers:

Check the running containers:

Configure Docker CLI for the second cluster and run 3 containers:

Check the running containers:

Create/Rebalance Couchbase cluster

Scaling and Rebalancing Couchbase Cluster using CLI explains how to create a cluster of Couchbase nodes and rebalance an existing cluster using Couchbase CLI.

Create Couchbase cluster on each Swarm cluster using this script.

The script needs to be invoked as:

And now rebalance this cluster using this script:

This script is invoked as:

Couchbase Web Console for any node in the cluster will show the output:


Invoke this script to create the second Couchbase cluster as:

Rebalance this cluster as:

Couchbase Web Console for any node in the second cluster will show the output:


Setup XDCR

Cross datacenter replication can be setup to be uni-directional, bi-directional or multi-directional. Uni-directional allows data to replicated from source cluster to destination cluster, bi-directional allows replication both ways, multi-directional allows to configure in any direction.

We’ll create a simple uni-directional replication using this script:

This script is invoked as:

A bi-directional replication can be easily created by executing the commands again but reversing the source and destination cluster.

Couchbase Web Console for the source cluster will show:


Couchbase Web Console for the destination cluster will show: swarm-xdcr-couchbase-clusterB-xdcr


This blog shows how you can simplify your complex deployments using Docker Machine, Docker Swarm, and Docker Compose.


Be Sociable, Share!
  • Tweet

4 thoughts on “Couchbase XDCR using Docker Swarm, Machine and Compose

  1. Dear Arun Gupta, thanks for teaching us. I am trying to learn programming for almost 2 years and I think there is still a long way to go for me, now I understand there are lots of things to learn. Lots of people lost their mind for writing the code. Setup XDCR is a little bit confusing for me can u tell us briefly about this term? Thank you :)

  2. Play free online spider solitaire game,such a beautiful graphic game,i have played the more different game but spider solitaire free online,and play in PC,android and window mobile.

  3. Advices from might be useful for you if you are going to write your leadership essay. It was useful for me and my college friends

  4. The article you have shared here very awesome. I really like and appreciated your work. I read deeply your article, the points you have mentioned in this article are useful

Leave a Reply

Your email address will not be published. Required fields are marked *