Ratnopam Chakrabarti is a software developer currently working for Ericsson Inc. He has been focused on IoT, machine-to-machine technologies, connected cars, and smart city domains for quite a while. He loves learning new technologies and putting them to work. When he’s not working, he enjoys spending time with his 3-year-old son.

Ratnopam Chakrabarti

Introduction

Running Couchbase as a Docker container is fairly easy. Simply inherit from the latest, official Couchbase image and add your customized behavior according to your requirement. In this post, I am going to show how you can fire up a web application using Spring Boot, Vaadin, and of course Couchbase (as backend)– all using Docker.

This is part one of a two-part series where I am going to describe ways to run a fully featured web application powered by Couchbase as the NoSQL backend using Docker toolsets. In this post, I will describe the steps to set up and configure a Couchbase environment using Docker; I will also mention ways to Dockerize the web application (in this case, it’s a Spring Boot application with Vaadin) and talk to the Couchbase backend for the CRUD operations.

Prerequisites

Docker needs to be set up and working. Please refer to the following link for details of the installation: https://docs.docker.com/engine/installation/ If you are on macOS or Windows 10, you can go for native Docker packages. If you are on an earlier version of Windows (7 or 8) like me, then you can use Docker Toolbox which comes with Docker achine.

The Application

Ours is a simple CRUD application for maintaining a bookstore. Users of the application can add books by entering information such as title and/or author, and can then view the list of books, edit some information, and even delete the books. The app is built on Spring Boot. The backend is powered by Couchbase 4.6, and for the front-end I have used Vaadin 7 since it has pretty neat integration with the Spring Boot framework.

The main steps to build this app are listed below:

  • Run and configure Couchbase 4.6, including setting up the bucket and services using Docker.
  • Build the application using Spring Boot, Vaadin, and Couchbase.
  • Dockerize and run the application.

Run Couchbase 4.6 using Docker

Check your Docker host IP. You can use:

The next task is to write the Dockerfile to run and configure Couchbase. For our application to talk to the Couchbase backend, we need to set up a bucket named “books” and also enable the index query services on the Couchbase node. The Dockerfile to all of this can be found here.

The Dockerfile uses a configuration script to set up the Couchbase node. Couchbase offers REST endpoints that can easily enable services such as querying, N1QL, and index. One can also set up buckets using these REST APIs. The configuration script can be downloaded from here.

Let’s try to build and run the Couchbase image now.

Go to the directory where the Dockerfile is.

 

REPOSITORY                   TAG                    IMAGE ID            CREATED             SIZE

chakrar27/couchbase          books               93e7ba199eef        1 hour ago         581 MB

couchbase                    latest              337dab68d2d1        9 days ago          581 MB

Run the image by typing

Sample output:

Verify the configuration by typing http://192.168.99.100:8091 into your favorite browser.

Configuration

Type “Administrator” as Username and “password” in the Password field and click Sign In.

Check the settings of the Couchbase node and verify that it is according to the configure.sh we used above.

Couchbase Setting Cluster Ram Quota

The bucket “books”.

Data bucket settings

At this point our back-end Couchbase infrastructure is up and running. We now need to build an application that can use this backend to build something functional.

Build the application using Spring Boot, Vaadin, and Couchbase

Go to start.spring.io and add Couchbase as a dependency. This would place spring-data-couchbase libraries in the application classpath. Since Couchbase is considered a first-class citizen of the Spring Boot ecosystem, we can make use of the Spring Boot auto-configuration feature to access the Couchbase bucket at runtime.

Also, add Vaadin as a dependency in the project. We are going to use it for building the UI layer.

The project object model (pom) file can be found here.

We create a Couchbase repository like this:

The annotations ensure that a View named “book” will be supplied at runtime to support view-based queries. A primary index will be created to support N1QL queries. In addition, a secondary index will also be provided.

The methods have been defined to return List<Book>. We don’t have to provide any implementation since that is already provided behind the scenes by the spring-data-couchbase.

We need to define the entity, which in our case is Book. We annotate it with @Document.

@Document

To enable auto-configuration, use application.properties or application.yml file as shown below:

One thing to note here is that when the application container runs, it would need to connect to the Couchbase container and set up the auto-configuration. The property spring.couchbase.bootstrap-hosts lists the IP address of the Couchbase node. Here, I have specified 127.0.0.1 which is not going to work since at runtime, the app container will not find the Couchbase container running in that IP. So we need to pass an environment variable (env variable) when running the Docker image of the application.

In order to pass an env variable as mentioned above, we need to write the Dockerfile of the application such that the value of the spring.couchbase.bootstrap-hosts property can be passed as an env variable. Here’s the Dockerfile of the app:

As you can see, we are basically overriding the value of the spring.couchbase.bootstrap-hosts property defined in the application.properties file by the env variable HOSTS.

This is pretty much all we have to do to wire Spring Boot with Couchbase.

UI (U and I)

For UI, we make use of the spring-vaadin integration. I am using version 7.7.3 of Vaadin, vaadin-spring version 1.1.0, and “Viritin,” a useful Vaadin add-on. To install Viritin, add the following dependency:

Annotate the UI class as

@SpringUI

@Theme(“valo”)

public class BookstoreUI extends UI {

//////

}

And then hook the repository methods with the UI elements.

A bean that implements the CommandLineRunner interface is used to prepopulate the database with some initial values.

For full source code, refer to this link.

Dockerize the application

Using Maven, it’s very easy to Dockerize an application using Spotify’s docker-maven plugin. Please check the pom.xml file plugin section.

Alternatively, you can build using Docker command line ->

And then run the image -> Note that we need to pass the value of the variable HOSTS that our app container is going to look for when it tries to connect to the Couchbase container. The run command would look like:

Once the application is started, navigate to http://192.168.99.100:8080/

The following page shows up:

pasted image 0 2

An entry can be edited and saved.

pasted image 0 1

There’s also a neat filtering feature provided by the N1QL query running underneath.

pasted image 0 3

Users can also add a new book and delete an existing record. All the CRUD (Create/Read/Update/Delete) features of this simple application are powered by Couchbase N1QL queries, which we enabled by creating the “BookStoreRepository,” and, in turn, extends the “CouchbasePagingAndSortingRepository.”

 

This post is part of the Couchbase Community Writing Program

Posted by Laura Czajkowski, Developer Community Manager, Couchbase

Laura Czajkowski is the Snr. Developer Community Manager at Couchbase overseeing the community, our incentive programs, Experts and Champions group, meetups, and defining our presence at developer events. She’s also responsible for our monthly developer newsletter and engaging with our community in various forms. Laura has been active in Open Source communities since 2000 and has been involved in various activities, including leading and organising conferences on software testing, documentation, and advocacy. Laura is an Open Source advocate and regular conference speaker who is passionate about getting people – everyone from primary school students to technology professionals – involved in Open Source communities both on IRC and in face-to-face discussions, she is easily found online at @czajkowski on twitter and on freenode.

One Comment

  1. […] run a Couchbase powered, fully functional Spring Boot web application using the Docker toolset. In part one of the series, I demonstrated how to run two Docker containers to run a functional application with […]

Leave a reply