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

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
Set Up Jenkins:
Ensure you have Jenkins installed and running.
Install the Docker Pipeline plugin in Jenkins.
Create a Jenkins Pipeline Job:
Open Jenkins.
Click on
New Itemand selectPipeline.Name your job and click
OK.
Configure the Pipeline:
- In the pipeline configuration, select
Pipeline scriptand enter the following script.
- In the pipeline configuration, select
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 anydirective 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
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.
Create a Jenkins Pipeline Job:
Open Jenkins.
Click on
New Itemand selectPipeline.Name your job and click
OK.
Configure the Pipeline:
- In the pipeline configuration, select
Pipeline scriptand enter the following script.
- In the pipeline configuration, select
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.buildto build the Docker image.The
customImage.insideblock 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:




