DEV Community

Cover image for Setting up Jenkins for CI/CD and containerizing the application with Docker
Rohith ND
Rohith ND

Posted on

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

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)

Enter fullscreen mode Exit fullscreen mode

Dockerfile

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

requirements.txt

scikit-learn==1.0.2
pandas==1.4.2
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

Collapse
 
mikolajbuchwald profile image
Mikołaj Buchwald

Nice!

Collapse
 
ndrohith profile image
Rohith ND

Thank you