How to Create Jenkins Agent Container using Docker

How to Create Jenkins Agent Container using Docker. In this post, we show you what is Jenkins, Jenkins agent, its advantages then explain how to create Jenkins agent container using Docker.

What is Jenkins?

Image Source: Medium

First of all, Jenkins is an open source automation server that helps developers and system administrators to automate the process of building, testing and deploying applications. This continuous integration software runs automated tasks on a set schedule. It is also used to build and test software projects. Continuous integration (CI) is the practice of merging code into a shared repository several times a day. It helps ensure that code changes do not break the project or introduce new bugs as they are added.

In addition, Jenkins is a free software written in Java that is also compatible with various operating systems, including Windows and macOS. Another best part about Jenkins is it supports integration with several deployment technologies and offers 1500+ plugins for testing, deploying, and monitoring software projects.

Moreover, Jenkins provides an easy way for developers to build their projects by providing tools like Maven, Gradle, Ant, and so on. Also, it provides various ways of running unit tests, such as JUnit or TestNG with just one click or command in Jenkinsfile. Developers also configure their own test strategies.

All in all, Jenkins offers a wide variety of plugins that can be used to extend its functionality. To be precise, it provides access to around 1500+ plugins, which are extensions that increase the functionality of Jenkins and allow it to do more than just build projects. These plugins are written in Java and typically have a JAR file as their distribution format.

What is Jenkins agent?

Image Source: Medium

A Jenkins Agent is a software component that runs on the computer where Jenkins is installed. The agent connects to the Jenkins master and performs tasks as instructed by the master.

After all, Jenkins agents are installed on each machine that needs to be monitored by Jenkins. These agents are also referred to as the Jenkins Server and are used to run build tests that are triggered on that machine or scheduled by the controller.

Benefits of Jenkins

As a result, Jenkins is a continuous integration tool that helps developers to automate the process of building, testing, and deploying their code. Here are some of the main benefits of Jenkins:

  • Flexible and Easy to use: Another benefit of Jenkins is it is versatile, user friendly, and compatible with projects of all sizes. The easy to use DevOps tool has an as open architecture and supports 1500+ plugins. Also, it supports integration with various deployment technologies. Further, with its vibrant and active community, users may also discuss their doubts and learn about the technology. 
  • Open source Automation Software: Being an open source software, it is easy to build, test, and deploy the latest versions of any application. Also, this feature enables users to access new versions with improved updates, features, and enhancements. 
  • Jenkins is a Java based application that is free to download and offers a web user interface, which makes it easy for non technical users to configure. The open source automation feature supported by the tool increases the potential for improvisation and contribution to Jenkins initiatives.
  • API support: Jenkins has an in built feature referred to as an API suite that helps in retrieving reliable data and performing validation checks on each web application.
  • Easy to Install and Configure DevOps Tool: Jenkins is an easy to install open source automation server that is used to automate the process of deploying applications. It is a self contained Java based program with in built features and multiple plugins. Then, Jenkins is easy to configure and compatible with several operating systems.
  • Supports Integration and Work Distribution: Jenkins allows integration with Git, CVS, Subversion, Docker, and other version control tools and deployment technologies. Also, it aids in easy work distribution across several platforms.

Now the main part of this article is to read about How to Create Jenkins Agent Container using Docker.

How to Create Jenkins Agent Container using Docker

Image Source: Coachdevops

Prerequisites

  • Two servers running Ubuntu 20.04 one for Jenkins master and one for Jenkins agent.
  • A root password is setup on both servers.

Install Jenkins on Master Server

In this section, we will install Jenkins on the master server.

Install Java JDK

First, install the Java JDK using the following command:

				
					apt install openjdk-11-jdk -y
				
			

Once the java is installed, you can verify the Java version using the following command:

				
					java --version
				
			

Install Jenkins

By default Jenkins is not included in the Ubuntu default repository. So you will need to add the Jenkins repository to your server.

First, download and add the Jenkins GPG key using the following command:

				
					wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | apt-key add -
				
			

Next, add the Jenkins repository using the following command:

				
					sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
				
			

Then, update the repository and install the jenkins using the following command:

				
					apt-get update -y
apt-get install jenkins -y
				
			

Once the Jenkins is installed on your server, you can verify the Jenkins active status using the following command:

				
					systemctl status jenkins
				
			

You should see the following output:

				
					● jenkins.service - Jenkins Continuous Integration Server
     Loaded: loaded (/lib/systemd/system/jenkins.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-11-26 08:50:46 UTC; 23s ago
   Main PID: 46209 (java)
      Tasks: 44 (limit: 4612)
     Memory: 363.2M
     CGroup: /system.slice/jenkins.service
             └─46209 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080

Nov 26 08:50:26 jenkins-server jenkins[46209]: This may also be found at: /var/lib/jenkins/secrets/initialAdminPassword
Nov 26 08:50:26 jenkins-server jenkins[46209]: *************************************************************
Nov 26 08:50:26 jenkins-server jenkins[46209]: *************************************************************
Nov 26 08:50:26 jenkins-server jenkins[46209]: *************************************************************
Nov 26 08:50:46 jenkins-server jenkins[46209]: 2022-11-26 08:50:46.142+0000 [id=29]        INFO        jenkins.InitReactorRunner$1#onAttained>
Nov 26 08:50:46 jenkins-server jenkins[46209]: 2022-11-26 08:50:46.164+0000 [id=22]        INFO        hudson.lifecycle.Lifecycle#onReady: Je>
Nov 26 08:50:46 jenkins-server systemd[1]: Started Jenkins Continuous Integration Server.
Nov 26 08:50:47 jenkins-server jenkins[46209]: 2022-11-26 08:50:47.194+0000 [id=44]        INFO        h.m.DownloadService$Downloadable#load:>
Nov 26 08:50:47 jenkins-server jenkins[46209]: 2022-11-26 08:50:47.196+0000 [id=44]        INFO        hudson.util.Retrier#start: Performed t>
Nov 26 08:50:47 jenkins-server jenkins[46209]: 2022-11-26 08:50:47.198+0000 [id=44]        INFO  

				
			

Perform Jenkins Web-based Installation

At this point, Jenkins is installed and listens on port 8080. You can now access the Jenkins web installation wizard using the URL http://your-server-ip:8080. You should see the Jenkins initial admin password screen:

Open your terminal and run the following command to retrieve the Jenkins admin password.

				
					cat /var/lib/jenkins/secrets/initialAdminPassword
				
			

You should see the following output:

				
					36365e2b29174497b54e4ccebf890e9c
				
			

Copy the above password and paste it on the Jenkins screen. Then, click on the Continue button. You should see the following screen:

Click on Install Suggested Plugins. You should see the Jenkins instance configuration screen:

Define your Jenkins IP and click on the Save and Finish button. You should see the Jenkins dashboard on the following screen:

Install and Configure Docker on Agent Server

In this section, we will show you how to install Docker and enable remote API on Jenkins Agent server.

Install Docker and Docker Compose

First, install all required dependencies using the following command:

				
					apt install apt-transport-https ca-certificates curl software-properties-common -y
				
			

Next, add the Docker repository using the following command:

				
					curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
				
			

Then, install Docker and Docker Compose using the following command:

				
					apt install docker-ce docker-compose -y
				
			

Once the Docker is installed, verify the Docker version using the following command:

				
					docker --version
				
			

You should see the following output:

				
					Docker version 20.10.21, build baeda1f
				
			

Configure a Docker Host With Remote API

Primarily, Jenkins master connects to the docker host using REST APIs. So you will need to enable the remote API for the docker host. You can enable it by editing Docker service file:

				
					nano /lib/systemd/system/docker.service
				
			

Find the following line:

				
					ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
				
			

Replaced it with the following line:

				
					ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock
				
			

Save and close the file then reload the systemd daemon to apply the changes:

				
					systemctl daemon-reload
				
			

Next, restart the Docker service using the following command:

				
					service docker restart
				
			

You can now validate the API using the following command:

				
					curl http://localhost:4243/version
				
			

You will get the following output:

				
					{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"20.10.21","Details":{"ApiVersion":"1.41","Arch":"amd64","BuildTime":"2022-10-25T18:00:04.000000000+00:00","Experimental":"false","GitCommit":"3056208","GoVersion":"go1.18.7","KernelVersion":"5.4.0-132-generic","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.6.10","Details":{"GitCommit":"770bd0108c32f3fb5c73ae1264f7e503fe7b2661"}},{"Name":"runc","Version":"1.1.4","Details":{"GitCommit":"v1.1.4-0-g5fd4c4d"}},{"Name":"docker-init","Version":"0.19.0","Details":{"GitCommit":"de40ad0"}}],"Version":"20.10.21","ApiVersion":"1.41","MinAPIVersion":"1.12","GitCommit":"3056208","GoVersion":"go1.18.7","Os":"linux","Arch":"amd64","KernelVersion":"5.4.0-132-generic","BuildTime":"2022-10-25T18:00:04.000000000+00:00"}

				
			

Create Jenkins Agent Container using Docker

In this section, we will show you how to create Jenkins agent container using Docker.

First, open the Jenkins Dashboard => Manage Jenkins => Plugin Manager as shown below:

Click on the Available tab and type the docker in the search bar. You should see the following screen:

install docker jenkins plugins

Select Docker and click on the Install without restart. You should see the following screen:

Wait for sometime to finish the installation. Once the Docker plugins is installed, click on the Manage Jenkins => Configure Clouds. You should see the following screen:

Define your cloud name and click on the Docker cloud details. You should see the following screen:

Provide your Docker Host IP, port and click on the Add Docker Template. You should see the following screen:

Define all required configurations and click on the Save button.

Next, go to the Jenkins dashboard as shown below:

Click on the New Item. You should see the following screen:

Select Freestyle project and click on the OK button. You should see the following screen:

Define your Jenkins job configurations and click on the Save then Apply button.

If you have done all the configurations right, Jenkins spins up a container, builds the project, and destroys the container once the build is done.

Thank you for reading How to Create Jenkins Agent Container using Docker. We shall conclude.

How to Create Jenkins Agent Container using Docker Conclusion

In this post, we explained how to create Jenkins agent container using Docker. Many companies use Jenkins to accelerate their software development process and achieve goals. It is easy to install and enables smooth functioning. Compared to other DevOps tools in the market, Jenkins can help discover and fix errors at a much early stage. It also helps keep track of repeated tasks and other activities. For customer queries or support, developers can also join their active and thriving community.

To read more about Jenkins please navigate to this section of our blog over here

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x