Introduction
In this tutorial, you will learn how to implement Continuous Integration and Continuous Deployment (CI/CD) for your Dockerized applications using Jenkins. Jenkins is a popular open-source automation server that enables you to automate the building, testing, and deployment of your software projects. This guide will walk you through the steps required to set up a Jenkins server, configure it to connect to a source code repository (e.g., GitHub), create a Jenkins pipeline for Docker image building, testing, and container deployment, and automate the CI/CD process to trigger builds on code commits.
Prerequisites
Before you begin, make sure you have the following prerequisites:
A running Jenkins server. You can set up Jenkins on your own server or use a Jenkins container (e.g., Docker).
A source code repository for your application (e.g., a GitHub repository).
Docker installed on the Jenkins server or the Jenkins agent (if using Docker containers).
Your application code, Dockerfiles, and any necessary testing scripts ready in the source code repository.
Step 1: Set up a Jenkins Server or Container
You can set up Jenkins either by installing it on a dedicated server or by running a Jenkins container. Here, we will use a Jenkins container for simplicity.
Install Docker on your server if it's not already installed. Follow the official Docker installation guide for your platform.
Run a Jenkins container using the official Jenkins image:
docker run -d -p 8080:8080 -p 50000:50000 --name jenkins jenkins/jenkins
- Docker Container: A Docker container is a lightweight and standalone executable package that includes everything needed to run a piece of software, including the code, runtime, system tools, and libraries.
Access Jenkins in your web browser by opening
http://your-server-ip:8080
. You'll be prompted to unlock Jenkins using an initial password. Retrieve the initial password by running:docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
- Initial Password: This is a security measure to ensure that only authorized users can set up Jenkins.
Follow the Jenkins setup wizard to complete the installation. Install recommended plugins and create an admin user.
Step 2: Configure Jenkins to Connect to Your Source Code Repository
To set up CI/CD, Jenkins needs to connect to your source code repository. We'll configure it to work with a GitHub repository.
Install the "GitHub" plugin in Jenkins:
From the Jenkins dashboard, go to "Manage Jenkins" > "Manage Plugins."
In the "Available" tab, search for "GitHub" and install it.
Set up a personal access token in your GitHub account:
Go to your GitHub account settings > "Developer settings" > "Personal access tokens."
Generate a token with the necessary permissions (e.g., repo, admin:repo_hook).
Copy the generated token.
In Jenkins, go to "Manage Jenkins" > "Configure System."
Scroll down to the "GitHub" section and add your GitHub access token.
Test the connection to verify that Jenkins can access your GitHub repository.
Step 3: Create a Jenkins Pipeline for Docker Image Building and Deployment
A Jenkins pipeline defines the steps of your CI/CD process. We will create a basic pipeline for building Docker images, running tests, and deploying containers.
Create a new pipeline job in Jenkins:
Click "New Item" on the Jenkins dashboard.
Choose "Pipeline" as the job type.
In the pipeline configuration, define the pipeline script, such as:
pipeline { agent any stages { stage('Checkout') { steps { checkout scm } } stage('Build and Test') { steps { script { docker.build("my-docker-image:${env.BUILD_NUMBER}") docker.image("my-docker-image:${env.BUILD_NUMBER}").push() sh 'docker-compose -f docker-compose.test.yml up --exit-code-from test' } } } stage('Deploy') { steps { sh 'docker-compose up -d' } } } }
- Pipeline Script: This script defines the stages of your pipeline, including checking out the code, building a Docker image, running tests, and deploying the application. Adjust it according to your project's needs.
Save the pipeline job configuration.
Step 4: Automate the CI/CD Process
To automate the CI/CD process, you can set up webhooks in your source code repository that trigger Jenkins builds on code commits.
In your GitHub repository, go to "Settings" > "Webhooks" > "Add webhook."
Add the Jenkins webhook URL, such as
http://your-jenkins-server/github-webhook/
.Configure the webhook to trigger "push" events.
Save the webhook configuration.
Now, every time you push changes to your repository, Jenkins will automatically build, test, and deploy your application, following the defined pipeline.
Conclusion
In this tutorial, you've learned how to set up a Jenkins server or container, configure it to connect to your source code repository, create a Jenkins pipeline for Docker image building and deployment, and automate the CI/CD process. CI/CD with Jenkins and Docker is a powerful way to streamline software development, testing, and deployment processes.