In modern software development, Continuous Integration (CI) and Continuous Deployment (CD) are essential practices for improving the speed, quality, and reliability of software delivery. By automating the testing, building, and deployment processes, teams can ensure faster development cycles and catch issues earlier.
In this blog post, we will walk through how to set up a CI/CD pipeline using Docker and Jenkins. We’ll cover the steps for automating the build, test, and deployment processes for your containerized applications, all while leveraging the power of Jenkins, an open-source automation server.
What is Jenkins?
Jenkins is one of the most popular open-source automation tools used for building, testing, and deploying software. It’s highly extensible and supports a wide range of plugins to integrate with various tools and services. Jenkins allows you to create custom workflows to automate tasks like building and testing code, deploying applications, and more.
What is Docker?
Docker is a platform that allows you to package your application and its dependencies into a standardized unit called a container. Containers ensure consistency across environments by providing an isolated and lightweight environment for your applications to run. Docker helps eliminate issues like “it works on my machine” by ensuring that your application works the same way regardless of the system it’s deployed on.
Why Use Docker with Jenkins?
Combining Docker with Jenkins creates a powerful CI/CD pipeline that benefits from the following:
• Isolation: Docker ensures that Jenkins jobs run in isolated environments, reducing conflicts between different builds.
• Consistency: With Docker containers, you can ensure that each build environment is identical, leading to consistent results across different machines.
• Portability: Docker containers are portable across environments, so you can easily move your application between development, staging, and production environments.
• Scaling: Docker allows you to scale Jenkins and other tools efficiently, especially in large teams or enterprises.
Now, let’s dive into how you can set up a CI/CD pipeline with Docker and Jenkins.
Step 1: Install Jenkins and Docker
Before setting up the CI/CD pipeline, you need to install Jenkins and Docker on your machine or server. Here’s how to do that:
Install Jenkins
You can install Jenkins on your local machine or a remote server. The easiest way to install Jenkins is through Docker.
- Pull the Jenkins Docker image:
- docker pull jenkins/jenkins:lts
- Run Jenkins in a Docker container:
- docker run -d -p 8080:8080 -p 50000:50000 –name jenkins jenkins/jenkins:lts
- Access Jenkins: Open a browser and go to http://localhost:8080. You will be prompted to unlock Jenkins by providing the initial administrator password, which you can find by running the following command:
- docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassword
- Complete the Jenkins setup: Follow the setup wizard and install the recommended plugins.
Install Docker
If Docker is not installed yet, follow these steps to install it: - For Linux: Follow the installation guide on Docker’s official documentation: Install Docker on Linux.
- For Windows/Mac: Download and install Docker Desktop from here.
Step 2: Install Jenkins Plugins for Docker
To integrate Jenkins with Docker, you’ll need to install some plugins:
- Open Jenkins in your browser and go to Manage Jenkins > Manage Plugins.
- Search for the following plugins and install them:
o Docker Plugin: Allows Jenkins to communicate with Docker.
o Docker Pipeline: Enables the use of Docker in Jenkins Pipelines.
o Git: Required to pull your source code from repositories.
o Blue Ocean (optional): Provides a modern UI for managing Jenkins pipelines.
After installation, restart Jenkins to apply the changes.
Step 3: Set Up Docker in Jenkins
Now that Jenkins is set up, you need to configure Jenkins to interact with Docker.
- Configure Docker in Jenkins:
o Go to Manage Jenkins > Configure System.
o Scroll to the Docker section and click Add Docker Cloud.
o Configure the Docker server (if you’re using Docker locally, it will typically be unix:///var/run/docker.sock). - Test Docker Connectivity: To ensure Jenkins can communicate with Docker, you can create a simple job that runs a Docker container. Try adding a Freestyle Project that runs a Docker container and see if it works.
Step 4: Create a Jenkins Pipeline for CI/CD
Now, we can set up a Jenkins Pipeline that automates the entire process of building, testing, and deploying a Dockerized application. Jenkins Pipelines are defined in a Jenkinsfile, which is stored in your project repository.
- Create a Jenkinsfile: In your project repository, create a file named Jenkinsfile in the root of your repository. This file will define the steps in your CI/CD pipeline.
Here’s an example Jenkinsfile for a basic CI/CD pipeline:
pipeline {
agent any environment {
DOCKER_IMAGE = “my-app”
DOCKER_TAG = “latest”
} stages {
stage(‘Checkout’) {
steps {
git ‘https://github.com/your-username/your-repo.git’
}
}stage('Build') { steps { script { docker.build("${DOCKER_IMAGE}:${DOCKER_TAG}") } } } stage('Test') { steps { script { docker.image("${DOCKER_IMAGE}:${DOCKER_TAG}").inside { sh 'npm test' // Replace with your testing command } } } } stage('Deploy') { steps { script { docker.image("${DOCKER_IMAGE}:${DOCKER_TAG}").push() // Replace with your deployment steps (e.g., to a cloud provider) } } }
} post {
always {
cleanWs() // Clean workspace after each run
}
}
}
In this pipeline:
o Checkout: Jenkins pulls the latest code from your Git repository.
o Build: Jenkins uses Docker to build your image with the tag latest.
o Test: It runs your tests inside the built Docker image.
o Deploy: Once tests pass, the image is pushed to a Docker registry (e.g., Docker Hub or AWS ECR). - Add the Jenkinsfile to your repository: Commit the Jenkinsfile to your repository so Jenkins can use it.
Step 5: Configure Jenkins Job to Run Pipeline
- Create a New Pipeline Job:
o Go to Jenkins Dashboard and click New Item.
o Choose Pipeline, give it a name, and click OK. - Link the Jenkinsfile to the Job:
o In the job configuration, under the Pipeline section, select Pipeline script from SCM.
o Choose Git as your source code management tool, and provide the URL of your Git repository.
o Under Branch Specifier, use */main (or whichever branch you’re working with).
o Jenkins will automatically detect the Jenkinsfile in your repository and run the pipeline.
Step 6: Triggering the Pipeline
Once the job is configured, the pipeline will trigger automatically whenever you push code to the repository. You can also manually trigger the pipeline from the Jenkins dashboard.
To verify that everything is working:
• Check that Jenkins correctly pulls the code.
• Confirm that Docker builds and tests the application.
• Ensure the application is deployed or pushed to your registry.
Conclusion
Setting up a CI/CD pipeline with Docker and Jenkins allows you to automate the entire process of building, testing, and deploying your applications in a consistent and reliable way. Docker containers offer a reproducible environment, while Jenkins provides a robust platform to manage your automation needs.
By following the steps outlined in this post, you can ensure faster, more reliable releases with reduced manual intervention and greater confidence in the stability of your application.
Happy automating!