How to Configure Docker Containers as Jenkins Build Agents?

How to Configure Docker Containers as Jenkins Build Agents

This tutorial will show you the entire step by step procedure to configure docker containers as jenkins build agents for our jenkins build.

Jenkins build agents or build slaves usually setup for distributing the jenkins jobs and also resource utilization of these jenkins slaves are very less, These jenkins slave agents will run only when builds are scheduled. Else it will be removed.

Lets assume you have already a running jenkins server. If not please go through this link How to install jenkins on Ubuntu?

Server Specification:
Operating System: Ubuntu 20.04
Jenkins Server IP: 192.168.2.21
Docker Server IP: 192.168.2.22

As we are going to setup the agent on docker. We need to install docker on server. For this purpose, I use the server (192.168.2.22)

First, lets install and configure the docker server ready for jenkins..

Install Docker on Ubuntu 20.04:

Use apt command to update the repository and install it as below.

sudo apt-get update
sudo apt-get install docker.io

Configure Docker Remote Host:

Edit the docker service file and change the line to allow the remote API for Docker host.

Search for ExecStart and replace that line with given line.
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock

Reload and restart docker service:

sudo systemctl daemon-reload
sudo service docker restart

Validate Docker Remote Host API

Use curl command to easily validate the docker remote host API from localhost.
curl http://localhost:4243/version
Output:
{"Platform":{"Name":""},"Components":[{"Name":"Engine","Version":"24.0.5","Details":{"ApiVersion":"1.43","Arch":"amd64","BuildTime":"2023-08-21T19:50:14.000000000+00:00","Experimental":"false","GitCommit":"24.0.5-0ubuntu1~20.04.1","GoVersion":"go1.20.3","KernelVersion":"5.4.0-163-generic","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.7.2","Details":{"GitCommit":""}},{"Name":"runc","Version":"1.1.7-0ubuntu1~20.04.2","Details":{"GitCommit":""}},{"Name":"docker-init","Version":"0.19.0","Details":{"GitCommit":""}}],"Version":"24.0.5","ApiVersion":"1.43","MinAPIVersion":"1.12","GitCommit":"24.0.5-0ubuntu1~20.04.1","GoVersion":"go1.20.3","Os":"linux","Arch":"amd64","KernelVersion":"5.4.0-163-generic","BuildTime":"2023-08-21T19:50:14.000000000+00:00"}

Login into the Jenkins server and validate the docker remote host to make sure there is a connection between jenkins and docker host.
curl http://192.168.2.22:4243/version
Output:
{"Platform":{"Name":""},"Components":[{"Name":"Engine","Version":"24.0.5","Details":{"ApiVersion":"1.43","Arch":"amd64","BuildTime":"2023-08-21T19:50:14.000000000+00:00","Experimental":"false","GitCommit":"24.0.5-0ubuntu1~20.04.1","GoVersion":"go1.20.3","KernelVersion":"5.4.0-163-generic","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.7.2","Details":{"GitCommit":""}},{"Name":"runc","Version":"1.1.7-0ubuntu1~20.04.2","Details":{"GitCommit":""}},{"Name":"docker-init","Version":"0.19.0","Details":{"GitCommit":""}}],"Version":"24.0.5","ApiVersion":"1.43","MinAPIVersion":"1.12","GitCommit":"24.0.5-0ubuntu1~20.04.1","GoVersion":"go1.20.3","Os":"linux","Arch":"amd64","KernelVersion":"5.4.0-163-generic","BuildTime":"2023-08-21T19:50:14.000000000+00:00"}
At this point, docker host is ready.

In order to run docker container as jenkins build agent, we need to create our docker images for jenkins build agents.

This docker image should be ready with all required packages for jenkins projects. For example, if our projects depends on git, maven, ant, artifactory. On that case, we have to install everything along with docker images.

So here is my basic dockerfile that I use for jenkins build agents.
FROM ubuntu:20.04
RUN apt-get update && \
    apt-get install -qy openjdk-13-jdk && \
    apt-get install -qy git && \
    apt-get install -qy maven && \
    apt-get install -qy openssh-server && \
    mkdir -p /var/run/sshd && \
    apt-get -qy autoremove && \
    useradd -m jenkins -s /bin/bash && \
    mkdir -p /home/jenkins/.ssh/
COPY id_rsa.pub /home/jenkins/.ssh/authorized_keys
RUN chown -R jenkins:jenkins /home/jenkins/.ssh/
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
Above dockerfile will build the docker image from Ubuntu 20 and will install required packages, user creations and other configurations required.

Also there is a line mentioned to copy SSH public key to a SSH folder. This key will be used by jenkins while launching the docker container to login and perform actions as per the jenkins build. So lets create SSH key.
selva@docker-host:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/selva/.ssh/id_rsa): Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/selva/.ssh/id_rsa
Your public key has been saved in /home/selva/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:1lEHdHxUMF8rJDdfky9wsrMXQTcLkPrh4AsAtL+eyec selva@docker-host
The key's randomart image is:
+---[RSA 3072]----+
|  ..       +*O*=O|
|   ..      o*oB=O|
|   ..     o  * *o|
|    ..   + oo + .|
|     .. S = .o o |
|      .o . o. .  |
|     .  . .  .   |
|    o o. .       |
|     =oE         |
+----[SHA256]-----+
selva@docker-host:~$
At this point, we will have both public and privay key in the same directory. So we can build the docker images.

Build Docker Image for Jenkins Build Agents:

As you are in the same directory, we can build the docker image using the below command.
sudo docker build -t learnitguide/jenkins-slave-agent:1.0 .
Replace with your docker hub repository name (learnitguide/jenkins-slave-agent) and tag (1.0) as per your need.

Our docker image is ready.

Push Docker Image to Docker Registry:

Login into docker hub registry using below command and push the image to docker hub. Then only our image will be pulled from jenkins to launch the docker container.
sudo docker login
Use "docker push" command to push the docker image to docker hub registry.
sudo docker push learnitguide/jenkins-slave-agent:1.0
Now the jenkins build agent docker image is pushed to our docker image registry. This image will be used on jenkins to build the agents.

Lets go to jenkins and configure the Jenkins Build agents.

Configure Jenkins Build Agents:

It required more steps to be done, so you can refer the below youtube video.

That's it for this post. Keep practicing and have fun. Leave your comments if any.

Post a Comment

0 Comments