Setting up Jenkins for CI/CD and containerizing the application with Docker
Tags: jenkins, docker
Published: 2022-06-06 09:55:04

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`
```python 
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`
```jsx 
FROM python:3.8-slim-buster 
WORKDIR /app 
COPY . . 
RUN pip install -r requirements.txt
CMD ["python3" , "logistic.py"]
```
`requirements.txt`
```jsx 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxhc004zlg34ze5u1uj9.png)
 

## Source Code Management

![jenkins-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jhn0pgnkacd66d5x1cw.png)
 

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdcebef76dj407hx69hm.png)
 


## Execute Shell

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

![jenkins-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7bvch09s5cbevuj0uml.png)
 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uda38mn08bpwtnxvvkl6.png)
 

Now specify the `build step` with `Docker Build and Publish`.


![jenkins-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/606b5zfadc7abwqu0hkj.png)
 

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` .


![jenkins-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m01g9wjddrxel2pt4xb9.png)
 

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


![jenkins-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4u9byja42o3kokpt3wo.png)
 

## Console Output

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

![jenkins-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nej0fsviu81eq9ssxr9.png)

![jenkins-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bouqvi74zsxnpp13ffh0.png)

![jenkins-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ikobtc5546944fct6zz.png)
 
![jenkins-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d9tbttfbiq9jbhqk9qdu.png)
 

Hope this blogs helps you to integrate docker in jenkins.