Skip to main content

Command Palette

Search for a command to run...

Jenkins Declarative Pipeline with Docker

Published
3 min read
A

👩‍💻 Hey there! I'm a DevOps engineer and a tech enthusiast with a passion for sharing knowledge and experiences in the ever-evolving world of software development and infrastructure. As a tech blogger, I love exploring the latest trends and best practices in DevOps, automation, cloud technologies, and continuous integration/delivery. Join me on my blog as I delve into real-world scenarios, offer practical tips, and unravel the complexities of creating seamless software pipelines. Let's build a strong community of tech enthusiasts together and embrace the transformative power of DevOps!

In our quest to master the world of DevOps, we're diving into the exciting realm of Jenkins Declarative Pipelines integrated with Docker. If you've been following along, Day 28 introduced you to Declarative Pipelines; now, let's level up by adding the power of Docker to the mix.

Docker Integration: A Game-Changer 🐳

Docker has revolutionized how we develop, ship, and run applications. Integrating Docker with your Jenkins pipelines brings efficiency and consistency to your CI/CD workflows. Let's see how to seamlessly merge these technologies.

Utilizing Docker Build and Run Knowledge 🏗️

docker build: In your pipeline, use sh 'docker build . -t <tag>' to execute the Docker build command. Make sure Docker is installed and properly configured on your Jenkins environment.

docker run: Within your pipeline stage block, leverage sh 'docker run -d <image>' to run the built container.

Defining Pipeline Stages 🚀

Here's a glimpse of how your pipeline stages could look:

stages {
    stage('Build') {
        steps {
            sh 'docker build -t django-app:latest'
        }
    }
}

Task: Building Your Docker-Integrated Pipeline 🛠️

Step 1: Log in to your Jenkins dashboard and navigate to "New Item" to create a new pipeline job.

Step 2: In the pipeline configuration, select "Pipeline" and provide a name for your job.

Step 3: Give the description of this job and under the "General" section, choose "Github project" and provide the repository URL.

Step 4: Define Pipeline Script - Under the "Pipeline" section, choose pipeline script and provide the below script.

pipeline {
    agent any
    stages{
        stage ('Code Clone') {
            steps {
                git url : 'https://github.com/Vasishtha15/node-todo-cicd.git' , branch : 'master'
            }
        }
        stage ('Build') {
            steps {
                sh 'docker build . -t  node-todo-cicd:latest'
            }
        }
        stage ('Testing') {
            steps {
                echo 'testing'
            }
        }
        stage ('Deploy') {
            steps {
                sh 'docker run -d -p 8000:8000 node-todo-cicd:latest'
            }
        }
    }
}

Step 6: Save and run the pipeline job. Jenkins will execute the Docker build command, creating a Docker image tagged as "node-todo-cicd:latest"

Now you can manually trigger a build of your pipeline job, or set up triggers based on events like code commits.

Step 7: Check the console output:

You will face errors in case of running a job twice, as the docker container will be already created.

Step 8: Make changes to the script by utilizing the commands docker-compose down and docker-compose up.

Step 9: Click the "Save" button, followed by clicking on "Build Now."

Step 10: Access the "Console Output" section.

Step 11: Verify the multi-stage view by selecting "Full Stage View" located on the main page of the project.

Step 12: Confirm whether the application is operational on port 8000.

Conclusion🌟

By following the provided steps and leveraging Docker's capabilities, you've expanded your DevOps toolkit. The marriage of containerization and pipeline orchestration elevates your automation prowess.

Incorporate this approach into your previous projects to achieve a more streamlined CI/CD process. Embrace the capabilities of Docker within your pipelines, enhancing the efficiency of your development journey.

#90DaysOfDevOps #Jenkins #DockerIntegration #CI/CD #DevOpsJourney #ContinuousIntegration #ContinuousDeployment

#90daysOfDevOps

Part 29 of 37

"Join us on an epic DevOps journey! Our blog series, #90DaysOfDevOps, explores the ins and outs of DevOps practices, tools, and success stories. Get ready to level up your skills! 🚀💻 #DevOps"

Up next

Jenkins Master & Agents

# Embrace Scalability with Jenkins Agents 🚀

More from this blog

DevOps & AWS Essentials

38 posts