Day 27: Jenkins Declarative Pipeline with Docker✨✨
Day#27 Of 90 Days Of Devops Challenge
👋Introduction
Welcome back, developers! It's time to take our Jenkins Declarative Pipeline skills to the next level.
In today's blog, we'll be diving into the exciting world of Docker integration within Jenkins pipelines.
If you're familiar with the basics of Declarative Pipelines and Docker commands, you're in for a treat. If not, don't worry; we'll guide you through the process step by step.
📌Apply your knowledge of Docker's build-and-run functionalities
To execute the Docker build process within your pipeline stage block, you can utilize the following command:
sh 'docker build . -t <tag>'
Ensure that you have Docker installed with the necessary permissions to carry out this operation.
For initiating the Docker run operation within your pipeline stage block to create the container, you can make use of the subsequent command:
sh 'docker run -d <image>'
Note: Please ensure that you replace <tag>
and <image>
with the appropriate tag and image name as per your Docker build and run requirements.
Regarding the structure of your stages, it appears as follows:
stages {
stage('Build') {
steps {
sh 'docker build -t trainwithshubham/django-app:latest'
}
}
}
📖Task-01: Jenkins declarative pipeline with Docker integration
Create a docker-integrated Jenkins declarative pipeline
Use the above-given syntax using
sh
inside the stage blockYou will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2
To create a new Jenkins job with the specified details, follow these steps:
Access Jenkins: Open your web browser and navigate to your Jenkins instance.
Log In: If not already logged in, log in to your Jenkins account.
Dashboard: Once logged in, you should be on the Jenkins dashboard.
Create New Job:
- Look for an option such as "New Item," "Create New Job," or "New Project" on the dashboard. Click on it.
Enter Job Details:
Name: Provide a name for your job. This will be used to identify the job within Jenkins.
Type: Choose "Pipeline" as the job type. This will allow you to define your job's tasks using a pipeline script.
Configure Pipeline:
After selecting "Pipeline," you'll typically see a section where you can define the pipeline configuration.
Depending on your Jenkins setup, you might have different options to define the pipeline script. The most common approaches are:
Pipeline Script: This is a simple script editor where you can write your pipeline script directly.
Pipeline from SCM: You can define your pipeline script in a version control repository (e.g., Git) and have Jenkins fetch and execute it.
Write or configure your pipeline script based on your requirements. A basic pipeline script might look like this:
pipeline {
agent any
stages{
stage ('Code Clone') {
steps {
git url : 'https://github.com/VishalPhadnis/django-todo-cicd.git' , branch : 'develop'
}
}
stage ('Build') {
steps {
sh 'docker build . -t django-todo-cicd:latest'
}
}
stage ('Testing') {
steps {
echo 'testing'
}
}
stage ('Deploy') {
steps {
sh 'docker run -d -p 8000:8000 django-todo-cicd:latest'
}
}
}
}
Save: Once you've configured your pipeline script, save your job configuration.
Build: You can now manually trigger a build of your pipeline job, or set up triggers based on events like code commits.
Check the "Console Output"
Check whether the application is working on port 8000 or not.
You will face errors in case of running a job twice, as the docker container will be already created.
📃Task-02: Improving the Declarative Pipeline of Jenkins with Docker Integration
Create a docker-integrated Jenkins declarative pipeline using the
docker
groovy syntax inside the stage block.You won't face errors, you can Follow this documentation
Complete your previous projects using this Declarative pipeline approach
Straightforwardly, make changes to the script by utilizing the commands docker-compose down and docker-compose up.
Click on "Save" and then click on "Build Now"
Check the "Console Output"
Now you check the multi-stage view by clicking on the “Full Stage View” on the project main page.
Check whether the application is working on port 8000 or not.
🌈Conclusion
In this post, we combined Jenkins Declarative Pipelines and Docker for enhanced CI/CD.
We used the sh
command to run Docker commands within pipeline stages.
Then, we explored Docker Groovy syntax in the script
block for smoother image creation and container handling.
Apply this to past projects for streamlined CI/CD. Embrace Docker's power in your pipelines for a more efficient development journey.
As we continue our #90DaysOfDevOps Challenge journey, remember that Jenkins Pipelines are just one part of the vast DevOps landscape.
Embrace the opportunity to learn and apply new concepts, tools, and practices.
Thank you for joining us on this exciting Day 27 of the challenge. I hope you found the information helpful and insightful.
So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :
Hashnode: vishaltoyou.hashcode.dev
LinkedIn: linkedin.com/in/vishalphadnis
So, Stay in the loop and stay ahead in the world of DevOps!
Happy learning 🚀😊🐧📦🔧🛠️💻💼🔍📈🚀🌟📊📚