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.

Introduction

Welcome to the part two of the series where I describe how to develop and 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 a presentable UI. The two Docker containers that we were running are:

  1. A Couchbase container with preconfigured settings
  2. An application container talking to the Couchbase container (Run in step 1)

While this method is useful, it’s not fully automated – meaning the automated orchestration is not there. You have to run two different Docker run commands to run the entire setup.

Is there a way to build and run the application container which also triggers running of the Couchbase container? Of course there’s a way.

Enter Docker Compose

Using Docker Compose, you can orchestrate the running of multi-container environments, which is exactly what we need for our use case. We need to run the Couchbase container first, and then the application container should run and talk to the Couchbase container.

Here’s the docker-compose.yml file to achieve this:

Our app “depends_on” the db image which is the Couchbase container. In other words, the Couchbase container runs first and then the app container starts running. There’s one potential issue here: the “depends_on” keyword doesn’t guarantee that the Couchbase container has finished configuring the image and started running. All it ensures is that the container is started first; it doesn’t check whether the container is actually running or ready to be accepting requests by an application. In order to ensure that the Couchbase container is actually running and that all the pre-configuration steps, such as setting up the query, index services, and bucket, is completed, we need to do a check from the application container.

Here’s the Dockerfile of the app container that invokes a script which, in turn, checks whether the bucket “books” has been set up already or not. It goes into a loop till the bucket is set up and then triggers the app container.

https://github.com/ratchakr/bookstoreapp/blob/master/Dockerfile-v1

The script can be seen at https://github.com/ratchakr/bookstoreapp/blob/master/run_app.sh

The script does the following things:

It uses the REST endpoint supported by Couchbase for querying the bucket.

Curl is used to call REST endpoints. Installation of curl is covered in the Dockerfile of the application.

The script parses the JSON response of the REST call by using a tool called jq.

If the bucket is set up, it then runs the app container; otherwise it waits for the bucket to be set up first.

It’s worth mentioning that more checks, such as verifying if the index service and the query service are set up properly or not, can be added in the shell script to make it more robust. One word of caution is to confirm your particular use case and requirement before following the docker-compose approach; there’s not a sure-fire way to determine if the Couchbase db container is fully up and running and ready to serve requests from the client application. Some of the approaches that might work are as follows:

  1. If you have a preconfigured bucket, you can test if the bucket exists
  2. Check if the indexes are in place
  3. If you know the record-count in a bucket (let’s say for a .csv file which has been imported into a bucket at the time of initial data load), you can check if the count matches the number of records in the .csv file). For our use case, the one mentioned above works nicely.

Build and Run

Now that we have our docker-compose file and Dockerfile, we can build the application image by using the simple docker-compose up command.

Here’s the output snippet from the Docker console:

At this point our application is up and running with a single docker-compose orchestration command.

Type 192.168.99.100:8080 into the browser; you should see the following screen:

pasted image 0 12

Docker Compose is a nice way to orchestrate multi-container Docker environments. It has almost similar command chains as “docker” command sets. For instance, to see a list of running containers, you simply type:

docker-compose ps > which would give you

The name of the containers are shown in bold here.

If you need to stop or tear down your orchestrated environment with Docker Compose, you can do that with the docker-compose down command as shown below:

A sample run produces:

Now, if you do a docker-compose ps, it shows that no container is currently running.

You can also use Docker compose for an automated test environment where you fire up your Docker containers, run the tests, then tear down the complete infrastructure – all with Compose. For a detailed overview of Docker compose, please visit the official website.

This post is part of the Couchbase Community Writing Program

Author

Posted by Laura Czajkowski, Developer Community Manager, Couchbase

Laura Czajkowski is the Snr. Developer Community Manager at Couchbase overseeing the community. She’s responsible for our monthly developer newsletter.

Leave a reply