Breaking

How to Setup Docker Containers as Build Agents for Jenkins

In the realm of DevOps, orchestrating seamless workflows is imperative, and Jenkins, with its extensibility, stands out as a powerhouse. 

In this comprehensive tutorial, we will delve into the integration of Docker and SSH for Jenkins agents, providing a step-by-step guide to enhance your CI/CD pipelines. You can see the previous guide, namely setup Jenkins on Kubernetes


Learn how to integrate Docker and SSH to set up a Jenkins agent for continuous integration and deployment (CI/CD) pipelines in this comprehensive tuto


Prerequisites 

Before embarking on this journey, ensure you have the following prerequisites in place:

 

  1.      A Jenkins server up and running.

 2.     Docker installed on both the Jenkins server and the machine you intend to use as a Docker host.

 3.     SSH access configured between the Jenkins server and the Docker host.

 4.    Java should be installed on your agent server.

 

Step 1: Set Up Jenkins


Open your Jenkins dashboard and navigate to "Manage Jenkins."

 

New code button in settings page. Access it through Jenkins dashboard by navigating to "Manage Jenkins."

Click on "Manage Nodes "


 

Settings page with new code button. Access additional options by clicking 'Manage Nodes'


Select "New Node" to create a new Jenkins agent.

 New code button in settings page. Click "New Node" to create a Jenkins agent.


Provide a name for the agent and choose "Permanent Agent."


'New code button in settings page: Name agent and select "Permanent Agent


In the configuration, specify the following:


New code button in settings page. Remote root directory: Choose directory for launching Jenkins agents.


•        Remote root directory: Choose a directory on the Docker host where Jenkins agents will be launched.

•        Labels: Assign labels to the agent for identification.


Related Article :  HOW TO INSTALL AND USE DOCKER : Getting Beginner


Select Manage Jenkins --> Credentials --> system --> Global unrestricted --> add credentials 


New code button in settings page. Accessible via Manage Jenkins --> Credentials --> system --> Global unrestricted --> add credentials.'


Input username and  password  for docker host. Example for user jenkins 



Input the ip addres of the  docker host




Note :

  • Usage : Select Usage this is node much as possible  for node ssh
  • Lauch Method : Select agent ssh for add node  docker with ssh agent
  • Host  :  Input IP ADDRESS OR Hostname Docker Host 
  • Credentials :   Select Credentials for docker host. 

 Save the configuration.

Related Articel :  How to Install Kubernetes on Ubuntu 20.04

Step 2 : Create user  jenkins  and install java for docker host


Add user jenkins in docker host

#  adduser jenkins


Add entri file  below this paragraph  in visudo

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
jenkins ALL=(ALL) NOPASSWD: ALL
# See sudoers(5) for more information on "@include" directives:


Install package java for  requiretment jenkins agent 


# sudo apt install default-jre -y


Verify java version 


# java --version
openjdk 11.0.21 2023-10-17
OpenJDK Runtime Environment (build 11.0.21+9-post-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.21+9-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)


Step 3 :  Install Docker  Plugin

 

In the Jenkins dashboard, navigate to "Manage Jenkins" > "Manage Plugins." >> Select Plugins --> Install

New code button in settings page for installing Docker plugin using Jenkins

Select  Plugin this below : 

CloudBees Docker Build and Publish plugin Version 1.4.0

CloudBees Docker Custom Build Environment Plugin Version 1.7.3

CloudBees Docker Hub/Registry Notification Version 2.7.1

Docker


Step 4:  CI/CD : Automation Deploy Application HTML Using Jenkins

Fantastic! Adding a Docker node is a great step. Now, let's dive into deploying that simple HTML application via Jenkins. Here's a brief guide for you:

Prerequisites 

  1. Docker Hub 
  2. Jenkins Server is running 
  3. Dockerfile
  4. Jenkinsfile
Absolutely, adding Docker Hub credentials to Jenkins is a smart move for secure and seamless integration.

New code button in settings page for adding Docker Hub credentials.

Add  Credentials for docker hub 

New code button in settings page. Add Credentials for docker hub.

Example Dockerfile  


FROM nginx:latest
COPY ./html/. /usr/share/nginx/html/.
EXPOSE 80


Example Jenkinsfile 


pipeline {
    agent any
    stages{
        stage("checkout"){
            steps{
                checkout scm
            }
        }

        stage("Build Image"){
            steps{
                sh 'sudo docker build -t webserver-devopsgol-betahjomblo:1.0 .'
            }
        }
        stage('Docker Push') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'docker_cred', passwordVariable: 'DOCKERHUB_PASSWORD', usernameVariable: 'DOCKERHUB_USERNAME')]) {
                    sh 'sudo docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD'
                    sh 'sudo docker tag webserver-devopsgol-betahjomblo:1.0 adinugroho251/webserver-devopsgol-betahjomblo:1.0'
                    sh 'sudo docker push adinugroho251/webserver-devopsgol-betahjomblo:1.0'
                    sh 'sudo docker logout'
                }
            }
        }

      stage('Docker RUN') {
          steps {
           sh 'sudo docker run -d -p 80 --name webserver-betahjombloterus  adinugroho251/webserver-devopsgol-betahjomblo:1.0'
      }
    }
 }
}
    


For detail  projects simple html : https://github.com/devopsgol/apps-html


Add Job in Jenkins  for CI/CD 

Add Job in Jenkins


Select Restrict where this project can be run  for remote docker host  and input label docker agent ssh. 

Select Restrict where this project can be run  for remote docker host  and input label docker agent ssh.

Select Source code Manangement  and  input source code project. example for a github.
Source code Manangement

Checklist  Pool  SCM  for automation deploy using jenkins. When there's a code change from the Source Code Management (SCM) like GitHub or the latest commit changes, Jenkins will trigger to perform the build and deploy.


Pool  SCM  for automation deploy using jenkins. When there's a code change from the Source Code Management (SCM) like GitHub or the latest commit changes, Jenkins will trigger to perform the build and deploy.

Select  Build Steps --> Execute Shell 


Certainly! Here's the command to build the container:

imageName=web-devopsgol:${BUILD_NUMBER}
containerName=webserver-betahjombloterus 

sudo docker build -t $imageName .
sudo docker run -p 3000:3000 -d --name $containerName $imageName



Build Steps
Save Configuration  

Click Build Now  for running is job

Build now  is running job

Logs for job pipelines jenkins

Logs for job pipelines jenkins

Alhamdulilah container is running and successfuly curl in services application. 

Container is running well

Conlusion 

Congratulations! You have successfully integrated Docker and SSH for Jenkins agents, enhancing the scalability and flexibility of your CI/CD pipelines.

If you want to read more insightful tutorials and articles related to DevOps, feel free to visit The Insider's Views.

 

No comments:

Post a Comment