DEV Community

Mrinalini Sugosh (Mrina) for IBM Developer

Posted on • Updated on

Deploy a NodeJS app to IBM Cloud Container Registry

Welcome to THINK Days!

A weekly blog series where I discuss and share my journey with IBM Cloud Services. In this THINK tutorial, we will demonstrate:

  • Building an image for a NodeJS app using a Dockerfile
  • Running an image as a container using Docker
  • Deploying an image to IBM Cloud Container Registry

What is the IBM Cloud Container Registry?

  • A place to store and share your Docker based containers in IBM Cloud
  • Highly available, scalable, and encrypted
  • Acts as the perfect segue to deploy images onto IBM Cloud Kubernetes Service (IKS)

Prerequistes:

docker --version
Enter fullscreen mode Exit fullscreen mode
ibmcloud --version
Enter fullscreen mode Exit fullscreen mode

In the case that you don't have the above CLIs installed, I would recommend navigating to the links and following the respective Getting Started guides.

Let's get started!

Building an image for a NodeJS app using a Dockerfile

I have created a simple NodeJS App that generates random facts about cats. We are using the Cat Facts API to generate the facts. Simply, clone this repo and follow along:

git clone https://github.com/mrinasugosh/random-cat-facts-nodejs.git
Enter fullscreen mode Exit fullscreen mode

The following files are needed to run the app in a container:

  • app.js - is the main application, which simply replies with a hello world message.
  • package.json - defines the dependencies of the application.
  • Dockerfile - defines the instructions Docker uses to build the image

1. Setting up Docker File

[1] Create a Docker File.
Add a new file at the root of the project and call it Dockerfile
image

[2] Add the FROM instruction
Any valid Dockerfile must start with a FROM instruction. The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions.

FROM node:14.15.1-alpine
Enter fullscreen mode Exit fullscreen mode

[3] Add the app.js and package.json files as part of the docker build instructions.

COPY app.js .
COPY package.json .
Enter fullscreen mode Exit fullscreen mode

[4] Add instructions to executing the app on Docker. We will first use the RUN command to install all packages in package.json that is needed for the app. Then we will use the EXPOSE command to point to the port that app will be listening on.

RUN npm install &&\
    apk update &&\
    apk upgrade
EXPOSE  3000
Enter fullscreen mode Exit fullscreen mode

[5] Finally, add instruction to run the app when we spin up a Docker image

CMD node app.js
Enter fullscreen mode Exit fullscreen mode

2. Build Docker Image

Run the following command to build the docker image:

docker build . -t random-cat-facts:v1
Enter fullscreen mode Exit fullscreen mode

The output of this command should include a step for each instruction we set in the Dockerfile. Note: each instruction step creates a new layer in the image
image

Verify that image was built. List images to see your image tagged random-cat-facts:v1

docker images
Enter fullscreen mode Exit fullscreen mode

image

Just like that we have built an image for our NodeJS App!

Running an image as a container using Docker

Now that we have successfully built an image, let's run it in a Docker container.

1. Run Docker image as a Docker Container

This step is pretty straightforward, simply write a docker run command pointing the image to the port the app will be listening on:

docker run -p 3000:3000 random-cat-facts:v1
Enter fullscreen mode Exit fullscreen mode

Once we have done this, I have inserted a console.log() statement that this step was successful and the app is ready to running in a Docker Container
image

2. Verify application

Let's test to see if our Docker Image did spin up our app

Open up a second terminal and use the curl command to ping the application.

curl -X POST localhost:3000/test
Enter fullscreen mode Exit fullscreen mode

In this demo app, I have built in a /test endpoint to test our application and as expected pinging our app does display a random cat fact.

image

Deploying an image to IBM Cloud Container Registry

After building and running the image, we are now ready to deploy the image onto an IBM Cloud Container Registry.

[1] Verify the account you are targeting is your IBM Cloud Account and the region is set to us-south

$ ibmcloud login
$ ibmcloud api https://cloud.ibm.com
$ ibmcloud cr region-set us-south 
$ ibmcloud target 
Enter fullscreen mode Exit fullscreen mode

The result should look something like the following where you are pointed to the respective api and region:
image

[2] Log your local Docker daemon into IBM Cloud Container Registry so that you can push to and pull from the registry.

ibmcloud cr login
Enter fullscreen mode Exit fullscreen mode

[3]Create an IBM Cloud Container Registry namespace

ibmcloud cr namespace-add random-cat-facts
Enter fullscreen mode Exit fullscreen mode

[4]Tag your image so that it can be pushed to IBM Cloud Container Registry.

docker tag random-cat-facts:v1 us.icr.io/random-cat-facts/random-cat-facts:1
Enter fullscreen mode Exit fullscreen mode

[5]Push the newly tagged image to IBM Cloud Container Registry.

docker push us.icr.io/random-cat-facts/random-cat-facts:1
Enter fullscreen mode Exit fullscreen mode

[6] Verify that the image was successfully pushed by listing images in Container Registry.

ibmcloud cr images
Enter fullscreen mode Exit fullscreen mode

image

Oila! You have successfully built a Docker Image for a NodeJS app and deployed it onto an IBM Cloud Container Registry.

Thank you for following along this THINK Day's Tutorial and be sure to look out for my next post, where I will continue sharing my Journey with IBM Cloud Services!!!

==== Follow me on Social Media(@mrinasugosh) ====
Dev.to: @mrinasugosh
Github: @mrinasugosh
Twitter: @mrinasugosh
LinkedIn: @mrinasugosh

Top comments (0)