Not too long ago I wrote about containerizing a Node.js RESTful API and Couchbase Server to demonstrate how easy it is to deploy web applications in a quick and reliable fashion.  In that guide we created a simple API, built a Docker image from it, deployed it as a container, and deployed Couchbase as a container.  However, I understand that not everyone is familiar with Node.js.

Here we’re going to build a simple Java RESTful API using Spring Boot, create a Docker image from it, and deploy it as a container with Couchbase.  This will create a familiar environment for Java developers.

This tutorial requires that you have a Docker installed and configured on your machine.  With Docker we’ll be creating custom Docker images and deploying them as containers.

Create a Custom Docker Image for Couchbase Server

Let’s start with creating a custom Docker image for Couchbase Server.  While an official Couchbase image exists, it isn’t automatically provisioned when deployed.  Our custom image will automatically provision itself upon deployment as a container.

Somewhere on your computer create a directory with a Dockerfile file and configure.sh file in it.  The Dockerfile file will be the blueprint for our image and the configure.sh file will be the provisioning script that is run when the container is deployed.

Open the configure.sh file and include the following:

Couchbase can be configured through HTTP after being deployed.  Our configuration script will specify instance resources, administrative credentials, a Bucket, and a primary index.  You’ll notice that a variety of variables are used such as $COUCHBASE_ADMINISTRATIVE_USERNAME and $COUCHBASE_BUCKET.  These can be passed in at runtime preventing us from having to hard-code sensitive information.

More information on provisioning a Couchbase container via HTTP can be seen in a previous article that I wrote on the topic.

With the provisioning script complete, we have to finish the Dockerfile file.  Open it and include the following:

The custom Docker image will use the official Docker image as the base, copy our provisioning script during the build process, and execute it at runtime.

To build the custom image for Couchbase, execute the following:

In the above command couchbase-custom is the image name and it is built from the path that contains the Dockerfile file.

Developing a Spring Boot RESTful API with Java

Before we can containerize our Java application we have to build it.  Because we are using Spring Boot, we need to download a starter project.  This can easily be done from the Spring Initializr website.

Spring Boot Initializr

For this project I’m using com.couchbase as my group and docker as my artifact.  I also prefer Gradle, so I’m using that instead of Maven.

Extract the downloaded project, and open the project’s src/main/resources/application.properties file.  In this file include the following:

In the above we are assuming our host instance is called couchbase and it has a passwordless Bucket called default.  If you were testing locally, the host would probably be localhost instead.  In any case, all these properties are going to be defined at container runtime through environment variables.

Now open the project’s src/main/java/com/couchbase/DockerApplication.java file.  Here we’re going to load our properties and define our endpoints.  Open this file and include the following Java code:

Not too much is happening in the above.  Much of it is boilerplate code and import statements.  Because the goal of this article isn’t in regards to using Java with Couchbase, I won’t explain each part of the code.  Instead know that it has three endpoints, one of which will get all documents in the Bucket and one of which will save new documents to Couchbase.

If you’re using Gradle like I am, you need to change the build.gradle file.  It needs to have a task created and dependencies added.  Your build.gradle file should look something like this:

To build the application, execute the following:

Now you’ll have a JAR file to be used in our Docker image.

Build a Custom Docker Image for the Spring Boot Application

Building a custom image will require that we have a Dockerfile file in place.  At the base of your Java project, add a Dockerfile file and include the following:

In the above we’re using the official OpenJDK image as our base and we’re copying our JAR into the image at build time.  At deployment, the JAR is executed.

To build this image, execute the following:

The above command should look familiar.  We’re creating a spring-boot-custom image using the blueprint found in the directory of our Dockerfile file.

For more information on creating custom Docker images, you can visit a previous article I wrote called, Build a Custom Docker Image for Your Containerized Web Application.

Deploying the Couchbase and the Spring Boot Images as Containers

There are a few options when it comes to deploying our images.  We can use a Compose file or deploy them as vanilla containers.  I find Compose to be a cleaner approach so we’ll use that.

Somewhere on your computer create a docker-compose.yml file and include the following:

In the above file we are defining the custom images that we built and we are doing port mapping to the host machine.  What is particularly interesting is the environment options.  These match the variables that we have in our application.properties and configure.sh files.

To deploy our containers with Compose, execute the following:

Something to note about the above commands.  Couchbase does not deploy instantly.  You’ll need to wait until it is completely launched before you deploy the Java application.  After both applications are launched, check them out by navigating to the Java application in your web browser.

Conclusion

You just saw how to create custom Docker images for a Spring Boot application and Couchbase Server.  After deploying each as containers they will be able to communicate to each other which is incredible convenient for maintenance.

If you’re interested in seeing this done with Node.js, check out the previous article I wrote on the topic.  If you’re interested in learning more about the Java SDK for Couchbase, check out the Couchbase Developer Portal.

Author

Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

One Comment

  1. […] you’re interested, you can check out Use Docker to Deploy a Containerized Java with Couchbase Web Application and Deploy a Node.js with Couchbase Web Application as Docker Containers, depending on your […]

Leave a reply