Terminologies to Know:
Jenkins: A popular open-source automation server for continuous integration and continuous delivery (CI/CD).
Docker: A containerization platform that allows developers to package applications with their dependencies into standardized units called containers.
Jenkins Pipeline: A declarative way to define and execute multi-stage software delivery pipelines within Jenkins.
Docker Agent: A Jenkins agent that runs within a Docker container, providing a controlled and isolated environment for executing pipeline steps.
How I Use Docker Agent In Jenkins Pipeline
At First, I created an EC2 instance and named it as Jenkins.
I connected to the EC2 instance from my terminal using its private IP address. If you wish to connect using the public IPv4 address, you must edit the inbound traffic rules and allow the required ports, such as port 8080."
Now, as a prerequisite for installing the Jenkins, we have to install the Java(JDK) using the following commands:
sudo apt update
sudo apt install openjdk-11-jre
After running the above commands successfully, now we have to install Jenkins by using below commands.
curl -fsSLpkg.jenkins.io/debian/jenkins.io-2023.key| sudo tee
/usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]pkg.jenkins.io/debianbinary/ | sudo tee
/etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install jenkinsAfter we successfully installed the Jenkins on our EC2 Instance(AWS) or Virtual Machine(Azure), Using the IPV4 address of this EC2 instance and by adding the port no(8080) to IP address, we can use Jenkins installed on our Virtual Machine.
After you login to Jenkins, - Run the command to copy the Jenkins Admin Password -
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
- Enter the Administrator password.Click on Install Suggested Plugins, Create First Admin User and Getting started. Finally our Jenkins is ready to use.
Now we have to Install the Docker Plugins available in Jenkins, in order to use the docker agent.
Log in to Jenkins.
Go to Manage Jenkins > Manage Plugins.
In the Available tab, search for "Docker Pipeline".
Select the plugin and click the Install button.
Restart Jenkins after the plugin is installed.
Now, we have to run these commands in order to install docker
sudo apt update
We have to grant access to the Jenkins user and the Ubuntu ( OS in our EC2 instance) user to grant the access to the docker deamon(Single Source of Truth, only accessible for root user) using the below commands.
sudo su - #su - : switch user as root user
usermod -aG docker jenkins #**adds the user 'jenkins' to the 'docker' group
usermod -aG docker ubuntu #**adds the user 'ubuntu' to the 'docker' group
systemctl restart docker #**command-line utility that restarts the docker
Once you are done with the above steps, it is better to restart Jenkins.
http://<ec2-instance-public-ip>:8080/restart
Finally we can make Jenkins pipeline using docker Agent:
Click on Dashboard > New project > Pipeline > Configuration
Under the configuration section click on Pipeline and change the description to "Pipeline script from SCM"
-
Now we will use the files that were in our GitHub repository, so provide the GitHub repository link under the URL section and the folder name.
Choose the branch as Master/Main according to your GitHub account.
Give the proper credentials to access the files in GitHub, if Your repository is Private otherwise leave the fields as it is.
Click on Save.
Now click on Build Now.
Do You want to know exactly what happens in this Build now
\>Fetches the code from GitHub----------checks for docker container------If exists, executes entire pipeline-------if not exists----creates an docker image and executes pipeline.
Our Build is succeeded for one tier application using one docker agent.
Multi Stage Multi Agent
Set up a multi stage Jenkins pipeline where each stage is run on a unique agent. This is a very useful approach when you have multi language application or application that has conflicting dependencies.
Building Jenkins pipeline using Docker agent has main advantage over the Jenkins:
Once the process is initiated using a Docker agent, the container is created, and after the execution of the process is done, the container is terminated. This is unlike Jenkins, where the Jenkins worker node handling the process remains unused until another process arrives with the same configuration.
Credits: I followed this tutorialJenkins zero to heromake this blog.
Pls follow Abhishek Veeramalla for more interesting topics in DevOps, his teaching is simply superb.
If you found this article helpful, consider sharing it with your tech-savvy friends and colleagues. The more people benefit, the better! Don't forget to subscribe to supercharge your DevOps and cloud journey!