Day 27 Task: Jenkins Declarative Pipeline with Docker

Radheya Zunjur
4 min readJul 13, 2023

--

Day 26 was all about a Declarative pipeline, I hope you must have gone through last article. Now it’s time to level up things, let’s integrate Docker and your Jenkins declarative pipeline

Use your Docker Build and Run Knowledge

What is 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.

What is 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’
}
}
}

Tasks

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

Step A) Create a new Jenkins Pipeline with the name “ Day 27-Jenkins Declarative Pipeline with Docker”.

Step B) Go to the Dashboard > Configure > Pipeline > Select Definition as “Pipeline Script” > And enter the following command.

pipeline{
agent any
stages{
stage('Clone'){
steps{
echo 'Cloning the git repo'
git 'https://github.com/LondheShubham153/node-todo-cicd.git'
}
}

stage('build docker'){
steps{
sh 'docker build -t todo-app .'
}

}

stage('Deploy'){
steps {
sh 'docker run -d -p 8000:800 todo-app'
}
}
}
}

Step C) Now go to Jenkins Pipeline and Build the Pipeline.

Step D) In the Pipeline Dashboard, you can see the Stage View as well. Our job is successfully run.

Step E) Let me try to build the same job again. The job has failed as the containers are already and exist for the port and the same is shown in the stage view.

For this, the existing containers need to be deleted manually from the host system. This issue can be solved by including the docker groovy syntax inside stage block.

That’s the next task.

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

To include docker in the stage block, the docker pipeline and docker plugin need to be installed.

Step 1) Go to Dashboard > Manage Jenkins > Manage Plugins > Available Plugins > Select the required plugins ‘Docker’ and ‘Docker Pipeline’> Click on Install without restart.

Step 2) Now let us start writing the Declarative pipeline command. Create a New Pipeline Job named “sample-app” and go to the pipeline script in configuration.

Docker Hub image used:


pipeline {
agent any
stages {
stage(‘Clone the Repo:’) {
steps {
echo ‘Cloning the repository:’
git ‘https://github.com/thesnehasuresh/node-todo-cicd.git’
}
}
stage('Build') {
agent {
docker {
image ‘snehaks/sample_nodejs:latest’
reuseNode true
}
}
steps {
echo “Building app using Docker Image”
}
}
stage(‘Deploy the Application’) {
steps {
sh “docker-compose down”
sh “docker-compose up -d”
}
}
}
}

Save the Code and build the job.

Let’s inspect the new block in Stage.

The stage Build in the Jenkinsfile defines a stage in the pipeline that will build the application.

The agent section of the stage specifies that the stage will be executed in a Docker container.

The image section of the agent specifies that the Docker image to use is snehaks/sample_nodejs:latest.

The reuseNode section of the agent specifies that the Docker container should be reused if possible.

Step 3) The build is successful. Same can be verified in Stage view and Console output as well!

--

--

Radheya Zunjur
Radheya Zunjur

Written by Radheya Zunjur

Database Engineer At Harbinger | DevOps | Cloud Ops | Technical Writer

No responses yet