Jenkins Declarative Pipeline with Docker

Jenkins Declarative Pipeline with Docker

Day 26 was all about a Declarative pipeline; now it's time to level up things; let's integrate Docker and our Jenkins declarative pipeline.

To integrate Docker and our Jenkins Declarative Pipeline, we will make use of our docker build and docker run knowledge.

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 the correct permissions.

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

Here is an example of how the stage will look like:

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

Task-1

  • Create a docker-integrated Jenkins declarative pipeline

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

  • You will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2

    First, we sign in to the Jenkins dashboard where you will click on the "New Item", provide a name for your project, and click on "Pipeline", then "ok" to create a new Jenkins Pipeline.

Then you will be directed to the configuration section where you will go to the pipeline section to write a pipeline script

 pipeline {
     agent any
     stages {
         stage('Clone Code') {
             steps {
                 // Build steps go here
                 git url: 'https://github.com/Ronke-Akinyemi/node-todo-cicd.git', branch: 'master'
             }
         }
         stage("Build"){
             steps{
                 sh '/usr/local/bin/docker build . -t node-todo-cicd'
             }
         }
         stage("Run"){
             steps{
                 sh '/usr/local/bin/docker run -d -p 8001:8000 node-todo-cicd'
             }
         }

     }
 }

Click on "save". Then by the side click on "Build Now"

The job was run successfully. Click on "Full stage view" and below is the stage view.

  • You will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2

Task-2

  • Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block.

  • You won't face errors

  • Complete your previous projects using this Declarative pipeline approach

Now, you can create a new job or make use of the existing one.

Click on "Save" and click on build now

pipeline {
    agent any
    environment {
        PATH = "$PATH:/usr/local/bin"
    }
    stages {
        stage('Clone Code') {
            steps {
                git url: 'https://github.com/Ronke-Akinyemi/node-todo-cicd.git', branch: 'master'
            }
        }
        stage("Build") {
            steps {
                sh '/usr/local/bin/docker build . -t node-todo-cicd'
            }
        }
        stage("Deploy") {
            steps {
                sh '/usr/local/bin/docker-compose down && docker-compose up -d'
            }
        }
    }
}

When this pipeline is run, it won't encounter any error because included is a step that will shut down existing containers before creating new ones. We can now run the job multiple times.

I'm open to receiving any suggestions, feedback, or further insights regarding the topic discussed in this article. Please share your thoughts or ideas, as I'm always open to learning. Thank you.