Open In App

Implementing CI/CD Pipelines with Docker and Jenkins

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Continuous Integration (CI) and Continuous Deployment (CD) have become the backbone of modern software development. They enable teams to deliver code faster, more reliably, and with fewer errors. Automating the process of building, testing, and deploying code allows developers to focus on what matters most: writing quality software.

In this article, we'll discuss how Docker and Jenkins work together to set up and manage a CI/CD Pipeline, helping developers streamline the process of building and deploying applications. By the end of this article, you'll understand the power of Docker Containers and Jenkins automation to create an efficient pipeline, and how they can help you integrate code and deploy applications seamlessly.

Why Combine Docker and Jenkins for CI/CD?

Docker provides a consistent environment by containerizing applications and all their dependencies, making it easy to move applications across different different environments (Development, testing, and production). This consistency ensures that the application runs the same way everywhere, preventing issues like "it works on my machine".

Jenkins, on the other hand, is an automation server that manages your CI/CD pipeline. It automates the entire process of building, testing, and deploying code, allowing you to focus more on writing code and less on manual tasks.

By Combining Docker and Jenkins, you can:

  • Automate the Deployment of application in consistent environment across all stages.
  • Streamline the CI/CD Process, making it faster and more reliable.
  • Reduce errors, as Docker ensures your code runs in the same environment, and Jenkins automates the process of pushing that code to production.

Primary Terminologies

  • Continuous Integration: A practice of automatically integrating the changed code committed by developers to the shared repository after each commit, followed by automated testing to detect issues early
  • Continuous Deployment: A practice of automatically deploying every change that passes through the CI phase into production so that the latest code is always made available to its users.
  • Docker: An open-source platform utilizing containerization to package and deploy an application properly in a consistent way in any environment.
  • Jenkins: An open-source automation server that runs the continuous integration and deployment initiative by automating the process of building, testing, and deploying applications.
  • Pipeline: An automated suite in the sequence for building, testing, and deployment in applications, mostly defined as Jenkinsfile.

How to Implement CI/CD Pipelines with Docker and Jenkins: Step by Step

Now that we understand why Docker and Jenkins are important for CI/CD, Follow the below steps how to set up your CI/CD pipeline using Docker and Jenkins:

Step 1: Install Docker

Install docker by using the following command

sudo yum -y install docker
To install docker in Redhat or CentOS

Start and enable docker

sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Command for starting docker

Step 2: Install Jenkins

Now install jenkins by using following commands

sudo wget -O /etc/yum.repos.d/jenkins.repo \

https://pkg.jenkins.io/redhat-stable/jenkins.repo

sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

sudo yum upgrade

sudo yum -y install java-17*

sudo yum -y install jenkins

Now start and enable jenkins by using following commands

sudo systemctl start jenkins
sudo systemctl enable jenkins
sudo systemctl status jenkins
Command to start and enable jenkins
  • Now navigate to ec2 dashboard and copy public ip of instance and browse it along with port 8080 because jenkins default port number is 8080
  • Copy and paste administration password and unlock jenkins
Jenkins Dashboard

Install Required Plugins

Now Navigate to Manage Jenkins -> Manage Plugins.

Install the following plugins:

  • Docker Pipeline Plugin
Docker Pipeline Plugin

Step 3: Create a Jenkins Pipeline

Create a New Pipeline Job:

  • In Jenkins, click New Item.
  • Enter a name for your job, select Pipeline, and click OK
Create a Jenkins Pipeline

Define Your Pipeline in a Jenkinsfile:

  • Give project description
  • Now go to pipeline section add pipeline script
pipeline {
agent any

stages {
stage('Pull Nginx Image') {
steps {
script {
// Pull the Nginx Docker image
dockerImage = docker.image('nginx:latest')
dockerImage.pull()
}
}
}

stage('Test') {
steps {
script {
// Run tests inside the Nginx container (you may need to adjust this based on your test needs)
dockerImage.inside {
sh 'echo Running tests on Nginx container...'
// Example: Check Nginx version
sh 'nginx -v'
}
}
}
}

stage('Deploy') {
steps {
script {
// Example of deployment step (optional)
// Here, we would normally push to a registry, but Nginx is usually pulled directly from Docker Hub.
echo 'Deployment step for Nginx container (usually unnecessary as Nginx is directly pulled)'
}
}
}
}
}
Pipeline Script to Pull Nginx Docker Image

Step 5: Build Pipeline

Now Build pipeline

Status of demo-pipeline

Here we can see the output of the pipeline

Pull Nginx Image


Deployment Stage for Nginx Container

Below figure shows that pipeline view of the job

Pipeline view of the job

Best Practices for Using CI/CD Pipelines with Docker and Jenkins

To ensure your pipeline runs smoothly and efficiently, follow the best practices:

  • Automate Testing: Make sure that your pipeline includes automated test to catch bugs early.
  • Use Version Control: Keep your Jenkinsfile and Docker Configurations under version control.
  • Monitor your Pipeline: Set up monitoring to track the performance and catch any bottlenecks in the pipeline.

Conclusion

The Docker and Jenkins CI/CD pipelines simplify and automate the development process. By using these tools, teams can minimize development cycles, ensure reliable tests, and achieve consistent deployment across environments.

Docker’s containerization ensures apps run seamlessly from development to production, while Jenkins automates building, testing, and deploying those apps. Together, they enable continuous integration and deployment, helping to reduce time-to-market and improve software quality.

For any DevOps team, understanding and implementing CI/CD pipelines with Docker and Jenkins is essential. With proper setup, software is always ready for deployment, allowing for quick iterations and continuous delivery of value to users. Ultimately, mastering CI/CD helps teams automate workflows, prevent errors, and deliver high-quality software faster


Next Article
Article Tags :

Similar Reads