How to configure Jenkins Slave to run Docker builds?
Create User as Jenkins
sudo useradd -m jenkins
Steps for installing Docker
sudo systemctl restart docker
sudo apt-get update && sudo apt install docker.io -y
Install Maven
sudo apt-get install maven -y
Add Jenkins to Docker Group
sudo usermod -aG docker jenkins
sudo usermod -aG docker jenkins
sudo newgrp docker
sudo systemctl daemon-reload
sudo systemctl daemon-reload
Restart Docker service
sudo systemctl start docker
sudo systemctl enable dockersudo systemctl restart docker
Login to Jenkins Master and restart Jenkins service
sudo service jenkins restart
(Make sure you execute this in Jenkins Master)
Add SSH Keys from Master to Slave
Execute the below command in Jenkins master Ec2.
sudo cat ~/.ssh/id_rsa.pub
Copy the output of the above command:
Now go to Slave node and execute the below command
sudo -u jenkins vi /home/jenkins/.ssh/authorized_keys
This will be empty file, now copy the public keys from master into above file.
Once you pasted the public keys in the above file in Slave, come out of the file by entering wq!
Login to master node and try to SSH from Master to Slave
ssh jenkins@slave_node_ip
this is to make sure master is able to connect slave node. once you are successfully logged into slave, type exit to come out of slave.
Execute the below command in Jenkins master Ec2.
sudo cat ~/.ssh/id_rsa.pub
Copy the output of the above command:
Now go to Slave node and execute the below command
sudo -u jenkins vi /home/jenkins/.ssh/authorized_keys
Once you pasted the public keys in the above file in Slave, come out of the file by entering wq!
Login to master node and try to SSH from Master to Slave
ssh jenkins@slave_node_ip
this is to make sure master is able to connect slave node. once you are successfully logged into slave, type exit to come out of slave.
Now
copy SSH keys into /var/lib/jenkins/.ssh folder also by executing below command
in Jenkins master(make sure you exited from slave by typing exit command:
sudo cp ~/.ssh/known_hosts /var/lib/jenkins/.ssh
Register slave node in Jenkins:
Now to go Jenkins Master, manage jenkins, manage nodes.
Click on new node. give name and check permanent agent.
give name and no of executors as 1. enter /home/jenkins as remote directory.
select launch method as Launch slaves nodes via SSH.
enter Slave node ip address as Host.
click on credentials. Enter user name as jenkins. Make jenkins lowercase as it is shown.
Kind as SSH username with private key. enter private key of master node directly by executing below command:
sudo cat ~/.ssh/id_rsa
(Make sure you copy the whole key including the below without missing anything)
-----BEGIN RSA PRIVATE KEY-----
-----
-----END RSA PRIVATE KEY-----
click Save.
select Host key verification strategy as "manually trusted key verification strategy".
Click Save.
Click on launch agent..make sure it connects to agent node.
Now to go Jenkins Master, manage jenkins, manage nodes.
Click on new node. give name and check permanent agent.
give name and no of executors as 1. enter /home/jenkins as remote directory.
select launch method as Launch slaves nodes via SSH.
enter Slave node ip address as Host.
click on credentials. Enter user name as jenkins. Make jenkins lowercase as it is shown.
Kind as SSH username with private key. enter private key of master node directly by executing below command:
sudo cat ~/.ssh/id_rsa
(Make sure you copy the whole key including the below without missing anything)
-----BEGIN RSA PRIVATE KEY-----
-----
-----END RSA PRIVATE KEY-----
click Save.
select Host key verification strategy as "manually trusted key verification strategy".
Click Save.
Click on launch agent..make sure it connects to agent node.
That's it! Jenkins Master and Slave is configured up!





