Day 27- Elevating Jenkins Declarative Pipeline with Docker Integration  (Dec 23, 2023)

Day 27- Elevating Jenkins Declarative Pipeline with Docker Integration (Dec 23, 2023)

Introduction:

As we continue our #90DaysOfDevOps Challenge, Day 27 introduces a powerful enhancement to our Jenkins Declarative Pipeline—integration with Docker. Dockerizing your applications simplifies deployment and ensures consistency across different environments. In this blog post, we'll walk through two tasks to seamlessly integrate Docker into our Jenkins Declarative Pipeline.

Task-01: Integrating Docker with Jenkins Declarative Pipeline

Let's begin by incorporating Docker commands within our Declarative Pipeline using the 'sh' step inside the stage block. This approach involves manually handling potential errors when running the job twice due to pre-existing Docker containers.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'docker build -t hassanb111/django-app:latest .'
            }
        }
    }
}

In this snippet, we use 'docker build' to create an image tagged as 'trainwithshubham/django-app:latest.' However, running the job twice may result in errors as the Docker container might already exist. Let's address this in Task-02.

Task-02: Docker Integration with Jenkins Declarative Pipeline (Error-Free)

To eliminate potential errors when running the job multiple times, we can leverage Docker's Groovy syntax within the stage block. This syntax enables cleaner and more error-resistant Docker integration.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    docker.image('hassanb111/django-app:latest').build()
                }
            }
        }
    }
}

By using the 'docker.image().build()' syntax, we instruct Jenkins to intelligently handle Docker container creation. This approach eliminates errors that might occur when attempting to build a Docker image that already exists.

Complete Your Previous Projects:

Now that we have Dockerized our Jenkins Declarative Pipeline, it's time to revisit and enhance our previous projects using this streamlined approach. Whether you've worked on web applications, micro services, or any other project, the integration of Docker with Jenkins enhances the consistency and reproducibility of your deployment processes.

Reflecting on #90DaysOfDevOps:

As we reach the fourth week of the #90DaysOfDevOps Challenge, take a moment to reflect on your journey. Embracing DevOps practices, Jenkins, and now Docker, you've undoubtedly expanded your skill set and refined your understanding of CI/CD pipelines. Share your thoughts on LinkedIn using the challenge hashtag, and connect with fellow learners to exchange insights and experiences.