Day 27 - Jenkins Declarative Pipeline with Docker

Day 27 - Jenkins Declarative Pipeline with Docker

DOCKER AND JENKINS INTEGRATION

Let's integrate Docker and Jenkins.

DOCKER HUB

Docker Hub is a container registry built for developers and open source contributors to find, use, and share their container images.

Step 1: Create a Docker Hub Account. Refer How to create account on Docker Hub.

Step 2: Create an "Access token" for your account. Go to "Your Account".

Step 3: Now, Go to "Security" and click on "New Access Token".

Step 4: Click on "Generate".

Step 5: You will see the below page with you token generated. Copy it and save it somewhere.

Step 6: So, here is our "Access token" of Docker Hub.

Step 7: You can go to "Your profile" and in "Repositories", you can see as of now we don't have images on the Docker Hub.

JENKINS

Step 1: Login to your AWS account and start your instance and connect your terminal.

Step 2: Login to Jenkins via browser using <Your_Public_IP:8080>.

Step 3: Create on "New Item" and create new pipeline named as "Node-todo".

Step 4: Now, In Jenkins go to "Manage Jenkins". And Go to "Credentials".

Step 5: Click on "System".

Step 6: Click on "Global Credentials".

Step 7: Click on "Add Credentials" to add Docker Hub credential here.

Step 8: Select Username and Password and add your Docker Hub Username, In password, your Access token which you have generated in Docker Hub, give some ID as shown below and then click on "Create".

GITHUB

Step 1: Setup "Webhook" in your Repository.

Step 2: In your Repository, add Jenkinsfile.

pipeline {
    agent any
    environment {
        appName = "node-app"
          dockerHub = "smritisharma21"
    }
    stages { 
        stage("code"){
            steps{
                echo 'Cloning the Repository....'
                git url: "https://github.com/Smriti0821/node-todo-cicd.git",branch:"Test"
                echo 'Repository Cloned!!'
            }
        }
        stage("Building Images and list images"){
            steps{
                echo 'Building Image from Dockefile...'
                sh "docker build . -t ${env.appName}"
                echo 'Image Build Completed!!'
                sh "docker images"
            }
        }
        stage("Pushing Image to Docker Hub"){
            steps{
                echo 'Logging to Docker Hub...'
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                echo 'Login Succeeded!!!'
                sh "docker tag ${env.appName}:latest ${env.dockerHubUser}/${env.appName}:latest"
                echo 'Pushing Image to Docker Hub...'
                sh "docker push ${env.dockerHubUser}/${env.appName}:latest"
                echo 'Image pushed to Docker Hub!!!'
                }
            }
        }
        stage("Deploy"){
            steps{
                echo 'Deployment Started....'
                sh "docker-compose down && docker-compose up -d"
                echo 'Deployment Completed!!!'
            }
        }
    }
}

Step 3: Make some changes in your repository in any file and commit it.

GITHUB INTEGRATION WITH JENKINS

Step 1: You will see the pipeline getting triggered.

Step 2: Here, you can see our pipeline is successful.

Step 3: You can see the detailed output in "Console Output" as shown below.

DOCKER HUB INTEGRATION WITH JENKINS

As you can see, Image has been pushed to docker using Jenkins pipeline.

The below stage in Jenkinsfile, let's the Jenkins login to Docker Hub and push the image to it.

stage("Pushing Image to Docker Hub"){
            steps{
                echo 'Logging to Docker Hub...'
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                echo 'Login Succeeded!!!'
                sh "docker tag ${env.appName}:latest ${env.dockerHubUser}/${env.appName}:latest"
                echo 'Pushing Image to Docker Hub...'
                sh "docker push ${env.dockerHubUser}/${env.appName}:latest"
                echo 'Image pushed to Docker Hub!!!'
                }
            }
        }
withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")])

dockerHub is the ID which we have given in "Manage Jenkins" under Credentials. and username is "smritisharma21"
environment {
        appName = "node-app"
          dockerHub = "smritisharma21"
    }

So the above WithCredentials Syntax is fetching username and password form Jenkins which we have store in "Credentials".

You can access your application via browser using <Your_Public_IP:port>.

CONCLUSION

In conclusion, the integration of Docker and Jenkins offers a powerful solution for streamlining and automating the software development lifecycle.

Docker provides containerization, ensuring consistency across different environments, while Jenkins facilitates continuous integration and delivery, automating the build, test, and deployment processes.

This combination enhances efficiency, scalability, and collaboration within development teams, ultimately leading to faster and more reliable software delivery. The seamless integration of Docker and Jenkins significantly contributes to a more agile and DevOps-oriented approach to software development.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

👆The information presented above is based on my interpretation. Suggestions are always welcome.😊

~Smriti Sharma✌