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)
- Docker CLI Verify that Docker CLI is installed
- IBM Cloud CLI Verify that IBM Cloud CLI is installed
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:
- 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
 Add the
Dockerfile must start with a
FROM instruction. The
FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions.
 Add the
package.json files as part of the docker build instructions.
COPY app.js . COPY package.json .
 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
 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
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.
 Verify the account you are targeting is your IBM Cloud Account and the region is set to
$ ibmcloud login $ ibmcloud api https://cloud.ibm.com $ ibmcloud cr region-set us-south $ ibmcloud target
 Log your local Docker daemon into IBM Cloud Container Registry so that you can push to and pull from the registry.
ibmcloud cr login
Create an IBM Cloud Container Registry namespace
ibmcloud cr namespace-add random-cat-facts
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
Push the newly tagged image to IBM Cloud Container Registry.
docker push us.icr.io/random-cat-facts/random-cat-facts:1
 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!!!