39
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:
What is the IBM Cloud Container Registry?
Prerequistes:
docker --version
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!
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
The following files are needed to run the app in a container:
[2] Add the
Any valid
FROM
instructionAny 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
Run the following command to build the docker image:
docker build . -t random-cat-facts:v1
Verify that image was built. List images to see your image tagged
random-cat-facts:v1
docker images

Just like that we have built an image for our NodeJS App!
Now that we have successfully built an image, let's run it in 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
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.
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
[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

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(
Dev.to: @mrinasugosh
Github: @mrinasugosh
Twitter: @mrinasugosh
LinkedIn: @mrinasugosh
@mrinasugosh
) ====Dev.to: @mrinasugosh
Github: @mrinasugosh
Twitter: @mrinasugosh
LinkedIn: @mrinasugosh
39