Containerized CI/CD Pipelines with Jenkins
Continuous Integration and Continuous Deployment (CI/CD) are essential practices in modern software development, enabling teams to automate their workflows and deliver software with speed and reliability. Jenkins, an open-source automation server, plays a pivotal role in implementing CI/CD pipelines by providing a flexible and robust platform to manage build, test, and deployment processes. In this tutorial, we will build a containerized CI/CD pipeline using Jenkins and Docker. By the end, you will have set up a Jenkins server, configured a pipeline using Jenkins' pipeline-as-code approach, and integrated Docker to containerize your builds, run tests, and deploy your application to a test environment. This hands-on guide is designed to provide you with a comprehensive understanding of how Jenkins and Docker work together in a CI/CD context, equipping you with the knowledge to implement similar pipelines in your own projects.
Setting Up Jenkins
In this section, we will focus on setting up Jenkins, the foundation of our CI/CD pipeline. Jenkins is a powerful open-source automation server that can orchestrate tasks such as building, testing, and deploying applications. We'll install Jenkins, configure it, and ensure it's ready to work with pipeline-as-code and Docker integration.
Install Jenkins
For this tutorial, we’ll use the official Jenkins Docker image to run Jenkins in a containerized environment. Running Jenkins in Docker ensures an isolated environment and simplifies installation.
Run the following command to start Jenkins in a Docker container:
docker run -d -p 8080:8080 -p 50000:50000 \
--name jenkins \
-v jenkins_home:/var/jenkins_home \
jenkins/jenkins:lts
This command pulls the Jenkins Long-Term Support (LTS) image, maps the container's port 8080 to the host, and persists Jenkins data in a Docker volume called jenkins_home.
Access Jenkins
Once the container is running, access Jenkins by opening a web browser and navigating to http://localhost:8080. You will be prompted to unlock Jenkins using an initial admin password.
Retrieve the password using the following command:
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
Copy the password, paste it into the setup screen, and proceed.
Complete the Initial Setup
Follow the setup wizard to install Jenkins plugins and create an admin user. During the process:
Choose to install the suggested plugins for a quick setup.
Create an admin user account with a username and password for future logins.
Once setup is complete, you will be redirected to the Jenkins dashboard.
Install Required Plugins
To enable pipeline-as-code functionality and Docker integration, we need to install a few plugins:
Pipeline: Provides the foundational tools to define and manage pipelines in Jenkins.
Docker Pipeline: Allows Jenkins to interact with Docker during pipeline execution.
To install these plugins:
Navigate to Manage Jenkins > Manage Plugins.
Go to the Available tab, search for the required plugins, and install them without restarting Jenkins.
Configure Jenkins for Docker
If your Jenkins container doesn’t already have Docker installed, or if you’re running Jenkins on a different host, you must ensure Jenkins can interact with Docker.
Install Docker on the host machine (if not already installed). Add the Jenkins user to the Docker group to grant permission to run Docker commands:
sudo usermod -aG docker jenkins
Restart the Jenkins container or service for the changes to take effect. To verify Docker is accessible to Jenkins, run a simple Docker command in the Jenkins UI. For example, create a freestyle job with a build step that runs docker --version. If the job succeeds, Docker is correctly configured.
Test the Jenkins Installation
Before proceeding with pipeline creation, ensure Jenkins is functioning as expected. Create a basic freestyle job:
Go to the Jenkins dashboard and click New Item.
Name the job, select Freestyle Project, and click OK.
Add a simple build step, such as executing a shell command: echo "Hello, Jenkins!".
Save and run the job.
If the job completes successfully and outputs the expected message, your Jenkins setup is ready.
At this stage, you have a fully functional Jenkins server running in a containerized environment, with the required plugins installed and Docker configured. You’re now ready to move on to creating and configuring your first pipeline using Jenkinsfile.