Skip to main content

Command Palette

Search for a command to run...

Integrating Docker with Jenkins Declarative Pipelines : A Step-by-Step Guide

Updated
3 min read
Integrating Docker with Jenkins Declarative Pipelines : A Step-by-Step Guide
U

Exploring the world of DevOps 🌐.


Task-01 : Create a Docker-Integrated Jenkins Declarative Pipeline

In this task, we will create a Jenkins declarative pipeline that integrates Docker. The pipeline will build and run Docker containers using shell commands.

Step-by-Step Instructions

  1. Set Up Jenkins:

    • Ensure you have Jenkins installed and running.

    • Install the Docker Pipeline plugin in Jenkins.

  2. Create a Jenkins Pipeline Job:

    • Open Jenkins.

    • Click on New Item and select Pipeline.

    • Name your job and click OK.

  3. Configure the Pipeline:

    • In the pipeline configuration, select Pipeline script and enter the following script.

Jenkinsfile for Task-01

pipeline {
    agent any 
    stages {
        stage('Build') {
            steps {
                sh 'docker build -t trainwithshubham/django-app:latest .'
            }
        }
        stage('Run') {
            steps {
                sh 'docker run -d --name django-app trainwithshubham/django-app:latest'
            }
        }
    }
}

Explanation:

  • Agent Section: The agent any directive indicates that the pipeline can run on any available Jenkins agent.

  • Build Stage: Uses sh 'docker build -t trainwithshubham/django-app:latest .' to build the Docker image.

  • Run Stage: Uses sh 'docker run -d --name django-app trainwithshubham/django-app:latest' to run the container in detached mode.

Common Issues:

  • Container Conflict: Running the job multiple times may result in errors due to the existing container. This occurs because Docker cannot create a container with the same name if it already exists.

Task-02 : Use Docker Groovy Syntax for Jenkins Pipeline

To avoid container conflicts and streamline the pipeline process, we will use the Docker Groovy syntax provided by Jenkins.

Step-by-Step Instructions

  1. Set Up Jenkins for Docker:

    • Ensure you have Docker installed and running with the correct permissions.

    • Verify that Jenkins has the necessary permissions to run Docker commands.

  2. Create a Jenkins Pipeline Job:

    • Open Jenkins.

    • Click on New Item and select Pipeline.

    • Name your job and click OK.

  3. Configure the Pipeline:

    • In the pipeline configuration, select Pipeline script and enter the following script.

Jenkinsfile for Task-02

pipeline {
    agent {
        docker {
            image 'trainwithshubham/django-app:latest'
            reuseNode true
        }
    }
    stages {
        stage('Build') {
            steps {
                script {
                    def customImage = docker.build("trainwithshubham/django-app:latest")
                    customImage.inside {
                        sh 'echo "Docker Image Built and Running"'
                    }
                }
            }
        }
    }
}

Explanation:

  • Agent Section: Specifies the Docker image (trainwithshubham/django-app:latest) and enables node reuse (reuseNode true).

  • Build Stage:

    • Uses docker.build to build the Docker image.

    • The customImage.inside block runs commands inside the built container.

Benefits:

  • Avoids Errors: By using the Docker Groovy syntax, we avoid errors caused by existing containers.

  • Streamlined Pipeline: The pipeline becomes simpler and more efficient with built-in Docker support.

Conclusion

Integrating Docker with Jenkins declarative pipelines enhances your CI/CD process, making deployments more efficient and reliable. By following this comprehensive guide, you should be able to set up and run Docker-integrated pipelines seamlessly.

References:

More from this blog

DevOps

65 posts