How to Deploy a Node.js App with Jenkins CI/CD and Docker Compose | How to make AWS EC2 and configure jenkins in it | #Day 24 | #90 Days of DevOps

How to Deploy a Node.js App with Jenkins CI/CD and Docker Compose | How to make AWS EC2 and configure jenkins in it | #Day 24 | #90 Days of DevOps

Pre-Requisites for this project:

  1. AWS EC2 Instance Server

  2. Docker.io - Install in server by- sudo apt docker.io

  3. GitHUB

  4. Jenkins Tool

  5. Install Node.js install by these commands

    sudo apt install nodejs

    sudo apt install npm

    npm install

    node app.js

    Or run by Docker compose file.

To create an Amazon Elastic Compute Cloud (Amazon EC2) instance on AWS, follow these steps:

  1. Log in to the AWS Console:

    • Go to the AWS Management Console (aws.amazon.com).

    • Click on "Sign in to the Console" and enter your AWS account credentials.

  2. Navigate to the EC2 Dashboard:

    • In the AWS Management Console, find the "Services" dropdown in the top-left corner.

    • Under "Compute," select "EC2" to access the EC2 Dashboard.

  3. Launch an Instance:

    • In the EC2 Dashboard, click the "Instances" link in the left navigation pane.
  4. Launch Instance:

    • Click the "Launch Instances" button to start the instance creation process.
  5. Choose an Amazon Machine Image (AMI):

    • Select an AMI from the list. AMIs are pre-configured operating systems and software stacks.(Eg- UBUNTU)

    • You can choose from AWS-provided AMIs or your custom AMIs if available.

  6. Choose an Instance Type:

    • Select an instance type based on your requirements. Instance types vary in terms of CPU, memory, storage, and network capacity.
  7. Configure Instance Details:

    • Configure instance details such as the number of instances to launch, network settings, and IAM roles (if needed).
  8. Add Storage:

    • Specify the amount and type of storage (EBS volumes) you want to attach to the instance.
  9. Add Tags (Optional):

    • Add tags to your instance for easy identification and organization. Tags are key-value pairs.
  10. Review and Launch:

    • Review the configuration of your instance to ensure it's accurate.

    • Click the "Launch" button.

  11. Select Key Pair:

    • If you haven't created an SSH key pair before, you'll be prompted to create one. This key pair is used to authenticate to the instance.
  12. Launch Instances:

    • Review the key pair selection and click the "Launch Instances" button.
  13. View Instances:

    • You'll see a confirmation message indicating that your instances are launching.

    • Click the "View Instances" button to go back to the Instances Dashboard.

  14. Wait for Instance to Start:

    • It may take a few minutes for the instance to start. You can monitor the instance's status in the Instances Dashboard.
  15. Access Your Instance:

    • Once the instance is running, you can connect to it using SSH (for Linux) or Remote Desktop (for Windows) using the key pair you specified during setup.
  16. Terminate Instance (When Done):

    • When you're finished with your instance, it's important to terminate it to stop incurring charges.

    • Select the instance in the dashboard, go to the "Actions" menu, and choose "Instance Settings" ➡️ "Terminate Instances."

Steps to install the jenkins on AWS EC2 instance:

  1. This is the Debian package repository of Jenkins to automate installation and upgrade. To use this repository, first add the key to your system.

    Copy this code and paste on terminal

       curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
         /usr/share/keyrings/jenkins-keyring.asc > /dev/null
    
  2. Then add a Jenkins apt repository entry:

     echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
         https://pkg.jenkins.io/debian binary/ | sudo tee \
         /etc/apt/sources.list.d/jenkins.list > /dev/null
    
  3. Update your local package index, then finally install Jenkins:

    copy these commands one by one in your terminal.

      sudo apt-get update
      sudo apt-get install fontconfig openjdk-17-jre
      sudo apt-get install jenkins
    

After installation kindly apply the security port 8080 as k=jenkins runs on this port from AWS EC2 instances Security Tab and Add a new rule in that adds port 8080 in custom TCP protocol select MyIp only and save the changes.

Now copy your IP address and paste it into the web browser (e.g.: 13.233.65.250:8080) like this and your Jenkins tool is ready to action.

Now lets see how to make CI/CD pipeline through Jenkins:

What is the Node.js Todo App?

The Node.js todo app that I will use for this tutorial is a simple todo app that allows users to create, read, update, and delete tasks. The app uses Express as the web framework and EJS as the templating engine.

The source code for the app is available on GitHub: https://github.com/avanishnit08/node-todo-cicd.git

The app has two main files:

  • app.js: The main entry point of the app that sets up the Express server, the routes, and the views.

  • The app also has a package.json file that defines the dependencies and scripts for the app.

How to Connect Jenkins Job and GitHub Repository via GitHub Integration?

To deploy the app using Jenkins, we need to create a Jenkins job that connects to the GitHub repository where the app code is stored. We also need to add a GitHub webhook that triggers the Jenkins job whenever there is a push event on the repository.

To do this, follow these steps:

  1. Go to your Jenkins dashboard and click on New Item.

  2. Enter a name for your job (e.g., Node JS Todo App) and select Freestyle project. Then click OK.

  3. On the configuration page, you can add some descriptions for your job if you want, like this and add the GitHub URL also.

  4. Under Source Code Management, select Git and enter the repository URL: https://github.com/avanishnit08/node-todo-cicd.git You can leave the other fields as default.

  5. Under Build Triggers, select the GitHub hook trigger for GITScm polling. This will enable the job to be triggered by GitHub webhooks.

  6. To use this concept install a plugin in Jenkins:

  7. Now, go to your GitHub repository and click on Settings. Then click on Webhooks and then on Add Webhook.

  8. Fill in the form as follows:

    • Payload URL: Enter your Jenkins URL followed by /github-webhook/. For example, http://example.com/github-webhook/.

    • Content type: Select application/json.

    • Secret: Leave it blank.

    • SSL verification: Select Enable SSL verification if your Jenkins URL uses HTTPS, otherwise select Disable SSL verification.

    • Which events would you like to trigger this webhook?: Select Just the push event.

    • Active: Check this box.

Click on Add webhook to create the webhook.

How to Run the App Using Docker Compose?

To run the app using Docker Compose, we need to create two files:

  • Dockerfile: A file that defines how to build a Docker image for the Node.js app.

  • docker-compose.yml: A file that defines how to run multiple containers for the Node.js app using Docker Compose.

Let’s create these files in our GitHub repository.

Creating the Dockerfile

The Dockerfile for the Node.js app will look something like this:

COPY

COPY

# Use node:14-alpine as base image
FROM node:14-alpine

# Set /app as working directory
WORKDIR /app

# Copy package.json and package-lock.json files to /app
COPY package*.json /app/

# Install dependencies
RUN npm install

# Copy app source code to /app
COPY . /app/

# Expose port 8000
EXPOSE 8000

# Run app.js as entrypoint
CMD ["node", "app.js"]

This Dockerfile does the following:

  • It uses node:14-alpine as the base image, which is a lightweight version of Node.js image based on Alpine Linux.

  • It sets /app as the working directory, where the app code will be copied and executed.

  • It copies the package.json and package-lock.json files to /app and installs the dependencies using npm install.

  • It copies the app source code to /app.

  • It exposes port 8000, which is the port that the app listens on.

  • It runs node app.js as the entry point, which starts the app.

Creating the docker-compose.yml File

The docker-compose.yml file for the app and the database will look something like this:

version: '3'
services:
  web:
    image: "trainwithshubham/node-app-test-new:latest"
    ports:
      - "8000:8000"

This docker-compose.yml file does the following:

The web service pulls the image from the Dockerhub in the current directory and maps port 8000 of the container to port 8000 of the host.

How to Build and Run the App with Jenkins?

Now that we have created the Dockerfile and the docker-compose.yml file, we can use them to build and run the app with Jenkins.

To do this, follow these steps:

  1. Go back to your Jenkins dashboard and click on your job name (e.g., Node Todo App). Then click on Configure.

  2. Under Build, click on the Add build step and select Execute shell (or Execute Windows batch command if you are using Windows). This will allow you to run any shell or batch command as part of your job.

  3. In the text area that appears, enter the command docker-compose up -d --build to build and run the app using Docker Compose. The -d flag runs the containers in detached mode and the --build flag forces a rebuild of the images if there are any changes.

  4. Click Save to save your job.

Now, whenever you push a new commit to the repository, a new build will be initiated automatically.

You will see a new build number appear under Build History with a blue ball indicating that the build is in progress. You can click on it to see the details of the build.

Click on Console Output to see the logs of the build.

You can see that the images and the containers are created and started successfully.

You can also verify that the app is running by going to your browser and typing http://localhost:8000. You should see a to-do app where you can add, edit, and delete tasks.

That's all for today kindly do share this blog post if you find this helpful for the DevOps enthusiasts.

Thanks for Reading.