Meta Title: Building a CI/CD Pipeline with Docker and Jenkins: A Comprehensive Guide
Meta Description: Learn how to create a robust CI/CD pipeline using Docker with Jenkins. This extra-large guide provides an in-depth, step-by-step approach to implementing CI/CD with a friendly and approachable tone.
Create a CI/CD Pipeline Using Docker with Jenkins: The Ultimate Guide
Welcome to a journey where we delve into one of the most essential aspects of modern software development: CI/CD pipelines. Together, we’ll explore Docker and Jenkins and see how they can come together to automate your build, test, and deployment processes. Just imagine me as a friendly father figure walking you through this; I’m here to guide, to support, and to make sure that by the end, you’ll be comfortable with setting up your own CI/CD pipeline.
In this guide, I’ll explain everything you need to know—right from setting up the basics to overcoming the hurdles that might come along the way. Grab a cup of tea, and let’s get started!
Introduction to CI/CD and Its Importance
Continuous Integration (CI) and Continuous Deployment/Delivery (CD) are methodologies that bring efficiency, reliability, and speed to software development. Think of CI/CD as the backbone of your development workflow—allowing developers to continuously merge their changes, test them, and deploy to production without the headache of manual processes. Imagine having to do all of these manually—it would feel like carrying a heavy load up a hill every single day!
Why CI/CD? Well, as developers, we all want our code to reach production quickly and without errors. CI/CD helps streamline that journey, ensuring quality and reliability while keeping the process smooth and automated.
What is Docker and Jenkins?
Docker
Docker is a platform that allows developers to package their applications and dependencies into containers. Think of a container as a box that contains everything an application needs to run, regardless of where it’s deployed. Imagine putting your entire kitchen into a box, so you can cook a meal anywhere in the world without worrying if they have the right utensils or spices—that’s Docker for you.
Jenkins
Jenkins is a popular open-source automation server that enables developers to build, test, and deploy applications automatically. It’s like having a butler for your software—managing all the tedious tasks, so you don’t have to. Jenkins, when paired with Docker, gives you the power to run your builds and tests in isolated environments—ensuring consistency and reducing “it worked on my machine” problems.

Setting Up Your Environment
Before we start building our CI/CD pipeline, let’s get all the tools and environments set up. This is like setting up the workbench before starting a project—everything needs to be in place for smooth progress.
Prerequisites
- A computer or virtual machine with Ubuntu 22.04 or similar.
- Basic knowledge of Linux commands.
- Administrative privileges (you’ll need to install packages).
Step 1: Install Java
Jenkins requires Java to run, so the first step is to ensure you have Java installed.
sudo apt update
sudo apt install openjdk-11-jdk -y
Run java -version
to verify the installation.
Installing Jenkins
Now that Java is ready, we’ll install Jenkins.
Step 2: Install Jenkins
- Add the Jenkins repository key to your system:
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee "/usr/share/keyrings/jenkins-keyring.asc"
- Add the Jenkins repository:
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | sudo tee "/etc/apt/sources.list.d/jenkins.list" > /dev/null
- Update your package list and install Jenkins:
sudo apt update
sudo apt install jenkins -y
- Start and enable Jenkins:
sudo systemctl start jenkins
sudo systemctl enable jenkins
Step 3: Access Jenkins Dashboard
Once installed, Jenkins runs on port 8080. To access it, go to http://your-server-ip:8080
in your browser.
Docker Installation and Integration
Step 4: Install Docker
Now, let’s get Docker installed on our system.
- Update your package list and install necessary dependencies:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
- Add Docker’s official GPG key and repository:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) stable”
“`
- Install Docker:
sudo apt update
sudo apt install docker-ce -y
Step 5: Add Jenkins User to Docker Group
To allow Jenkins to run Docker commands, we need to add the Jenkins user to the Docker group:
sudo usermod -aG docker jenkins
Restart Jenkins to apply changes:
sudo systemctl restart jenkins
Creating Your First Jenkins Job
Step 6: Set Up a Freestyle Job
Now that Jenkins is installed and Docker is configured, let’s set up our first Jenkins job. This will be the foundation for our CI/CD pipeline.
- Open Jenkins Dashboard.
- Click on New Item.
- Enter an item name, select Freestyle project, and click OK.
Here, we’ll be defining the steps to pull the code from a repository, build the Docker image, and push it to Docker Hub or deploy it directly.
Example Script for Jenkins Job
Here’s a sample script you can use in the Build step to create a Docker image:
docker build -t your-dockerhub-username/your-app-name:$BUILD_NUMBER .
docker push your-dockerhub-username/your-app-name:$BUILD_NUMBER
Bold Reminder: Make sure to replace your-dockerhub-username
and your-app-name
with your actual Docker Hub username and application name.
Setting Up a Simple Dockerized Application
Step 7: Create a Dockerfile
We’ll create a Dockerized application—a simple Node.js app—to demonstrate how Jenkins can automate the entire build and deployment process.
- Create a directory for your application:
mkdir my-docker-app
cd my-docker-app
- Create a file named
Dockerfile
with the following content:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
Step 8: Create server.js
This is the main server file for our simple Node.js app.
const http = require('http');
const port = 8080;
const requestHandler = (request, response) => {
response.end('Hello, Jenkins and Docker!');
};
const server = http.createServer(requestHandler);
server.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Space for image suggestion: An image showing a Docker container running a Node.js application.
Building a CI/CD Pipeline
Step 9: Define the Pipeline Script
We’ll now create a Jenkins Pipeline script to automate building, testing, and deploying our application.
- In Jenkins, click New Item.
- Name it and select Pipeline.
- In the pipeline section, select Pipeline script.
- Use the following script:
pipeline {
agent { docker { image 'node:14' } }
environment {
DOCKERHUB_CREDENTIALS = credentials('dockerhub')
}
stages {
stage('Build') {
steps {
sh 'npm install'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
stage('Docker Build & Push') {
steps {
script {
docker.build("your-dockerhub-username/your-app-name:${env.BUILD_NUMBER}").push()
}
}
}
}
}
Friendly Note: This script performs three major tasks—building, testing, and pushing the Docker image to Docker Hub.
Testing and Quality Assurance
Testing is an essential part of CI/CD. Jenkins makes it easy to integrate different types of tests, such as unit tests, integration tests, and even security checks.
Step 10: Running Unit Tests
Modify your server.js
code to add a simple test. Then integrate a unit test tool like Mocha or Jest to ensure your code’s integrity.
Add a testing stage to your Jenkins pipeline as shown above. This ensures that the pipeline only proceeds if all tests pass—safety first!
Deploying the Application
Once we have a Docker image, it’s time to deploy it.
Step 11: Deploy Using Docker Compose
A simple way to deploy our containerized app is using Docker Compose.
Create a docker-compose.yml
file:
version: '3.3'
services:
web:
image: your-dockerhub-username/your-app-name:latest
ports:
- "8080:8080"
Deploy the app by running:
docker-compose up -d
Deployment Note: Jenkins can be configured to run this command automatically upon a successful build.
Best Practices for CI/CD Pipelines
- Keep Builds Fast: The pipeline should be optimized for speed.
- Fail Fast: Configure the pipeline to fail early in case of errors.
- Security Checks: Integrate tools like OWASP Dependency-Check to ensure your dependencies are secure.
Troubleshooting Common Issues
- Permission Issues: Ensure the Jenkins user has the necessary permissions to run Docker commands.
- Port Conflicts: Docker uses ports that might already be in use; modify the ports in
docker-compose.yml
if necessary.
Conclusion
With Jenkins and Docker, building a CI/CD pipeline becomes an enjoyable and productive endeavor. We’ve walked through setting up Jenkins, installing Docker, creating a Dockerized app, and deploying it—all while learning the importance of automation in today’s fast-paced software world. Remember, just like teaching a child to ride a bike, creating a CI/CD pipeline takes patience, practice, and a few bumps along the way—but in the end, it’s totally worth it.
Your Turn: Ready to set up your CI/CD pipeline with Jenkins and Docker? Follow these steps, and before you know it, you’ll have automated your entire workflow, giving you more time for creative and innovative development!