Automated Docker using Jenkins

Hey Guys!!

Today we are going to create a very interesting and an automated Docker-Jenkins Pipeline. For this first of all we need to understand What is Docker?, What is Jenkins? and what is the meaning of a pipeline?

What is docker?

What is Jenkins ? How does it work with Docker?

What is the meaning of a Pipeline?

With all these tools and great technologies, we will create a great integration such that as soon as the developer update the code and push it to the GitHub, As the Jenkins is continuously monitoring the GitHub(a SCM system) as it observe any changes it will pull the code or we can say the updated codes, then it will copy those files to the docker environment and then deploy the container in the docker environment, as the docker is connected to the Jenkins.

In this experiment, we will pull the code from two different branches through jenkins and deploy separate web server for both of them . Then we will create a third job that will first validate the Web Server created for dev branch code, and after validating if the web server is properly working then we will merge both the branches that is master as well as dev branch of GitHub, created by Developer.

Architecture of the Integration

We will create this code pipeline step by step :

Step 1 : Create a html file named index.html in the master branch and create some changes in it :

index.html file
Creating changes in the index.html file

Step 2 : Commit changes with automated push in it, through the post-commit command :

committing changes and automated git push

By -m we give the comment to the commit we are doing.

Step 3: Create and switch to the dev branch, with checkout command :

git checkout dev

Creating and switching to dev branch

Step 4 : Create a new.html file in this dev branch and add some changes in it :

new.html file in dev branch
creating changes in new.html file

Step 5 : Commit the changes in the dev branch, with the git commit command :

git commit . -m “dev branch commit”

Committing changes of the dev branch

Step 6 : Check the GitHub repository for both the branches individually :

Files in the master branch
Files in the dev branch

Step 7 : Create a docker image in the RHEL8 , through the Dockerfile that includes httpd installed in it and as soon as we launch the container, the httpd service get started :

  1. Create a workspace named webserver :
separate workspace named webserver

2. Create a Dockerfile, through gedit Dockerfile :

Creating Dockerfile
Dockerfile for creating docker images with httpd service

We have used expose command here in order to access the containers of this image from the outside world through some port number.

3. Build the docker image :

building the docker image
accessing the docker image

Step 8 : Clean up the docker environment, so that if any container is running previously, then it would be removed:

Deleting previously running containers

Step 9 : Now sign in to the Jenkins on port number 8080 (by default port for jenkins):

Sign in to Jenkins

Step 10 : Create a separate view to create the jobs for this task :

Creating view in Jenkins

Step 11 : Start creating jobs :

  1. Create job1 that will pull the code from the git repository from the dev branch :
Pulling code from GitHub — dev branch

Build a trigger to continuous monitoring to the GitHub .

Building trigger for continuous monitoring

Now, the Jenkins will copy this code to the branch directory in the RHEL8, then create a docker container with the image created above and it will link the branch directory with the container’s working Directory:

Creating a docker container through jenkins

Now, save and apply changes.

2. Similarly create job2 , but here pull the code from GitHub from the master branch :

Pulling code from master branch
Copying the code and creating the container from docker image

Now, save and apply changes.

3. Create job3 that will pull the code from dev branch

pulling the code and write the merging code

Create trigger that this project will be build only after the execution of job1

Building Triggers

Now validate or check the web server created from dev branch

Validating the dev branch container

As soon as it validate the web server it will merge the dev branch code to the master branch.

Build a post trigger to job2 , i.e.,

Building post trigger on job2

Hence, we have successfully created the code pipeline for jenkins, And on building or executing job1, the complete pipeline will be automatically executed.

code pipeline in jenkins

But here we see that there is an error in merging the two branches, i.e.,

merging conflict

Now we need to solve this merging conflict manually, by removing either the dev branch code or by removing the master branch code.

Successful pipeline

For Visualization this pipeline, we will use the Build Pipeline , i.e.,

Build Pipeline view

We can also access the web server created by docker , i.e.,

Successfully created web server

Hence, we have successfully created the merging of the two branches of our GitHub Repository. It is a complete automated integration of GitHub — Jenkins — Docker, a complete code Pipeline for creating docker containers through Jenkins.

Thank You !!!

I am a tech enthusiast, researcher and work for integrations. I love to explore and learn about the new technologies and their right concepts from its core.