Automate MEAN Stack Deployment with Jenkins CI/CD

This project automates the CI/CD pipeline by building and pushing updated Docker images from GitHub to Docker Hub and deploying them on a VM with automatic container restarts and reverse proxy configured.

šŸ“ Repository Structure

The project follows a modular structure to separate the frontend, backend, and configuration files:

MEAN-CRUD-APP/
ā”œā”€ā”€ backend/
│   ā”œā”€ā”€ Dockerfile
│   └── (backend code)
ā”œā”€ā”€ frontend/
│   ā”œā”€ā”€ Dockerfile
│   └── (frontend code)
ā”œā”€ā”€ docker-compose.yml                 # Local development
ā”œā”€ā”€ docker-compose.server.yml          # Server/AWS deployment
ā”œā”€ā”€ Jenkinsfile.docker-push            # CI: Build and push images to DockerHub
ā”œā”€ā”€ Jenkinsfile.deploy-app             # CD: Deploy application on server 
ā”œā”€ā”€ nginx-default.conf                 # Nginx reverse proxy config file
└── README.md

ā˜ļø Steps for Deployment on the AWS Server

Here is the step-by-step process for setting up the automated deployment environment:

1. Provision EC2 Instance

  • Launch an EC2 instance (e.g., in the default VPC).

  • Crucial Step: Configure the security group to allow inbound traffic on the following ports:

    • 22 (SSH)

    • 80 (HTTP)

    • 443 (HTTPS)

    • 8080 (Backend Service)

    • 8081 (Application Access)

    • 9090 (Jenkins)

2. Install Required Software

  • Install Jenkins, Docker, and Docker Compose on the EC2 server.

  • Verify that Jenkins is accessible on port 9090.

3. Configure Jenkins

  • Install necessary plugins in Jenkins.

  • Configure GitHub credentials and Docker Hub credentials.

  • Set up GitHub webhook integration to trigger pipelines automatically upon code changes.

4. Set Up Docker and Docker Compose

  • Prepare the server environment to use Docker and Docker Compose for managing the application's containers: Frontend, Backend, and MongoDB.

5. MongoDB with Persistent Storage

  • Run the MongoDB database within a Docker container.

  • Utilize a persistent volume to ensure that application data is never lost during container updates or redeployment.

6. Implement CI Pipeline (Jenkinsfile.docker-push)

  • The CI pipeline is responsible for detection and build:

    • Detects changes pushed to the GitHub repository (frontend or backend).

    • Builds the respective container image (frontend or backend).

    • Pushes the new image to Docker Hub.

    • Removes the local image to conserve server space.

7. Implement CD Pipeline (Jenkinsfile.deploy-app)

  • The CD pipeline handles the deployment:

    • Monitors Docker Hub for new image pushes.

    • Pulls the newly pushed image.

    • Removes any existing running containers.

    • Deploys the updated version automatically using docker-compose.server.yml.

8. Configure Nginx for Reverse Proxy

  • Use the nginx-default.conf file to configure Nginx as a reverse proxy.

  • This setup allows the final application to be accessible directly via the server's IP on ports 80 (HTTP) and 443 (HTTPS), hiding the application's internal ports.

9. Access the Application

  • The final application is accessible via the EC2 public IP or domain name on ports 80/443.

  • Jenkins remains accessible on port 9090 for CI/CD management.


šŸ’» Local Testing Steps (Quick Start)

Before deploying to the server, you can quickly test the containerized setup locally:

1. Clone the Repository

Bash

git clone <https://github.com/iabhishekpratap/mean-crud-app>
cd mean-crud-app

2. Build and Start the Application

Use docker-compose.yml (for local development) to build and start all services in detached mode:

Bash

docker-compose up --build -d

3. Verify Container Status

Check if the frontend, backend, and MongoDB containers are running:

Bash

docker-compose ps

4. Access the Application

Open your web browser and navigate to:

http://localhost:8081

5. Test Functionality

Verify the basic CRUD (Create, Read, Update, Delete) operations through the application's UI.

Cleanup

To stop and remove all containers, networks, and persistent volumes:

Bash

docker-compose down -v

šŸ› ļø Support

For any issues or further documentation, refer to:

  • Docker and Docker Compose documentation

  • Jenkins documentation

  • MongoDB documentation

Updated on