IBM Cloud Global

 View Only

Deploying a Simple Hello World Node.js App with IBM Code Engine and Container Registry

By Gunasekaran Venkatesan posted Mon February 26, 2024 06:38 AM

  

In today's fast-paced world of application development and deployment, streamlined processes are essential. IBM offers powerful tools like Code Engine and Container Registry to simplify and enhance the deployment of your applications. In this guide, we'll walk through the process of deploying a simple Hello World Node.js application using IBM Code Engine, and then storing the Docker image in IBM Container Registry.

ce

Prerequisites:

  1. An IBM Cloud account.
  2. Install the IBM Cloud CLI.

  3. Install the Docker CLI.

  4. Basic knowledge of Node.js and Docker.
Step 1: Set Up Your Node.js Application

First, let's create a simple Node.js application. Create a new directory for your project and navigate into it. Then, create a file named index.js with the following content.

const express = require("express");
const app = express();
const PORT = process.env.PORT || 8080;

app.get("/", (req, res) => {
    res.send("Hello World!");
});

app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});


Next, create a package.json file with the following minimal configuration:

{
  "name": "hello-world-app",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

Install Express by running npm install express in your project directory.

Step 2: Dockerize Your Application

Now, let's create a Dockerfile to package our Node.js application into a Docker image. Create a file named Dockerfile in your project directory with the following content:

# Use the official Node.js image as a base
FROM node:latest

# Create app directory
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install app dependencies
RUN npm install

# Bundle app source
COPY . .

# Expose port 8080
EXPOSE 8080

# Define environment variable
ENV PORT=8080

# Command to run the app
CMD ["npm", "start"]

Step 3: Build the Docker Image

Login to IBM Cloud: 

ibmcloud login -a https://cloud.ibm.com

Install the Container Registry plug-in.

ibmcloud plugin install container-registry -r 'IBM Cloud'

Build the Docker image:

docker build -t <image-name>:<tag-name> .

Ensure that the Docker image you've created is actively running on your local machine.

docker run -p port <imagename>:<tagname>
Step 3: Push the Docker Image to IBM Container Registry

Firstly, Install the Container Registry plug-in

ibmcloud plugin install container-registry -r 'IBM Cloud'

 Log your local Docker daemon into the IBM Cloud Container Registry.

ibmcloud cr login

 Ensure that you're targeting the correct IBM Cloud Container Registry region.

ibmcloud cr region-set us-south

Choose a name for your first namespace, and create that namespace.

ibmcloud cr namespace-add <my_namespace>

 Run the below to tag a Docker image with a specific name and optionally a tag.

docker tag image-name us.icr.io/<my_namespace>/<my_repository>:<my_tag>
  • docker tag: This is the Docker command used to tag an image.

  • helloworld: This is the name of the existing Docker image you want to tag. 

  • us.icr.io/<my_namespace>/<my_repository>:<my_tag>: This is the new name and tag you're assigning to the image. It follows the format <registry>/<namespace>/<repository>:<tag>.

  • us.icr.io: This is the domain of the container registry where you want to store the tagged image. In this case, it's an IBM Cloud Container Registry.

  • <my_namespace>: This is the namespace within the container registry where you want to store the image. Namespaces help organize images within a registry.

  • <my_repository>: This is the name of the repository where you want to store the image. Repositories are used to group related images together.

  • <my_tag>: This is the tag you want to assign to the image. Tags are used to differentiate different versions or variants of an image.

Next, Once tagged the image. you need push the image to IBM cloud container registry.

docker push us.icr.io/<my_namespace>/<my_repository>:<my_tag>

Once pushed, you can verify it from your IBM cloud account - UI

Step 4: Deploy the Application to IBM Code Engine


Create a Code Engine project:

ibmcloud ce project create --name <project-name>

Run the below command to set the project

ibmcloud ce project select -n <project>

Note: Prior to deploying the application, ensure that you have access to the Container Registry from IBM Code Engine. For detailed instructions, please refer to the documentation provided here.  And make sure to create the secret registry here 

Deploy the application: 

ibmcloud ce application create --name <app-name> --image <region>.icr.io/<namespace>/<image-name> --registry-secret <registry-secret-name>

Step 5: Access the Deployed Application
Once the deployment is complete, access your application using the provided URL:

You should see "Hello, World!" displayed in your browser.

To check the application status, 
ibmcloud ce application get --name <app-name>


Congratulations! You've completed the deployment process of your Node.js application using IBM Code Engine and IBM Container Registry. You can now access your application and share it with others. Enjoy exploring further capabilities of IBM Cloud for your development needs!

Conclusion:

In this tutorial, you learned how to deploy a simple Node.js application using IBM Code Engine and Container Registry. This approach provides a straightforward method for deploying containerized applications to a managed Kubernetes environment, enabling developers to focus on building and deploying their applications without managing the underlying infrastructure.

Deploying app workloads from images in IBM Cloud Container Registry
https://cloud.ibm.com/docs/codeengine?topic=codeengine-deploy-app-crimage&interface=ui

IBM Cloud Code Engine CLI
https://cloud.ibm.com/docs/codeengine?topic=codeengine-cli#cli-job

0 comments
31 views

Permalink