Skip to main content

Command Palette

Search for a command to run...

Day 27: Jenkins Declarative Pipeline with Docker

Updated
3 min read
Day 27: Jenkins Declarative Pipeline with Docker
P

Greetings! 👋 I'm Priyadarshi Ranjan, a dedicated DevOps Engineer embarking on an enriching journey. Join me as I delve into the dynamic realms of cloud computing and DevOps through insightful blogs and updates. 🛠️ My focus? Harnessing AWS services, optimizing CI/CD pipelines, and mastering infrastructure as code. Whether you're peers, interns, or curious learners, let's thrive together in the vibrant DevOps ecosystem. 🌐 Connect with me for engaging discussions, shared insights, and mutual growth opportunities. Let's embrace the learning curve and excel in the dynamic realm of AWS and DevOps technology!

Welcome back, DevOps enthusiasts! Yesterday, we delved into the world of Jenkins Declarative Pipelines. Today, we're taking it a step further by integrating Docker into our Jenkins pipeline. This combination is powerful, allowing us to build, test, and deploy applications consistently and efficiently.

Why Integrate Docker with Jenkins?

Docker allows you to package your applications with all dependencies into a standardized unit, ensuring consistency across various environments. Integrating Docker with Jenkins automates the process of building and deploying these containers, making your CI/CD pipeline robust and reliable.

Prerequisites

Before we start, ensure you have:

  1. Docker installed and configured with the necessary permissions.

  2. Jenkins installed and running.

  3. Basic understanding of Jenkins Declarative Pipeline syntax.

Creating a Docker-Integrated Jenkins Declarative Pipeline

Task 01: Basic Integration

Let's start with a simple example where we use Docker commands within our Jenkins pipeline.

Pipeline Syntax

Here’s how your Jenkinsfile would look:

groovyCopy codepipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh 'docker build -t darshif5/django-app:latest .'
            }
        }
        stage('Run') {
            steps {
                sh 'docker run -d darshif5/django-app:latest'
            }
        }
    }
}

Explanation

  • agent any: Runs the pipeline on any available agent.

  • stage('Build'): Builds the Docker image using the docker build command.

  • stage('Run'): Runs the Docker container using the docker run command.

Common Issue: Running the Job Twice

If you run this pipeline twice, you might encounter errors because the Docker container is already created. To handle this, you can remove the existing container before creating a new one.

Task 02: Using Docker Groovy Syntax

Jenkins provides a more seamless way to handle Docker within pipelines using its Groovy syntax.

Improved Pipeline Syntax

Here's an enhanced version of the pipeline using the Docker Groovy syntax:

groovyCopy codepipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    docker.build('darshif5/django-app:latest')
                }
            }
        }
        stage('Run') {
            steps {
                script {
                    def app = docker.image('darshif5/django-app:latest')
                    app.run('-d')
                }
            }
        }
    }
}

Explanation

  • script { docker.build('trainwithshubham/django-app') }: Builds the Docker image using Jenkins' Docker DSL.

  • def app = docker.image('trainwithshubham/django-app'): References the Docker image.

  • app.run('-d'): Runs the Docker container in detached mode.

Real-Life Example

Imagine you have a Django application that needs to be built and deployed. Using the above pipelines, you can automate the process from building the Docker image to running it as a container. This automation ensures that every time you push code changes, your application is built and deployed consistently.

Conclusion

Integrating Docker with Jenkins Declarative Pipelines enhances your CI/CD process by providing a consistent environment for building and deploying applications. While using simple Docker commands is a good start, leveraging Jenkins' Docker Groovy syntax offers a more robust and error-free solution.

By mastering these techniques, you're well on your way to becoming a proficient DevOps engineer. Keep experimenting, learning, and building!

Happy DevOps-ing!

Connect and Follow Me on Socials

LINKDIN | GITHUB |TWITTER

More from this blog

Priyadarshi Ranjan

71 posts

As a DevOps engineer, I leverage automation and continuous integration to streamline development workflows, ensuring robust and scalable deployments.