Sometimes there will be cases where we have to execute in a remote machine. So the same case will be applicable for a Jenkins execution also. When we configure a job in Jenkins, we might need to run that particular content in another virtual machine or a remote machine.
In such cases, we have to establish a connection to the remote machine from Jenkins to execute the content.
When an organization has more virtual machines, it becomes easier to connect with them and work, but in some organizations, they will have the limitation for the virtual machines.
In such cases, we will be using a docker to create a virtual machine and will connect to that particular docker container and execute our Jenkins job.
I hope you know that the docker container is nothing but a basic version of a Unix system or it could be any other OS system.
So let me explain my case here, I have a Windows laptop, now I have created a virtual machine with Virtualbox, and my virtual machine is centos 8.
In the centos I have installed the docker, I have created one of the docker containers to contain Jenkins; similarly, I have created another docker container that will have a remote machine where scripts will be executed from Jenkins.
So in this lecture, we are going to learn how to execute a Jenkins job in a remote Docker container.
[[email protected] jenkins-data]# ls demo.sh docker-compose.yml jenkins_home [[email protected] jenkins-data]# mkdir centos-vm [[email protected] jenkins-data]# ls centos-vm demo.sh docker-compose.yml jenkins_home
RUN yum -y install openssh-server
RUN useradd remote_user && echo "1234" | passwd remote_user --stdin && mkdir /home/remote_user/.ssh && chmod 700 /home/remote_user/.ssh
echo "remote_user:1234" | chpasswdsets the password for the created user.
ssh-keygen -f remote-key
[[email protected] centos-vm]# ls Dockerfile remote-key remote-key.pub
remote-keyis a private key and
remote-key.pubis a public key
Dockerfileand COPY the
remote-key.pubfile into the virtual machine.
COPY remote-key.pub /home/remote_user/.ssh/keys
remote_useronly has ownership from
#chown username:groupname -R directory RUN chown remote_user:remote_user -R /home/remote_user
chmod 600 /home/remote_user/.ssh/keys
CMD /usr/sbin/sshd -D
Complete Dockerfile content
FROM centos:7 RUN yum -y install openssh-server RUN useradd remote_user && echo "1234" | passwd remote_user --stdin && mkdir /home/remote_user/.ssh && chmod 700 /home/remote_user/.ssh COPY remote-key.pub /home/remote_user/.ssh/authorized_keys RUN chown remote_user:remote_user -R /home/remote_user && chmod 600 /home/remote_user/.ssh/authorized_keys RUN /usr/sbin/sshd-keygen CMD /usr/sbin/sshd -D
So far, we have set the details of how the virtual machine should be. Now we have to edit the docker-compose file to create an image using the Dockerfile we created.
[[email protected] centos-vm]# ls Dockerfile remote-key remote-key.pub [[email protected] centos-vm]# cd .. [[email protected] jenkins-data]# ls centos-vm demo.sh docker-compose.yml jenkins_home
docker-compose.ymlfile add another service (virtual machine creation)
remote_vm: container_name: remote_vm image: remote_vm build: context: centos-vm networks: - net
Dockerfilerelated things present; directory path where the
Complete docker-compose.yml file
version: '3.7' services: jenkins: container_name: jenkins image: jenkins/jenkins ports: - "8080:8080" volumes: - $PWD/jenkins_home:/var/jenkins_home networks: - net remote_vm: container_name: remote_vm image: remote_vm build: context: centos-vm networks: - net networks: net:
Now we are done with configurations; let's build the image for the virtual machine.
Verify whether the centos image has been downloaded.
From the above image, you can confirm that centos and remote_vm images are present/downloaded.
In the above command, you have built the virtual machine, but that does not mean you have made the service up.
Let's make the virtual machine up using the
docker-compose up -d
Verify whether the virtual machine is up.
You cannot connect to the virtual machine from the base machine (where all images are present) because the base machine and virtual machine are not in the same network
ssh [email protected]_vm
The Jenkins machine can connect to the virtual machine because they are in the same network (-net). Check docker-compose.yml if you have doubts.
So if you provide the password you will able to log in, and the password we have set is "1234". But every time we try to login, it will ask for the password, to avoid this we have already created
Also, we have already copied the
remote-key.pub to the virtual machine using the Dockerfile
COPY remote-key.pub /home/remote_user/.ssh/authorized_keys
Now we have to copy the remote-key file to the Jenkins container so that we can log in without providing the password.
The copy command for docker
docker cp remote-key jenkins:/tmp/
[[email protected] jenkins-data]# cd centos-vm/ [[email protected] centos-vm]# ls Dockerfile remote-key remote-key.pub [[email protected] centos-vm]# docker cp remote-key jenkins:/tmp/
Before you can use the remote key, change the permission for the remote key.
docker exec -u root jenkins bash -c "chown jenkins:jenkins /tmp/remote-key"
Now try to login from Jenkins container;
-i /tmp/remote-key used to mention where the key is present.
ssh -i /tmp/remote-key [email protected]_vm
We have to add SSH plugins to connect with a remote server in Jenkins;
echo "this is from jenkins" > /tmp/demo-remote-execution.txt
[[email protected] jenkins-data]# ls centos-vm demo.sh docker-compose.yml jenkins_home [[email protected] jenkins-data]# docker exec -it remote_vm bash [[email protected] /]# cat /tmp/demo-remote-execution.txt this is from jenkins
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.