DEV Community

Prajwal Patil
Prajwal Patil

Posted on

"Streamlining CI/CD: A Step-by-Step Guide to Setting Up Jenkins on Docker"

How to Set Up Jenkins on Docker
Introduction
Jenkins is a widely-used open-source automation server that helps automate the non-human part of the software development process. Docker, on the other hand, is a platform that enables developers to create, deploy, and run applications in containers. Combining Jenkins with Docker provides a powerful tool for continuous integration and continuous delivery (CI/CD).
In this guide, we'll walk through the steps to set up Jenkins on Docker.
Prerequisites

  • Docker installed on your system.
  • Basic understanding of Docker and Jenkins.
  • Sufficient privileges to run Docker commands.

Setting up Jenkins on Docker offers several advantages that can streamline and enhance your CI/CD workflow. Here are the key reasons why you might choose to deploy Jenkins using Docker:
1. Consistency and Isolation
Consistency: Docker containers ensure that Jenkins runs in a consistent environment across different development, testing, and production environments. This consistency helps eliminate issues caused by variations in software configurations.
Isolation: Docker containers isolate Jenkins and its dependencies from other applications on the host system. This isolation helps prevent conflicts and makes it easier to manage dependencies.
2. Simplified Setup and Configuration
Ease of Setup: Docker simplifies the setup process by allowing you to pull and run pre-configured Jenkins images. This reduces the complexity involved in manually installing Jenkins and configuring its environment.
Configuration Management: Docker makes it easy to version and manage configurations through Dockerfiles and Docker Compose, ensuring that your Jenkins setup can be easily replicated or modified.
3. Portability
Docker containers can run on any system that supports Docker, making your Jenkins setup highly portable. This portability is particularly useful for developers working in different environments or for teams that need to move their CI/CD pipeline across various stages of development and production.
4. Scalability
Resource Allocation: Docker allows you to allocate specific resources (CPU, memory) to Jenkins containers, ensuring that Jenkins performs optimally without affecting other applications.
Scaling: Running Jenkins in Docker containers makes it easier to scale your CI/CD infrastructure. You can quickly spin up additional Jenkins instances to handle increased workloads or parallelize build processes.
5. Simplified Maintenance and Upgrades
Upgrades: Upgrading Jenkins is straightforward with Docker. You can pull the latest Jenkins image and recreate the container without worrying about breaking the underlying system.
Backup and Recovery: Docker volumes can be used to persist Jenkins data, making it easier to backup and restore configurations, jobs, and build history.
6. Security
Sandboxing: Docker containers provide an additional layer of security by sandboxing Jenkins from the host system. This reduces the risk of potential vulnerabilities in Jenkins affecting the host.
Controlled Access: Docker's networking and permission features allow for fine-grained control over how Jenkins interacts with other services and the network.
7. DevOps Integration
Docker is a staple in modern DevOps practices. Running Jenkins on Docker integrates seamlessly with other containerized services and tools in your DevOps pipeline, promoting a more cohesive and efficient workflow.
Step-by-Step Guide
Step 1: Install Docker
Before setting up Jenkins, ensure Docker is installed on your machine.
For Ubuntu:

sudo apt update
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

Step 2: Pull the Jenkins Docker Image
Jenkins maintains an official Docker image. To pull the latest Jenkins image, run:

docker pull jenkins/jenkins:lts
Enter fullscreen mode Exit fullscreen mode

The lts tag refers to the Long Term Support version, which is stable and recommended for most users.

Step 3: Run the Jenkins Container
Create and start a Jenkins container with the following command:

docker run -d -p 8080:8080 jenkins/jenkins:lts
Enter fullscreen mode Exit fullscreen mode

Here's a breakdown of the command:
-d: Run the container in detached mode.
-p 8080:8080: Map port 8080 of the host to port 8080 of the container (Jenkins web int)

Step 4: Access Jenkins
Once the container is running, you can access Jenkins by navigating to http://localhost:8080 in your web browser.

Step 5: Unlock Jenkins
On your first visit, Jenkins will ask you to unlock it using a password stored in the Docker container. Retrieve this password with:

docker exec -it jenkins bash
cat /var/jenkins_home/secrets/initialAdminPassword
Enter fullscreen mode Exit fullscreen mode

Copy the password and paste it into the Jenkins unlock page.

Step 6: Install Suggested Plugins
After unlocking Jenkins, you'll be prompted to install plugins. Choose the "Install suggested plugins" option to get started quickly.

Step 7: Create an Admin User
Next, you'll need to create an admin user. Fill in the required details and complete the setup.

Step 8: Configure Jenkins

  1. Now that Jenkins is set up, you can start configuring it to suit your project needs. This includes setting up:
  2. Global Tool Configuration: Define the locations for JDK, Git, Gradle, etc.
  3. Credentials: Add necessary credentials for accessing repositories and other tools.
  4. Jobs/Pipelines: Create jobs or pipelines for your CI/CD process.

Conclusion
Deploying Jenkins on Docker simplifies the setup and management of your CI/CD pipeline. Docker containers provide a consistent environment for Jenkins, enhancing the reliability and scalability of your build process.
By following the steps outlined in this guide, you will have a fully functional Jenkins server running in a Docker container. This setup allows you to explore and leverage Jenkins' extensive range of plugins and configurations to further optimize your CI/CD workflow.

Top comments (0)