Jenkins Declarative Pipeline with Docker

Jenkins Declarative Pipeline with Docker

·

2 min read

Table of contents

In the last blog, we looked into How to create a Declarative pipeline. Now we will integrate docker into the pipeline. This will be done by adding the commands in the stages syntax of the pipeline.

Alright then let's get into it.

Task-01

  • Create a docker-integrated Jenkins declarative pipeline

We have to follow the same steps that we did in the last one to create a pipeline.

Rest should be left as it is

  • Use the above-given syntax using sh inside the stage block
pipeline {
    agent any

    stages {
        stage('Code Clone') {
            steps {
                git url: 'https://github.com/pmgoriya/node-todo-cicd'
            }
        }
         stage('build') { 
             steps {
                sh 'docker build -t node-todo-cicd:latest .'
             }
         }
         stage('Deploy') {
             steps {
                 sh 'docker run -d -p 8000:8000 node-todo-cicd:latest'
             }
         }
        }
    }

Let's build it Now

  • 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

Build it for one more time to see the error

Go to the Console output and see what the error is all about.

Here the daemon is telling that the port is occupied by another container.
To solve this we need to run this through Docker-compose

Task-02

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

    We will use docker-compose here

Our syntax will be :

pipeline {
    agent any

    stages {
        stage('Code Clone') {
            steps {
                git url: 'https://github.com/pmgoriya/node-todo-cicd'
            }
        }
         stage('build') { 
             steps {
                sh 'docker build -t node-todo-cicd:latest .'
             }
         }
         stage('Deploy') {
             steps {
                 sh 'docker-compose down'
                 sh 'docker-compose up -d'
             }
         }
        }
    }

Before proceeding make sure to delete all the previous docker images and docker containers that are not required through the terminal using the commands :

#To delete all containers including its volumes use,

docker rm -vf $(docker ps -aq)
# To delete all the images,

docker rmi -f $(docker images -aq)

Note : this deletes all the previous containers and images.

Now let's build it

Even after doing two builds one after another there wasn't any issue because of docker-compose. It first builds down and then again builds up.
For more refer to my blog on Docker-compose.

  • Complete your previous projects using this Declarative pipeline approach

Now the node-todo-cicd project has been run through this approach.

Thank you for following up here.Hope it added valuable things to your understanding. For more such content do follow me on my handles.
Happy coding!