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.
While running the node app locally is fun, it still requires all the precautions of installing the prerequisite softwares such as Couchbase, etc. on the local system. An alternative is to use Docker to containerize the app and run it as a bundled image. The same can be done with the Couchbase container, thus we don’t have to install it locally.
Run Couchbase Custom Image
In order to run Couchbase using Docker, refer to the custom Couchbase image Dockerfile. It makes use of a script which can be found here.
The reason I am not using the base Couchbase image is because I want to preconfigure my Couchbase instance with index and query services and also create a bucket named “photogallery” with a primary index created on it. If I were to use the base Couchbase Docker image, all the steps mentioned above would have to be set up manually. One of the advantages of using Docker is that you can extend from a base image and do all sorts of automation steps as per your needs. This is achieved by the script (configure.sh) mentioned above. Once the image is up and running, you get a Couchbase instance with index and query service already up and running, and a bucket in place with an index placed on it.
The script sets up the Couchbase service, creates the bucket and the index all at one go.
The image can be created using:
1 |
docker build -t chakrar27/couchbase:photogallery <path-to-Dockerfile> |
Once the image is built, run it using:
1 |
docker run -d -p 8091-8094:8091-8094 -p 11210:11210 -e BUCKET_NAME=photogallery --name couchbase chakrar27/couchbase:photogallery |
At this point, you can log into the Couchbase Docker container by going to:
1 |
192.168.99.100:8091 |
192.168.99.100 is the Docker machine IP.
Run the App Image
Once the Couchbase container is up and running, we can proceed to building the app container. Please check out the Dockerfile for the photogallery app for reference.
You can build the app container by:
1 |
docker build -t chakrar27/nodejs:photogalleryapp <path-to-Dockerfile> |
Run the image by:
1 |
docker run -p 3000:3000 -e AWS_ACCESS_KEY=<your access key> -e AWS_SECRET_KEY=<your secret key> -e S3_BUCKET=<s3 bucket> -e HOST=http://192.168.99.100:3000 -e COUCHBASE_HOST=couchbase -e COUCHBASE_BUCKET=photogallery -e PORT=3000 --name node-photo-app chakrar27/nodejs:photogalleryapp |
Now, if you type 192.168.99.100:3000 in the browser, you should see
Test it out:
The car image is saved and tagged. It has 4 “likes.” Let’s see the Couchbase bucket:
As expected, the bucket has the updated information with 4 “likes” and the tags.
So, we have managed to run the photogallery app in a Docker container talking to another container (Couchbase) without installing anything on the local machine. Isn’t it cool?
References
The following posts from Nic Raboy are very helpful to get going with Docker and Node.js.
- Build a photogallery app with auto tagging using AWS Rekognition and Couchbase – Part 1
- Build a photogallery app with auto tagging using AWS Rekognition and Couchbase – Part 2
- Build a photogallery app with auto tagging using AWS Rekognition and Couchbase – Part 3
This post is part of the Couchbase Community Writing Program