Deploy a NodeJS app to IBM Cloud Container Registry
Tags: node, docker, cloud, containers
Published: 2021-07-20 23:07:57

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](https://www.ibm.com/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 CLI](https://docs.docker.com/get-started/)
Verify that Docker CLI is installed
```
docker --version
```
- [IBM Cloud CLI](https://cloud.ibm.com/docs/cli?topic=cli-getting-started)
Verify that IBM Cloud CLI is installed
```
ibmcloud --version
```

> 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](https://catfact.ninja/) to generate the facts. Simply, clone this repo and follow along:
```
git clone https://github.com/mrinasugosh/random-cat-facts-nodejs.git
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhs0tgrfadfv6e3xxlqn.png)
 
[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
```

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

[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
```

[5] Finally, add instruction to run the app when we spin up a Docker image
```
CMD node app.js
```
### 2. Build Docker Image
Run the following command to build the docker image:
```
docker build . -t random-cat-facts:v1
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jyslisa1qll3if0xezs.png)
 
Verify that image was built. List images to see your image tagged `random-cat-facts:v1`
```
docker images
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2myxhomv190yyxoto9gn.png)
 
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
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xu6x0fu6aat761li92b9.png)

### 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
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1n5idj1gm4l97idu74nt.png)
 
## 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 
```
The result should look something like the following where you are pointed to the respective api and region:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v9qvjo8u0zx203sqte1w.png)

[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
```

[3]Create an IBM Cloud Container Registry namespace 
```
ibmcloud cr namespace-add random-cat-facts
```

[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
```

[5]Push the newly tagged image to IBM Cloud Container Registry.
```
docker push us.icr.io/random-cat-facts/random-cat-facts:1
```

[6] Verify that the image was successfully pushed by listing images in Container Registry.
```
ibmcloud cr images
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27l4ne18vmh1mjpke26w.png)
 
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](https://github.com/mrinasugosh)
Twitter: [@mrinasugosh](https://twitter.com/mrinasugosh)
LinkedIn: [@mrinasugosh](https://www.linkedin.com/in/mrinasugosh/)