We have a Jenkins master server and where we have installed docker as well. Also we have other 3 remote vms with docker installed on them,
Our requirement here in Jenkins to do below things.
1)Jenkins should create image from the committed docker file in git
2) Jenkins should push this image to the DTR
3) Jenkins should launch these pushed images to the containers in the remote vms.
What are the possible ways to do.
How to configure Jenkins to run the containers in the remote vms?
There are alot of ways to achieve this, what fits best will depend on your requirements. The following is very simple and just executes shell commands on different jenkins-agents.
If those vms are connected to your jenkins master as agents, you are free to checkout your Dockerfile and build on one agent (or master), then switch agents and launch your image there.
Along the lines of the following snipped
def image = 'image123:latest'
node('build-vm') {
stage('Checkout') {
checkout scm
}
stage('Build image') {
sh "docker build -t ${name} ."
[...]
sh 'docker push ${name}'
}
}
node('vm1') {
stage('Run image on vm1') {
sh 'docker run -d ${name}'
}
}
node('vm2') {
stage('Run image on vm2') {
sh 'docker run -d ${name}'
}
}
node('vm3') {
stage('Run image on vm3') {
sh 'docker run -d ${name}'
}
}
Ideally, you would use the Jenkins Kubernetes plugin in order to execute jobs on any remote VM Jenkins agents, where kubectl has been installed.
That allows a Docker in Docker build: from "Building Docker Images inside Kubernetes" written by Vadym Martsynovskyy.
You also have other options, detailed in "Pipelines With Docker Alternatives" from "Joost van der Griendt's CI/CD Knowledge Docs!", using either:
The main point is: build docker containers with Jenkins is often done in a Kubernetes setup context.