Configure Docker Container as Build Slave for Jenkins | by Gaurav Gupta

Jenkins - an open-source automation server that enables developers around the world to reliably build, test, and deploy their software.

Jenkins is used to building and testing your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build.

When we run a job, it is necessary to have that command/program installed in the node where Jenkins running. Like if we need to run git plugin for SCM, it is also compulsory to have git installed in the local node.

And We all know that it is always good to have an isolated environment or system for each program. Like we need to run maven in one of my jobs and have need to train model of machine learning in another job, So it is good way to have an isolated system for any of the need. Here the role of Container comes into play…

Now let’s come to the task…

Task Description:

Prerequisites:

  • Have a Jenkins running either on top of local VM or in Cloud Instance.
  • Have an OS having Docker-ce-18.09* installed.

Steps:

Plugins are the primary means of enhancing the functionality of a Jenkins environment to suit organization- or user-specific needs. There are over a thousand different plugins that can be installed on a Jenkins controller and integrate various build tools, cloud providers, analysis tools, and much more.

To set up a dynamic node using Docker, we need to install Docker Plugin…

In Dashboard, go to Manage Jenkins -> Manage Plugins

Then click to Available, and then search for Docker

Click on Install without restart.

Now tick on Restart Jenkins when installation is complete and no jobs are running.

I have one more OS where Docker is already installed and up…

Make sure you installed docker-ce-18.09* version so while connect with Jenkins, no conflict occurs.

Now as my Docker Engine and Jenkins run in different OS, so we need to tell docker service that if any other system wants to use Docker Engine, then it comes via TCP to a particular port.

For this, go to Docker Service configuration file (/usr/lib/systemd/system/docker.service)

vi /usr/lib/systemd/system/docker.service

In this file, edit line started with ExecStart similar to below image.

- H tcp://{{ ip_adress }}:{{ port_no }}

After that, restart the docker service using command…

systemctl daemon-reload

systemctl restart docker

Docker help in launching dynamic node because it immediate launch a new OS as any job runs with isolated environment.

  • In Jenkins Dashboard, Goto Manage Jenkins -> Manage Nodes and Clouds
  • Click on Configure Clouds
  • Click on Add a new Cloud and select Docker. This option comes up because we already installed Docker plugin.
  • Now give Name, and Click on Docker Cloud Details…
  • Give Docker Host URL and Click on Test Connection.

If you got any error, check your docker version and make sure you have same version(18.09*) installed.

  • Tick on Enabled and Click on Docker Agent Templates…
  • Prerequisites for docker image:
  1. Docker image must have Java installed.
  2. Docker image CMD must either be empty or simply sit and wait forever, e.g. /bin/bash.
  3. Don’t forget to create one directory in image for Remote File System Root.

I created one image gaurav0417/jenkins-slave using Dockerfile and push to docker hub, if you want you can try with same image.

  • In Docker Agent Templates, fill following options…

Click on Apply and then Save.

Now let’s check the whole setup properly worked or not by configure a job on Jenkins…

  • On Dashboard, Click on New Item. Enter a name and choose Freestyle project.
  • Tick on Restrict where this project can be run in General and choose the same label which you give earlier.
  • For testing purpose, i just run a command. For this, In Build tab, Click on Add Build Step and Choose Execute Shell.

You can create any pipeline, just make sure you have that program(command) installed in the image you created.

  • Click on Apply and then Save.
  • Click on Build Now
  • Go to Console Output of Job, you can see here that it immediate launches one container and run the command there. As soon as command execute, container also removed.

Thanks for reading :)

Do Clap if you found it worth…👏

Feel free to connect on linkedin…😊

Having any issue related to task, please DM me…

Tech Enthusiasts, Passion to learn and share