CI/CD Pipeline Automation with Jenkins and Docker

Introduction
Continuous Integration and Continuous Delivery (CI/CD) are critical practices in modern software development that ensure efficient, reliable, and repeatable deployments. Jenkins, an open-source automation server, is widely used to implement CI/CD pipelines, while Docker provides a lightweight, portable containerization solution for applications.
In this guide, we will demonstrate how to set up a CI/CD pipeline using Jenkins and Docker. The pipeline will include:
Integration with GitHub for version control
Building and testing Docker images
Automating testing processes
Deploying to different environments
Prerequisites
Before setting up the CI/CD pipeline, ensure you have the following:
A server with Jenkins installed
Docker installed on the server
A GitHub repository for version control
A sample application with a Dockerfile
Basic knowledge of Jenkins Pipelines and Groovy scripting
Step 1: Installing and Configuring Jenkins
Jenkins can be installed on Linux, macOS, or Windows. Below are the steps to install Jenkins on Ubuntu:
sudo apt update
sudo apt install openjdk-11-jdk -y
wget -q -O - https://pkg.jenkins.io/debian/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 jenkins -y
sudo systemctl start jenkins
sudo systemctl enable jenkins
Once installed, access Jenkins by navigating to http://<server-ip>:8080
. Complete the initial setup by installing recommended plugins and creating an admin user.
Step 2: Installing Required Plugins
To integrate Jenkins with Docker and GitHub, install the following plugins:
Pipeline
Git
Docker Pipeline
Go to Manage Jenkins > Plugins > Available Plugins and install them.
Step 3: Configuring GitHub Webhook
To trigger Jenkins builds automatically, configure a GitHub webhook:
Navigate to your GitHub repository.
Go to Settings > Webhooks > Add Webhook.
Set the Payload URL to
http://<JENKINS_URL>/github-webhook/
.Choose "application/json" as the content type.
Select "Just the push event" and click "Add Webhook".
Step 4: Writing the Dockerfile
A Dockerfile
defines the application's containerized environment. Below is a simple Dockerfile
for a Node.js application:
FROM node:14
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["node", "index.js"]
EXPOSE 3000
This Dockerfile
does the following:
Uses the official Node.js 14 image.
Sets
/app
as the working directory.Copies
package.json
and installs dependencies.Copies the rest of the application code.
Runs the
index.js
file.Exposes port 3000.
Step 5: Creating the Jenkins Pipeline
Jenkins Pipelines are defined using a Jenkinsfile
. Below is a Jenkinsfile
that automates building, testing, and deploying a Docker container:
pipeline {
agent any
environment {
DOCKER_IMAGE = "myapp"
DOCKER_TAG = "latest"
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-repo/your-app.git'
}
}
stage('Build Docker Image') {
steps {
script {
sh "docker build -t $DOCKER_IMAGE:$DOCKER_TAG ."
}
}
}
stage('Run Tests') {
steps {
script {
sh "docker run --rm $DOCKER_IMAGE:$DOCKER_TAG npm test"
}
}
}
stage('Push to Docker Hub') {
steps {
withDockerRegistry([credentialsId: 'docker-hub-credentials', url: '']) {
sh "docker tag $DOCKER_IMAGE:$DOCKER_TAG your-dockerhub-username/$DOCKER_IMAGE:$DOCKER_TAG"
sh "docker push your-dockerhub-username/$DOCKER_IMAGE:$DOCKER_TAG"
}
}
}
stage('Deploy') {
steps {
script {
sh "docker run -d -p 80:3000 your-dockerhub-username/$DOCKER_IMAGE:$DOCKER_TAG"
}
}
}
}
}
Explanation of the Jenkinsfile
Checkout: Clones the latest code from GitHub.
Build Docker Image: Uses the
docker build
command to create an image.Run Tests: Executes tests inside a container.
Push to Docker Hub: Uploads the built image to Docker Hub.
Deploy: Runs the application container on a server.
Step 6: Running the Pipeline
To run the pipeline:
Create a new Jenkins job.
Select Pipeline and specify the repository URL.
Set the pipeline definition to Pipeline script from SCM and point it to the
Jenkinsfile
in the repository.Click Build Now.
Step 7: Monitoring and Debugging
Monitor builds in Jenkins under Build History. If failures occur:
Check the console output for error logs.
Ensure Docker is running (
systemctl status docker
).Verify GitHub webhook delivery status.
Validate Docker credentials in Jenkins.
Conclusion
By integrating Jenkins with Docker, we have created a robust CI/CD pipeline that automates building, testing, and deploying applications. This setup ensures consistent and reliable software delivery while reducing manual effort.
Next Steps
Implement Blue/Green deployments.
Use Kubernetes for scalable deployments.
Automate security scanning in the pipeline.
Subscribe to my newsletter
Read articles from The DevOps Dojo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
