Setup Dynamic Docker Slave(Agent) and Integrate with Jenkins Master

Setup Dynamic Docker Slave(Agent) and Integrate with Jenkins Master

- 4 mins

Introduction

Jenkins offers a robust master-slave architecture that allows for distributed builds, making it highly flexible. In this guide, we’ll explore how to configure slave nodes using Docker and connect them to the Jenkins Master.

Benefits of Using Docker Containers as Jenkins Build Agents:


Prerequisites

1- Two Ubuntu virtual machines (VMs) are required: one for Jenkins Master and another for the Docker Host.

2- Jenkins Master is already installed and operational.

3- Set up the Docker Host.


Step 1 - Set Up Docker Host with Remote API

Access the Docker host machine and edit the Docker service configuration file. Look for the ExecStart line and replace it with the updated version.

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

Update the ExecStart line to the following:

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

docker-jenkins

Restart the Docker service by running the following commands:

sudo systemctl daemon-reload
sudo service docker restart

docker-jenkins

Verify that the API is correctly set up by executing this curl command:

curl http://localhost:4243/version

docker-jenkins


Step 2 - Create Jenkins Slave Docker Image

1- Clone the public repository for Jekinds Docker Slave image bulding. The repository forked from another project. We need to clone the code in Docker Host VM. We will build image in Docker Host VM and We will no need to pull image from public registery.

Clone the repository containing the Dockerfile using the command below:

git clone https://github.com/guneycansanli/jenkins-docker-slave
cd jenkins-docker-slave

docker-jenkins

Build the Docker image for the Jenkins slave:

sudo docker build -t guney-jenkins-slave .

To view the list of Docker images available on the host, run:

sudo docker images

docker-jenkins

Step 3 - Set Up Jenkins Server with Docker Plugin

Log in to the Jenkins Master and ensure the Docker plugin is installed.

docker-jenkins

Configuraiton of Docker Slave , Cloud Node

docker-jenkins

docker-jenkins

tcp://docker_host_dns-or-IP:4243

docker-jenkins


Step 4 - Set Up Docker Agent Templates

/home/jenkins

docker-jenkins

docker-jenkins

Adding New SSH Creds: docker-jenkins

docker-jenkins


Step 5 - Create a Build Job in Jenkins

docker-jenkins

pipeline {
    agent { 
        label "docker-slave"
    }
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

docker-jenkins

docker-jenkins

docker-jenkins


Thanks for reading…





:+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1:


Guneycan Sanli.


Guneycan Sanli

Guneycan Sanli

A person who like learning, music, travelling and sports.

comments powered by Disqus