image source : Internet
Build & Deploying Docker image in the Docker Server.
1) Install jenkins, Docker in CI\CD Server.
2) Install docker in deployment server.
3)Create jenkins pipeline job to build & deploy docker image in docker(Deployment)server.
Prerequisites :
1) AWS Account
2)Docker Hub Account (Public Repo for Docker images)
1) Setup CI\CD Server with Jenkins & Docker in Ubuntu
#update package Manager π $sudo apt update
#InstallJava π $sudo apt install openjdk-8-jdk
#InstallJenkins
$wget -q -O - pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add $sudo sh -c 'echo deb pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
$sudo apt update
$sudo apt install jenkins
$sudo systemctl start jenkins
$sudo systemctl status jenkins
Jenkins is now up & runningπ
#InstallDocker π apt install docker.io -y
Add jenkins users to Docker π usermod -aG docker jenkins
Restart π Sudo systemctl restart jenkins
Plugins used in jenkins : π SSH
2) Install Docker in Deployment Server
update package Manager
$sudo apt update
Install Docker
$sudo apt install docker.io -y
Jenkin Pipeline Script using secret key
pipeline {
agent any
environment{
buildnumber = currentBuild.getNumber()
}
tools{
maven 'maven3.8.5'
}
stages {
stage('clone') {
steps {
git 'https://github.com/NageshRep0/maven-web-application.git'
sh 'mvn clean package'
}
}
stage('imageBuild') {
steps {
sh 'docker build -t dockerhandsoff/mavenwebapp:${buildnumber} .'
}
}
stage('imagepush') {
steps {
withCredentials([string(credentialsId: 'mycreds', variable: 'mycreds')]) {
sh 'docker login -u dockerhandsoff -p ${mycreds}'
}
sh 'docker push dockerhandsoff/mavenwebapp:${buildnumber}'
}
}
stage('imagedeployment') {
steps {
sshagent(['63f06107-ba1d-4de8-8e59-a8267ef02444']) {
sh 'ssh -o StrictHostKeyChecking=no ubuntu@172.31.36.211 docker run -d --name
mavenwebapp -p 8080:8080 dockerhandsoff/mavenwebapp:${buildnumber}'
}
}
}
}
}
3) now running the jenkins & build is success π
Authentication & pushing the image in Docker