Setup Docker Containers as Build Slaves for Jenkins

Rohit Raut
4 min readJul 24, 2021

--

Jenkins is one of the most important tools in DevOps. Jenkins is used in the CI/CD stage of DevOps. In this blog, I am going to talk about Setup Docker Containers as Build Slaves for Jenkins.

Steps:

  1. Configure docker host for remote connection
  2. Write Docker file to Configure Docker Container
  3. Configure Docker as a cloud in Jenkins server
  4. Create a freestyle job to test

5. checks When Build Start:

  • The container will automatically be launched on the docker host machine
  • Container destroyed after build completion.

Prerequisite:

One VM with Docker installed also called as Docker Host

Let's start with practical

Configure docker host for remote connection

The first step is to set up a docker host where the Jenkins server will launch slave containers. Jenkins master connects to docker using APIs. So we need to enable remote connection/remote API for docker. In this article, we will open the connection at port 2376.

  • Login to the docker host machine and open the docker service file with path /usr/lib/systemd/system/docker.service . In this file search ExecStart and append -H tcp://192.168.43.177:2376 .
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://192.168.43.177:2376
  • reload and restart the docker service.
systemctl daemon-reload
service docker restart

That’s it! All done with Docker Host machine🤩

Step2: Write Dockerfile for slave

There are 3 connection methods for which we have different container environments needed

  1. Attach Docker Container
* Docker image must have Java installed.
* Docker image CMD must either be empty or simply sit and wait forever, e.g. /bin/bash.

2. Connect with JNLP[Java Network Launch Protocol]

* Jenkins master has to be accessible over network from the container.* Docker image must have Java installed.* Docker image must launch agent.jar by itself or using the EntryPoint Arguments below.

3. Connect with SSH

* The docker container’s mapped SSH port, typically a port on the docker host, has to be accessible over network from the master.* Docker image must have sshd installed.* Docker image must have Java installed.* Log in details configured as per ssh-slaves plugin.

Step3: Configure Docker as a cloud in Jenkins server

Install Plugin Docker Plugin https://plugins.jenkins.io/docker-plugin/

Let's, Configure docker cloud

manage-Jenkins → Manage Nodes and Cloud →Configure Cloud

Add docker as a cloud

Enter docker URI and click on test connection if show you version of docker then docker is configured as cloud

Now we need to provide templates[Details of Image and Connection Method].

Method-1: Attach Docker Container

FROM centos:latestRUN yum install java git -yCMD ["/bin/bash"]

build image and push to docker hub repository

docker build -t docker_hub_account_name/image_name:image_version  location_of_docker_filedocker logindocker push image_name:image_version

Create a new job and only we need to pass label

save and start the job. Jenkins Launch the container with the help of a template.

For a conceptual understanding of the other two methods, you can refer to the below article.

For method 3:

I hope, you liked this article. Follow me for more interesting technology-related articles.

Thank you for reading…😇

--

--