Day 27 : Jenkins Declarative Pipeline with Docker

In the world of continuous integration and deployment, Jenkins has long been a cornerstone tool for automating workflows. With the rise of containerization technologies like Docker, integrating Docker into Jenkins pipelines has become a common practice. In this blog post, we'll explore how to use Jenkins Declarative Pipeline with Docker to streamline your development and deployment processes.

Jenkins Declarative Pipeline

Jenkins Declarative Pipeline provides a more structured and concise way to define your build, test, and deployment pipelines as code. It allows you to define stages, steps, and post-actions easily, making your pipeline code readable and maintainable.

Docker Build and Run

docker build — you can use sh 'docker build . -t <tag>' in your pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.

docker run: you can use sh 'docker run -d <image>' in your pipeline stage block to build the container.

How will the stages look

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

Task-01

  • Create a docker-integrated Jenkins declarative pipeline

  • Use the above-given syntax using sh inside the stage block

First create an EC2 instance , install Jenkins on it. Access Jenkins using the public IP of the EC2 instance and the 8080 port.

Once you are done with the Jenkins installation and are able to access the same on the browser then open the Jenkins Dashboard and select “New Item”.

Here you need to add the Project Name and then select the project type as “pipeline” in this case because we will be creating the Jenkins File further.

Click on “OK”.

Now we will land into the “Project Configuration” section.

Here you need to go to the “pipeline” section and there you need to select the “Pipeline script” in definition.

Now we will write a basic Pipeline Script for for django app.

pipeline {
    agent any 
    stages {
        stage('Code') { 
            steps {
                git url: 'https://github.com/PrathmeshV2001/django-todo-cicd.git' , branch: 'develop' 
            }
        }
        stage('Build') { 
            steps {
                sh 'docker build . -t django_app_img:latest' 
            }
        }
        stage('Test') { 
            steps {
                echo "Testing" 
            }
        }
        stage('Deploy') { 
            steps {
                sh "docker run -d --name django_react_app_jenkins -p 8000:8000 django_app_img:latest"
            }
        }
    }
}

Click on the save button to save the Pipeline.

Now we can start the build process by manually clicking on the "Build Now".

Once the build is complete you can check the output of the build by clicking on the “Console Output” tab present there.

You can see here the pipeline is running successfully.

Now you check the multi stage view by clicking on the “Full Stage View” in the project main page.

Conclusion

Integrating Docker with Jenkins Declarative Pipeline provides a robust and scalable approach to automate your build, test, and deployment workflows. By following the syntax and examples provided in this post, you can create efficient pipelines that leverage the power of Docker for containerized development and deployment.

I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching .

thank you : )

0
Subscribe to my newsletter

Read articles from Prathmesh Vibhute directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prathmesh Vibhute
Prathmesh Vibhute