Setup Docker container as Build Slaves for Jenkins for a Dynamic Provisioning

Introduction

In my previous write-up i show you how to create your own docker images with components centos, maven, openjdk8…And today i will walk you through a step by step how we’ll use this image to integrate a Master node Jenkins with a docker engine to launch some slave containers.

Environment

  • one Guest Os of Rhel8 from Virtualbox which contains the Jenkins Master node inside.
  • And my local machine Ubuntu as a Docker engine server to provide containers for the Jenkins.

My Plan

Below steps will show you how to achieve this plan.

Step 1: Start the Jenkins service and login it

Jenkins service running and now login with this ip

Step 2: Install Docker Plugin in the Master node

Install docker plugin

Step 3: Start Docker engine service on my local Machine

docker service running

Step 4 : Enable tcp connection on Docker engine

Modify the service file by binding the ip address of your local machine with port 2376 like:

# sudo vim /lib/systemd/system/docker.service

# systemcl daemon-reload => restart the daemon

# systemctl restart docker.service => restart the docker service as we have modified the config file

Suceessfully bind my local machine ip in Docker engine file

Step 5: Return to Master node to manage nodes by configuring Cloud

Head over to Jenkins Dashboard –> Nodes –> Configure Clouds.

  • Choose “Name” as : “Docker” => as we want to integrate Docker engine
  • In “Docker Host URL”: => type the ip of the Machine running the Docker engine server and port of Docker engine which you define in the Config file of this service
Configure Clouds option
  • Now click “Test connection” =>shows connection refused so you have to disable firewall on the machine running Docker engine server:

In my case it’s ubuntu so i will deactivate firewalld service like: sudo service ufw stop

Now After disable firewall click again “Test connection” and you will see the version of your Docker engine showing in Jenkins.

Now Click on “Docker Agent templates” and add the image from https://hub.docker.com/repository/docker/tcdocker2021/jenkins-slave-maven-jdk

So after add a label name and name for the container which will be used by Jenkins like the image below :

After save it also in Configure Clouds options

Step 6: Launch a freestyle project job to test our setup

  • Start a new project like
  • Next page in “Restrict where this project can be run ” choose the name you define as label when configure the “Configure Cloud” previously
As i want to test our setup so feel free to give any command you want to be execute in the “Execute shell” field
  • Now After click save, you will be on the below page and and you will see that the job not yet started . So click on “Build now” to start the job:
  • After click build now, below you can see the job is in a pending state
  • After some minutes, as the node doesn’t exist on master, so Master has to contact docker engine in another machine (my local ubuntu machine) and then launch a container. After job terminated the container is removed and know that this process is very quick.
  • As is a dynamic provisioning, the process is very quick, below you can see while job running, the slave node is created from Docker engine by our master
Job running and immediatley a slave node is created in nodes tabs
  • After job ran, you can check the “Console Output” and see Master has created a node at the node name given in my “Configure Cloud” options
  • Click on “Built on Docker” for the confirmation, a container was launched and removed asap

And that’s how we can make a dynamic provisioning in Jenkins with a Docker Engine as integration.

Step 7: Let’s create a package and a test with our job from a Github maven app and our docker image

  • And under “Build”, will test our maven app from github by test it before and then create a package for this app
  • Now After click save, you will be on the below page and and you will see that the job not yet started . So click on “Build now” to start the job:
  • After click “Build Now”, below you can see the job is in a pending state
  • After some minutes, go to “under nodes” , while the job is running you will see again a Docker node is created
  • If you go quickly in your local machine running the Docker engine you will see a docker container launched successfully
  • Check the Console Output, we can see our jobs ran with success
  • And also under Workspace, we have a package created successfully

CONCLUSION

Thank you and see you in another fun story where we can discover something new in DevOps world 👻 !!!

--

--

CI/CD Tech Explorer / ( DevOps & Cybersecurity Enthusiast ) on Lunar Planet🚀

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Edmond Tchamie

CI/CD Tech Explorer / ( DevOps & Cybersecurity Enthusiast ) on Lunar Planet🚀