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
Source Code Management
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.
Execute Shell
Let us now specify the build step
with Execute shell
and our commands to run it.
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.
Now specify the build step
with Docker Build and Publish
.
Specify the repository name with dockerhub_username/docker_image_name
. Here it is mentioned as ndrohith09/jenkins-docker
and the tag is set to latest . Now click Save
.
Now try to commit to your chosen github repository. Jenkins will automatically start the job.
Console Output
To view your results, go to the Console Output of the specified job.
Hope this blogs helps you to integrate docker in jenkins.
Top comments (2)
Nice!
Thank you