banner



How Does Self Repair Work In The Tinkers Plugin

This article guides you to fix Jenkins build inside a Docker container using Docker-based Jenkins build agents.

The resource utilization of the Jenkins agents is very less if y'all do not take builds happening continuously. It is amend to apply ephemeral Docker containers as Jenkins agents for better resource utilization in this scenario.

As you know, spinning up a new container takes less than a minute; every build spins up a new container, builds the project, and is destroyed. This way, you can reduce the number of static Jenkins build VMs.

Docker Containers every bit Build Agents/Slaves

In this guide, I will walk you through the steps for configuring Docker Containers every bit build agents.

I assume that you have a Jenkins server up and running. If you practise non have ane, follow this tutorial. How to setup Jenkins ii

If you want docker based Jenkins setup, you tin can follow this tutorial -> Setup Jenkins On a Docker container

Let's Implement Information technology

Configure a Docker Host With Remote API [Important]

The beginning affair we should do is prepare a docker host. Jenkins server volition connect to this host for spinning upwardly the build agent containers. I am going to apply the Centos server equally my docker host. You can use any Bone which supports Docker.

Jenkins primary connects to the docker host using Rest APIs. Then we need to enable the remote API for our docker host.

jenkins docker slave

Brand sure the following ports are enabled in your server firewall to accept connections from Jenkins principal.

Docker Remote API port 4243
Docker Hostport Range 32768 to 60999

32768 to 60999 is used by Docker to assign a host port for Jenkins to connect to the container. Without this connection, the build slave would go in a awaiting state.

Lets become started,

Step one: Spin up a VM, and install docker on information technology. Y'all tin can follow the official documentation for installing docker. based on the Linux distribution you utilise. Make sure the docker service is up and running.

Pace 2: Log in to the server and open up the docker service file /lib/systemd/system/docker.service. Search for ExecStart and replace that line with the following.

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

Step 3: Reload and restart docker service.

          sudo systemctl daemon-reload sudo service docker restart        

Footstep 4: Validate API by executing the following scroll commands. Supersede 54.221.134.7 with your host IP.

          coil http://localhost:4243/version scroll http://54.221.134.7:4243/version        

Check the docker remote API article for a detailed explanation of Docker API.

One time you enabled and tested the API, y'all can now start building the docker slave image.

You Might Like
Setup Jenkins Build Agents on Kubernetes Cluster

Create a Jenkins Amanuensis Docker Paradigm

I have created a Jenkins docker image for maven. You can use this epitome or use its Dockerfile as a reference for creating your ain.

If you are creating the prototype on your own, its image should contain the following minimum configurations to act as a slave.

  1. sshd service running on port 22.
  2. Jenkins user with password.
  3. All the required application dependencies for the build. For example, for a java maven project, you need to have git, java, and maven installed on the epitome.

Make sure the sshd service is running and can be logged into the containers using a username and password. Otherwise, Jenkins will not be able to beginning the build process.

Notation: The default ssh username is jenkins and the password is also jenkins every bit per the given Dockerfile. You will accept to utilize these credentials in the beneath configuration.

Configure Jenkins Server With Docker Plugin

Step one: Head over to Jenkins Dashboard –> Manage Jenkins –> Manage Plugins.

Step two: Under the Available tab, search for "Docker" and install the docker cloud plugin and restart Jenkins. Here is the official plugin site. Make sure you install the correct plugin as shown below.

Install Jenkins Docker Plugin

Step 3: Once installed, head over to Jenkins Dashboard –> Manage Jenkins –>Configure system.

Step 4: Nether "Configure System", if you scroll downward, there will be a section named "cloud" at the last. In that location you can make full out the docker host parameters for spinning up the slaves.

Note: In Jenkins versions 2.200 or afterward you volition find defended deject configuration under Manage Jenkins –> Manage Nodes and Clouds

Pace 5: Nether docker, you need to fill out the details equally shown in the prototype below.

Note: Replace "Docker URI" with your docker host IP. For case, tcp://10.128.0.3:4243 You tin can use the "Examination connexion" to test if Jenkins is able to connect to the Docker host.

configure Jenkins docker cloud

Footstep six: At present, from "Docker Agent Template" dropdown, click theastward "Add Docker template" and fill in the details based on the explanation and the paradigm given below and salvage the configuration.

  1. Labels – Identification for the docker host. It will be used in the Chore configuration. Here we use java-docker-slave
  2. Name: Proper name of the docker template. Here we use the same name as label ie, java-docker-slave
  3. Docker Image bibinwilson/jenkins-slave:latest or the image that you created for the slave.
  4. Remote Filing System Root – Dwelling house folder for the user y'all have created. In our example, it'due south /dwelling house/jenkins
  5. Credentials – click add and enter the SSH username and password that you lot take created for the docker image. Leave the rest of the configuration equally shown in the image below and click save. If you are using my Docker image, the user volition exist jenkins & password is also jenkins.

Note: There are additional configurations like registry authentication and container settings that you might have to use when configuring this gear up in the corporate network.

Jenkins Docker agent tempalte configuration

Yous tin can also use JNLP-based slave agents. For this, the configurations need a piddling change as shown below. Primarily the docker paradigm proper name and the connect method.

Note: For JNLP to work, you demand to enable the JNLP connection port (50000) in Jenkins's global security configuration (TCP port for inbound agents). Also, the Jenkins primary firewall should be able to take this connectedness form the docker host.

Docker jnlp agent template configuration

By default, the workspace will not be persisted in the host. Notwithstanding, if you want the workspace to be persistent, add a host volume path under container settings.

For example, if you want the workspace to be available at /home/ubuntu, yous can add the volume path as shown beneath. /home/jenkins is the path inside the container.

          /domicile/ubuntu:/abode/jenkins        

Towards the right of the Volumes option, if you click the question mark, it will show you additional volume options as shown below.

mount volume to Jenkins docker agent

Building Docker Images using Docker Agents

In that location are use cases where you have to build Docker images in the CI process.

Yous can reach that using Docker based Jenkins amanuensis as well.

If you lot are planning to run docker in docker for your CI process, you can mount the host docker.sock every bit book to execute docker commands.

Check out my article on running docker in docker to know how to run docker in docker.

Test Jenkins Build Inside a Docker container

At present that you accept the slave configurations gear up, we will test the docker agent plugin using a freestyle task.

  1. Create a freestyle chore, select "Restrict where this project can be run" option and select the docker host every bit a slave using the label.
  2. Add a shell build step which echoes a uncomplicated "Hi World"
Jenkins build job with container as build agent

If yous take washed all the configurations right, Jenkins will spin up a container, builds the projection, and destroys the container in one case the build is done.

Kickoff, you volition encounter a pending notification every bit Jenkins tries to deploy a container on run time and establishes an SSH connection. After a few seconds, your task will showtime building.

image

You can bank check the build logs in your jobs console output every bit shown below.

docker slave output min

Also, you can check out the video explaining the whole process.

Possible Errors:

  1. Jenkins is non able to deploy containers on the host:– Please make sure you have proper connectivity to the docker host on API port.
  2. Jenkins builds goes in the pending state forever:– Make sure you have Docker host ports (32768 to 60999) access from Jenkins chief to docker host.
  3. JNLP slaves go into the pending state: Make sure you take enabled the JNLP port in the Jenkins global security configuration.

Decision

In this article, I walked you lot through the process of setting up dynamic Jenkins Docker agents

If you want to build Jenkins inside a Docker container, and y'all don't have a Kubernetes setup, this is the all-time option.

If you lot have a Kubernetes setup, then you lot consider setting upwardly the agents on Kubernetes. You tin bank check my web log on Kubernetes based Jenkins build agents to for more than details.

It can be further customized to fit your specific utilise cases.

Please let me know your thoughts in the comment section. Also, don't forget to share this article 🙂

Source: https://devopscube.com/docker-containers-as-build-slaves-jenkins/

Posted by: suttonnizips.blogspot.com

0 Response to "How Does Self Repair Work In The Tinkers Plugin"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel