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.
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> .