0% found this document useful (0 votes)
16 views13 pages

L2 - 7 - Daily Task - 7

The document outlines the steps to create a simple Flask web application, set up a GitHub repository, and configure a Jenkins pipeline for continuous integration and deployment. It includes instructions for installing Flask, writing the application code, creating a Dockerfile, and configuring Jenkins to automate the build and deployment process. Finally, it provides steps to verify the deployment by checking running containers and accessing the Flask app in a web browser.

Uploaded by

Kunal Puri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

L2 - 7 - Daily Task - 7

The document outlines the steps to create a simple Flask web application, set up a GitHub repository, and configure a Jenkins pipeline for continuous integration and deployment. It includes instructions for installing Flask, writing the application code, creating a Dockerfile, and configuring Jenkins to automate the build and deployment process. Finally, it provides steps to verify the deployment by checking running containers and accessing the Flask app in a web browser.

Uploaded by

Kunal Puri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name: Kunal Pradeep Puri

Batch No.:WLBatchB029

L2_7_DAILY TASK - 7

🚀 Step 1: Develop the Flask Web


Application
We'll create a simple Flask application that serves an HTML file.

1.1 Install Flask

First, make sure Python is installed on your system. Then, install Flask:

pip install flask

1.2 Create the Project Structure

Navigate to your working directory and create a new project folder:

mkdir flask-jenkins-docker
cd flask-jenkins-docker

Create the following directory structure:

flask-jenkins-docker/
│── app.py
│── templates/
│ └── index.html
│── requirements.txt
│── Dockerfile
│── Jenkinsfile

1.3 Write app.py (Flask Application)

Create and open app.py:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def home():
return render_template("index.html")

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)

• This script starts a Flask web server and serves the index.html file.

1.4 Write index.html

Create the templates/ directory and inside it, create index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flask Web App</title>
</head>
<body>
<h1>Welcome to My Flask Web App – Kunal Pradeep Purih1>
</body>
</html>
1.5 Define Dependencies in requirements.txt

Create requirements.txt to list required packages:

flask

This ensures Flask gets installed inside the Docker container.


🔗 Step 2: Set Up GitHub Repository
We need to push the project to GitHub for Jenkins to fetch the latest code.

2.1 Initialize Git

Run the following commands inside the project directory:

git init
git add .
git commit -m "Initial commit"

2.2 Create a GitHub Repository

1. Go to GitHub and create a new repository named flask-jenkins-docker.


2. Copy the repository URL and add remote in Git:
3. git remote add origin https://github.com/yourusername/flask-jenkins-
docker.git
4. git push -u origin main
🐳 Step 3: Write a Dockerfile
We'll create a Dockerfile to containerize the Flask application.

3.1 Create Dockerfile

Inside the flask-jenkins-docker directory, create a file named Dockerfile:

# Use an official Python runtime as a base image


FROM python:3.9

# Set the working directory inside the container


WORKDIR /app

# Copy the application files into the container


COPY . .

# Install required Python dependencies


RUN pip install --no-cache-dir -r requirements.txt

# Expose port 5000 for Flask


EXPOSE 5000

# Command to run the Flask application


CMD ["python", "app.py"]

• Base image: Python 3.9


• Copies the Flask application code into the container
• Installs dependencies from requirements.txt
• Runs the Flask app on port 5000

⚙ Step 4: Install and Configure Jenkins


Jenkins will automate the process of building and deploying the application.

4.1 Install Jenkins and Required Plugins

If Jenkins is not installed, install it using Docker:

docker run -d -p 8080:8080 -p 50000:50000 --name jenkins \


-v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

Access Jenkins UI at: http://localhost:8080

Install the following Jenkins plugins:

1. Docker Pipeline Plugin


2. Git Plugin
3. Pipeline Plugin

📝 Step 5: Configure Jenkins Pipeline


Now, let's create a Jenkins pipeline that will:

1. Pull the latest code from GitHub.


2. Build the Docker image.
3. Run the Flask app inside a Docker container.
4. Clean up old Docker images.

5.1 Create a Jenkinsfile

Create a file named Jenkinsfile in your project root directory:

pipeline {
agent any

environment {
DOCKER_IMAGE = "flask-app"
CONTAINER_NAME = "flask-container"
}

stages {
stage('Checkout Code') {
steps {
git branch: 'main', url:
'https://github.com/yourusername/flask-jenkins-docker.git'
}
}

stage('Build Docker Image') {


steps {
script {
sh 'docker build -t ${DOCKER_IMAGE} .'
}
}
}

stage('Run Docker Container') {


steps {
script {
// Stop and remove any existing container
sh 'docker stop ${CONTAINER_NAME} || true'
sh 'docker rm ${CONTAINER_NAME} || true'

// Run the container


sh 'docker run -d -p 5000:5000 --name ${CONTAINER_NAME}
${DOCKER_IMAGE}'
}
}
}

stage('Clean Up') {
steps {
script {
sh 'docker system prune -f'
}
}
}
}
}

• Stage 1: Pull Code from GitHub.


• Stage 2: Build Docker Image for the Flask app.
• Stage 3: Stop old container, Run new container.
• Stage 4: Clean up old Docker images to free up space.

🚀 Step 6: Configure and Run Jenkins


Pipeline
6.1 Create a New Pipeline Job

1. Open Jenkins Dashboard.


2. Click New Item → Select Pipeline → Name it Flask-CI-CD.
3. Select Pipeline script from SCM.
4. Choose Git and enter your repository URL.
5. Set Branch as main.
6. Set Script Path to Jenkinsfile.
7. Click Save.
6.2 Run the Pipeline

1. Open the Flask-CI-CD pipeline.


2. Click Build Now.
3. Jenkins will:
o Clone the repository.
o Build the Docker image.
o Deploy the Flask application in a Docker container.
o
o

✅ Step 7: Verify Deployment


7.1 Check Running Containers

Run:

docker ps

You should see your flask-container running.


7.2 Access the Flask App

Open a browser and visit:

http://localhost:5000

You should see:

Welcome to My Flask Web App – Kunal Pradeep Puri

You might also like