Skip to main content

Command Palette

Search for a command to run...

Setting up Jenkins for CI/CD and containerizing the application with Docker

Published
3 min read
Setting up Jenkins for CI/CD and containerizing the application with Docker
R

To pursue a highly rewarding career, seeking for a job in challenging and healthy work environment where I can utilize my skills and knowledge efficiently for organizational growth. Seeking a beginners role to explore and enhance my technical knowledge gained at University in the last few years. I am looking for a responsible job with an opportunity for professional challenges and self development that enables me as a fresh graduate to grow while fulfilling organizational goals

In this blog, I'll show you how to set up a Jenkins CI/CD pipeline and containerize an application with Docker. Let us take a logistic regression python code and push it to github. Jenkins will pull the code, run it, containerize the Python application, and publish the docker image to Docker Hub for each commit to your github.

Code

Before moving on to jenkins let's create a simple logistic.py which consists of logistic regression python code. In the same folder let's create our Dockerfile and requirements.txt .

logistic.py

import pandas
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
import joblib

url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv"
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
dataframe = pandas.read_csv(url, names=names)
array = dataframe.values
X = array[:,0:8]
Y = array[:,8]
test_size = 0.33
seed = 7

X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=test_size, random_state=seed)

# Fit the model on training set
print("Creating Logistic Regression Model ..")
model = LogisticRegression()
print("Training Model ..")
model.fit(X_train, Y_train)
# save the model to disk
print("Saving Model ..")
filename = 'logistic.sav'
joblib.dump(model, filename)
print("Model Saved ..")

# load the model from disk
print("Loading Saved Model ..")
loaded_model = joblib.load(filename)
result = loaded_model.score(X_test, Y_test)
print("==================================================")
print("\n Score = " , result)

Dockerfile

FROM python:3.8-slim-buster 
WORKDIR /app 
COPY . . 
RUN pip install -r requirements.txt
CMD ["python3" , "logistic.py"]

requirements.txt

scikit-learn==1.0.2
pandas==1.4.2

Let's now make a freestyle project called jenkins-docker and add our description to it. Once we add our description let's add our git repo url in source code management and specify the git branch as master.

General

jenkins-docker

Source Code Management

jenkins-docker

Build Triggers

Poll SCM polls the SCM on a regular basis to see if commits have been made and builds the project if new commits have been pushed since the last build.

jenkins-docker

Execute Shell

Let us now specify the build step with Execute shell and our commands to run it.

jenkins-docker

Thus, we're checking to see if the Python code is executing properly. If it executes properly, we will create a docker image and test it again to see if the docker image is running successfully.

Docker Build and Publish

Now to push your docker image to docker hub Jenkins has a plugin called CloudBees Docker Build and Publish . Let's install it to perform docker operations.

jenkins-docker

Now specify the build step with Docker Build and Publish.

jenkins-docker

Specify the repository name with dockerhub_username/docker_image_name. Here it is mentioned as ndrohith09/jenkins-dockerand the tag is set to latest . Now click Save .

jenkins-docker

Now try to commit to your chosen github repository. Jenkins will automatically start the job.

jenkins-docker

Console Output

To view your results, go to the Console Output of the specified job.

jenkins-docker

jenkins-docker

jenkins-docker

jenkins-docker

Hope this blogs helps you to integrate docker in jenkins.

DevOps

Part 4 of 7

Software development (Dev) and IT operations (Ops) are combined in DevOps. Its goal is to abbreviate the systems development life cycle and provide high-quality software delivery on a continual basis.

Up next

Introduction to Jenkins Pipeline

Jenkins Pipeline is a collection of plugins that aid in the implementation and integration of continuous delivery pipelines into Jenkins.Pipeline offers an extendable collection of tools for modelling basic to complex delivery pipelines "as code" usi...

More from this blog

ND Rohith's Blog

36 posts