Automated Docker using Jenkins
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?
Docker is a set of platform as a service products that uses OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files, they can communicate with each other through well-defined channels.
What is Jenkins ? How does it work with Docker?
Jenkins is a automation tool. With this, we can create jobs to build, test and deploy our applications. We can create our pipelines through Jenkins.
What is the meaning of a Pipeline?
Pipeline is a script of steps of all things we need to do. Example: we will create a pipeline to build our code. First step is to checkout the git code, after this we need to test, build, package, create a docker image and finally, deploy your code.
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.
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 :
Step 2 : Commit changes with automated push in it, through the post-commit command :
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
Step 4 : Create a new.html file in this dev branch and add some changes in it :
Step 5 : Commit the changes in the dev branch, with the git commit command :
git commit . -m “dev branch commit”
Step 6 : Check the GitHub repository for both the branches individually :
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 :
- Create a workspace named webserver :
2. Create a Dockerfile, through gedit Dockerfile :
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 :
Step 8 : Clean up the docker environment, so that if any container is running previously, then it would be removed:
Step 9 : Now sign in to the Jenkins on port number 8080 (by default port for jenkins):
Step 10 : Create a separate view to create the jobs for this task :
Step 11 : Start creating jobs :
- Create job1 that will pull the code from the git repository from the dev branch :
Build a trigger to continuous monitoring to the GitHub .
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:
Now, save and apply changes.
2. Similarly create job2 , but here pull the code from GitHub from the master branch :
Now, save and apply changes.
3. Create job3 that will pull the code from dev branch
Create trigger that this project will be build only after the execution of job1
Now validate or check the web server created from dev branch
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.,
Hence, we have successfully created the code pipeline for jenkins, And on building or executing job1, the complete pipeline will be automatically executed.
But here we see that there is an error in merging the two branches, i.e.,
Now we need to solve this merging conflict manually, by removing either the dev branch code or by removing the master branch code.
For Visualization this pipeline, we will use the Build Pipeline , i.e.,
We can also access the web server created by docker , i.e.,
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 !!!