Open In App

Implementing CI/CD Pipelines with Docker and Jenkins

Last Updated : 27 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Modern software development has CI and CD pipelines as its core practices, fostering the delivery of code faster, more reliably, and with fewer errors. CI/CD pipelines generally automate the process of building, testing, and deployment to ease the development process, so that the lifecycle becomes smooth through code changes and their integration and deployment.

This in combination of Docker and Jenkins will prove a lot more powerful in automating CI/CD. Docker ensures that the application runs in the same working environment, and Jenkins automates every phase of the pipeline—starting with fetching the most recent code from a repository to deploying the application into a production environment.

The article shows how CI/CD pipelines are set up using Docker with Jenkins, how the pipeline is used, and elaborated examples. By the end of this tutorial, you should have a clear understanding of how to set up and manage CI/CD pipelines that leverage the power of Docker containers and Jenkins automation.

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.

Step-by-Step Process for Implementing CI/CD Pipelines with 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

Conclusion

The Docker and Jenkins CI/CD pipelines are a great way to automate and ease the development process. In the present scenario, a bundle of such integrated tools can ensure shorter development cycles, improve the reliability of tests, and smoothen deployment without differences or inconsistencies across any environment.

The containerization technology developed by Docker ensures that applications run consistently from development through production. Jenkins, on the other hand, provides an automation framework to build, test, and deploy those applications. These join forces in enabling one continuous integration and deployment process to reduce market time with better software quality.

Any team of developers that aims at embracing DevOps really needs to understand the key terminologies and steps that are in place to have these pipelines set up. With the correct configuration, your software is always in a state where it can be deployed; as a consequence, you enable fast iterations and the continuous delivery of value to your users.

To put it in other words, mastering CI/CD with Docker and Jenkins enables teams to automate workflows, prevent errors, and deliver high-quality software faster. This makes CI/CD a cornerstone of effective software development in today's fast-paced world


Next Article
Article Tags :

Similar Reads