16
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 CLI Verify that Docker CLI is installed
docker --version
- IBM Cloud CLI 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!
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
[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
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
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
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
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
The result should look something like the following where you are pointed to the respective api and region:
[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(@mrinasugosh
) ====
Dev.to: @mrinasugosh
Github: @mrinasugosh
Twitter: @mrinasugosh
LinkedIn: @mrinasugosh
16