In the world of continuous integration and continuous delivery (CI/CD), Jenkins has emerged as a powerful automation tool for building, testing, and deploying applications. One of the most popular ways to define pipelines in Jenkins is through Declarative Pipelines. When combined with Docker, Jenkins Declarative Pipelines can take your CI/CD process to the next level by providing a consistent and isolated environment for your builds and deployments.
In this blog post, we will explore the concept of Jenkins Declarative Pipelines with Docker, discussing its benefits, how to set it up, and some practical examples to demonstrate its power.
Understanding Jenkins Declarative Pipelines
- Jenkins Declarative Pipelines allow you to define your build and deployment process using a domain-specific language (DSL). This DSL is easy to read, write, and maintain, making it accessible to both beginners and experienced Jenkins users. Declarative Pipelines encourage best practices by structuring your pipeline into stages, steps, and post-build actions and the following pipeline is
pipeline {
agent any
stages{
stage("code") {
steps{
echo "code cloned"
}
}
stage("build") {
steps{
echo "building the image"
}
}
stage("pushing into docker hub") {
steps{
echo "pushing the image into docker hub"
}
}
stage("deploy") {
steps{
echo "deploying the image"
}
}
}
}
Why Docker with Jenkins?
- Docker is a containerization platform that allows you to package your applications and their dependencies into lightweight, portable containers. Integrating Docker with Jenkins provides several advantages:
Consistency: Docker containers provide a consistent runtime environment, ensuring that your builds and deployments run the same way in development, testing, and production.
Isolation: Each Docker container is isolated from its host and other containers, preventing conflicts between different projects or builds.
Reproducibility: Docker images are versioned and can be stored in registries, making it easy to reproduce builds and deployments at any point in time.
Scalability: Docker containers can be scaled horizontally to handle increased workloads, which is essential for continuous integration and delivery.
Setting Up Jenkins Declarative Pipelines with Docker
Now, let's walk through the steps to set up Jenkins Declarative Pipelines with Docker:
First, you have to install Jenkins using this blog
Make sure you have to add Jenkins and $USER in the docker group
Step 1: Go to your Jenkins dashboard, click on New item give a name and select Pipeline.
Step 2: write a description and Scroll down to check on git and paste URL of your GitHub repository
Step 3: Now write a groovy syntax of the pipeline
pipeline {
agent any
stages{
stage("code") {
steps{
echo "code cloned"
}
}
stage("build") {
steps{
echo "building the image"
}
}
stage("deploy") {
steps{
echo "deploying the image"
}
}
}
}
Step 4: Now Save it and click on Build now to check if it's working, as now you see we have successfully built the structure
Step 5: come back to configure and pass commands in the pipeline script to clone the GitHub repo, build the docker image, push it to the docker hub and finally deploy it In this, we use "sh" to write shell commands in Groovy syntax and click on save
pipeline {
agent any
stages{
stage("code") {
steps{
echo "code cloned"
git url: "https://github.com/nallabellisriparthu/django-todo-cicd.git", branch: "develop"
}
}
stage("build") {
steps{
echo "building the image"
sh "docker build . -t django-todo-app"
}
}
stage("deploy") {
steps{
echo "deploying the image"
sh "docker run -d -p 8000:8000 django-todo-app:latest"
}
}
}
}
Step 6: Open your AWS console in the inbound rules and add port no 8000
Happy Learning
Thanks For Reading! :)
-Sriparthu๐๐ฅ