Automating CI/CD with Jenkins and Docker: A Step-by-Step Guide
In today's fast-paced software development environment, integrating Continuous Integration and Continuous Deployment (CI/CD) practices is essential for delivering quality software quickly and efficiently. One of the best combinations to achieve this is leveraging Jenkins, an open-source automation server, with Docker, a powerful platform for creating, deploying, and managing containerized applications. This blog post will guide you through the process of integrating Jenkins with Docker on an Ubuntu machine, enabling you to automate your build, test, and deployment workflows seamlessly.
Why Integrate Jenkins with Docker?
Integrating Jenkins with Docker offers several advantages:
Isolation: Docker containers provide a consistent execution environment, reducing the "it works on my machine" syndrome.
Scalability: Easily spin up and down containers as needed to test or build applications.
Portability: Dockerized applications can run on any system with Docker installed, simplifying deployment across different environments.
Prerequisites
Before we dive into the installation and configuration steps, ensure you have the following set up:
Step 1: Install Java
Jenkins requires Java to run. Install OpenJDK with the following command:
sudo apt install openjdk-11-jdk -y
Step 2: Add Jenkins Repository and Install Jenkins
Add the Jenkins repository and install Jenkins using the commands below:
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins -y
Step 3: Start Jenkins Service
Once installed, start the Jenkins service and enable it to run on boot:
sudo systemctl start jenkins
sudo systemctl enable jenkins
Step 4: Access Jenkins
Open your web browser and navigate to http://your_server_ip_or_domain:8080
. During the first access, you’ll need to unlock Jenkins using the initial admin password obtained with:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Proceed to install the suggested plugins and create your admin user.
Step 5: Install Docker
To install Docker on Ubuntu, first remove any old versions, then set up the Docker repository:
sudo apt remove docker docker-engine docker.io containerd runc
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
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"
sudo apt update
sudo apt install docker-ce -y
Step 6: Add Jenkins User to Docker Group
To allow Jenkins to execute Docker commands, add the Jenkins user to the Docker group:
sudo usermod -aG docker jenkins
After that, restart Jenkins:
sudo systemctl restart jenkins
Step 7: Configure Jenkins for Docker
Install Docker Plugin:
Configure Docker:
Step 8: Test Docker Integration
Create a new Jenkins job (either a Pipeline or Freestyle job):
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
sh 'docker --version'
sh 'docker run hello-world'
}
}
}
}
}
Step 9: Run your Job
Trigger the job and monitor the console output to verify that the Docker commands execute successfully. If you encounter permissions errors, double-check that Jenkins has the necessary permissions to run Docker commands.
Conclusion
Integrating Jenkins with Docker can greatly enhance your CI/CD workflows, allowing for streamlined builds, tests, and deployments in isolated environments. By following this guide, you can automate your development processes and deliver software faster and more reliably.
Feel free to reach out in the comments if you have questions or run into any issues while setting up! Happy coding!