DEV Community

Cover image for Setting Up a Production-Ready CI/CD Pipeline with Jenkins & Docker
Yash Sonawane
Yash Sonawane

Posted on

Setting Up a Production-Ready CI/CD Pipeline with Jenkins & Docker

Introduction: Why CI/CD Matters in DevOps

Imagine you're working on a critical software project, and every time a developer pushes new code, it has to be manually tested and deployed. This process is slow, error-prone, and hinders rapid releases. Now, imagine automating this with a Continuous Integration and Continuous Deployment (CI/CD) pipeline using Jenkins and Dockerโ€”ensuring that every code change is automatically tested, built, and deployed.

This blog will walk you through setting up a production-ready CI/CD pipeline using Jenkins & Docker, helping you streamline your DevOps workflow. By the end, you'll have a fully functional pipeline that automates the process from code commit to deployment.

Jenkins CI/CD Pipeline (Image source: Official Jenkins Website)


Step-by-Step Guide: Building the CI/CD Pipeline

Prerequisites

Before we start, ensure you have:

  • A Linux server (Ubuntu 22.04 recommended)
  • Docker and Docker Compose installed
  • Jenkins installed and running
  • A GitHub repository with sample code (Node.js application for this tutorial)

Step 1: Install and Configure Jenkins

1. Install Jenkins

Installing Jenkins

sudo apt update && sudo apt install -y openjdk-11-jdk
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
echo "deb http://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update && sudo apt install -y jenkins
Enter fullscreen mode Exit fullscreen mode

2. Start and Enable Jenkins

sudo systemctl start jenkins
sudo systemctl enable jenkins
Enter fullscreen mode Exit fullscreen mode

3. Access Jenkins Dashboard

Open your browser and go to http://your-server-ip:8080 to complete the setup.

Jenkins Dashboard

Step 2: Install Required Plugins

Navigate to Manage Jenkins > Manage Plugins and install:

  • Pipeline
  • Docker Pipeline
  • Git Plugin
  • Blue Ocean (for visualization)

Step 3: Configure Docker in Jenkins

1. Install Docker

Installing Docker

sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

2. Add Jenkins to Docker Group

sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a Jenkins Pipeline

1. Create a Jenkinsfile in Your Project Repository

Jenkins Pipeline Code

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/your-repo/sample-app.git'
            }
        }
        stage('Build Docker Image') {
            steps {
                sh 'docker build -t myapp:latest .'
            }
        }
        stage('Run Tests') {
            steps {
                sh 'docker run myapp:latest npm test'
            }
        }
        stage('Deploy') {
            steps {
                sh 'docker-compose up -d'
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Create a New Pipeline in Jenkins

  1. Go to Jenkins Dashboard > New Item
  2. Select Pipeline, name it, and click OK
  3. Under Pipeline Definition, choose Pipeline script from SCM
  4. Enter your GitHub repository URL
  5. Save and Build Now

Jenkins Build Success

Troubleshooting Common Issues

  • Jenkins can't access Docker? Run sudo chmod 666 /var/run/docker.sock
  • Pipeline fails at docker build? Ensure the Dockerfile is present in the repo.
  • Permission denied errors? Run sudo chown -R jenkins:jenkins /var/lib/jenkins

Real-World Use Cases & Comparisons

How Companies Use Jenkins & Docker

Company Use Case
Netflix Automates deployments to Kubernetes clusters
Spotify Manages microservices with Docker and Jenkins
Airbnb Uses CI/CD for seamless feature rollouts

Docker vs. Virtual Machines

Feature Docker Containers Virtual Machines
Performance Lightweight, fast Heavy, slower
Isolation Process-level Full OS emulation
Resource Usage Minimal High

Docker vs Virtual Machines


Interactive Learning & Resources


Engaging Conclusion & Call-to-Action

Setting up a Jenkins & Docker CI/CD pipeline is a game-changer for any DevOps engineer. It improves efficiency, reliability, and scalability, ensuring your deployments are smooth and automated.

What do you think? Comment below with your thoughts or challenges! Donโ€™t forget to subscribe for more DevOps content and follow us on LinkedIn & Twitter. ๐Ÿš€


๐Ÿ“š Further Reading

Top comments (0)