DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Implementing CI/CD Pipelines With Jenkins and Docker
  • Java CI/CD: From Local Build to Jenkins Continuous Integration
  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Jenkins in the Age of Kubernetes: Strengths, Weaknesses, and Its Future in CI/CD

Trending

  • Top NoSQL Databases and Use Cases
  • Stop Building Monolithic AI Brains, Build a Specialist Team Instead
  • Why API-First Frontends Make Better Apps
  • Leveraging AI: A Path to Senior Engineering Positions
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Install and Set Up Jenkins With Docker Compose

How to Install and Set Up Jenkins With Docker Compose

A complete step-by-step guide to install and run Jenkins using Docker Compose with detailed explanation and walk-through.

By 
Faisal Khatri user avatar
Faisal Khatri
DZone Core CORE ·
Jun. 12, 25 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
4.2K Views

Join the DZone community and get the full member experience.

Join For Free

Jenkins is an open-source CI/CD tool written in Java that is used for organising the CI/CD pipelines. Currently, at the time of writing this blog, it has 24k stars and 9.1k forks on GitHub. With over 2000 plugin support, Jenkins is a well-known tool in the DevOps world.

The following are multiple ways to install and set up Jenkins:

  • Using the Jenkins Installer package for Windows
  • Using Homebrew for macOS
  • Using the Generic Java Package (war)
  • Using Docker
  • Using Kubernetes
  • Using apt for Ubuntu/Debian Linux OS

In this tutorial blog, I will cover the step-by-step process to install and setup Jenkins using Docker Compose for an efficient and seamless CI/CD experience.

Using Dockerwith Jenkins allows users to set up a Jenkins instance quickly with minimal manual configuration. It ensures portability and scalability, as with Docker Compose, users can easily set up Jenkins and its required services, such as volumes and networks, using a single YAML file. This allows the users to easily manage and replicate the setup in different environments.

Installing Jenkins Using Docker Compose

Installing Jenkins with Docker Compose makes the setup process simple and efficient, and allows us to define configurations in a single file. This approach removes the complexity and difficulty faced while installing Jenkins manually and ensures easy deployment, portability, and quick scaling.

Prerequisite

As a prerequisite, Docker Desktop needs to be installed, up and running on the local machine. Docker Compose is included in Docker Desktop along with Docker Engine and Docker CLI.

Jenkins With Docker Compose

Jenkins could be instantly set up by running the following docker-compose command using the terminal:

Plain Text
 
docker compose up -d

This docker-compose command could be run by navigating to the folder where the Docker Compose file is placed. So, let’s create a new folder jenkins-demo and inside this folder, let’s create another new folder jenkins-configuration and a new file docker-compose.yaml.

The following is the folder structure:

Plain Text
 
jenkins-demo/
├── jenkins-configuration/
└── docker-compose.yaml


The following content should be added to the docker-compose.yaml file.

YAML
 
# docker-compose.yaml
  version: '3.8'
  services:
    jenkins:
      image: jenkins/jenkins:lts
      privileged: true
      user: root
      ports:
       - 8080:8080
       - 50000:50000
      container_name: jenkins
      volumes:
      - /Users/faisalkhatri/jenkins-demo/jenkins-configuration:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock

Decoding the Docker Compose File

The first line in the file is a comment. The services block starts from the second line, which includes the details of the Jenkins service.

The Jenkins service block contains the image, user, and port details. The Jenkins service will run the latest Jenkins image with root privileges and name the container as jenkins.

The ports are responsible for mapping container ports to the host machine. The details of these ports are as follows:

  • 8080:8080:This will map the port 8080 inside the container to the port 8080 on the host machine. It is important, as it is required for accessing the Jenkins web interface. It will help us in accessing Jenkins in the browser by navigating to http://localhost:8080
  • 50000:50000:This will map the port 50000 inside the container to port 50000 on the host machine. It is the JNLP (Java Network Launch Protocol) agent port, which is used for connecting Jenkins build agents to the Jenkins Controller instance. It is important, as we would be using distributed Jenkins setups, where remote build agents connect to the Jenkins Controller instance.

The privileged: true setting will grant the container full access to the host system and allow running the process as the root user on the host machine. This will enable the container to perform the following actions :

  • Access all the host devices
  • Modify the system configurations
  • Mount file systems
  • Manage network interfaces
  • Perform admin tasks that a regular container cannot perform

These actions are important, as Jenkins may require permissions to run specific tasks while interacting with the host system, like managing Docker containers, executing system commands, or modifying files outside the container.

Any data stored inside the container is lost when the container stops or is removed. To overcome this issue, Volumes are used in Docker to persist data beyond the container’s lifecycle. We will use Docker Volumes to keep the Jenkins data intact, as it is needed every time we start Jenkins.

Jenkins data would be stored in the jenkins-configuration folder on the local machine. The /Users/faisalkhatri/jenkins-demo/jenkins-configuration on the host is mapped to /var/jenkins_home in the container. The changes made inside the container in the respective folder will reflect on the folder on the host machine and vice versa.

This line /var/run/docker.sock:/var/run/docker.sock, mounts the Docker socket from the host into the container, allowing the Jenkins container to directly communicate with the Docker daemon running on the host machine. This enables Jenkins, which is running inside the container, to manage and run Docker commands on the host, allowing it to build and run other Docker containers as a part of CI/CD pipelines.

Installing Jenkins With Docker Compose

Let’s run the installation process step by step as follows:

Step 1 — Running Jenkins Setup

Open a terminal, navigate to the jenkins-demo folder, and run the following command:

Plain Text
 
docker compose up -d


Running Jenkins Setup

After the command is successfully executed, open any browser on your machine and navigate to https://localhost:8080, you should be able to find the Unlock Jenkins screen as shown in the screenshot below:

Unlock Jenkins screen

Step 2 — Finding the Jenkins Password From the Docker Container

The password to unlock Jenkins could be found by navigating to the jenkins container (remember we had given the name jenkins to the container in the Docker Compose file) and checking out its logs by running the following command on the terminal:

Plain Text
 
docker logs jenkins


jenkins container logs

jenkins container logs

Copy the password from the logs, paste it in the Administrator password field on the Unlock Jenkins screen in the browser, and click on the Continue button.

Step 3 — Setting up Jenkins

The “Getting Started” screen will be displayed next, which will prompt us to install plugins to set up Jenkins.

Select the Install suggested plugins and proceed with the installation.

Setting up Jenkins: Install suggested plugin screen

It will take some time for the installations to complete.

Step 4 — Creating Jenkins user

After the installation is complete, Jenkins will show the next screen to update the user details. It is recommended to update the user details with a password and click on Save and Continue.

This username and password can then be used to log in to Jenkins.

Creating Jenkins user


Step 5 — Instance Configuration

In this window, we can update the Jenkins accessible link so it can be further used to navigate and run Jenkins.

 Instance Configuration

However, we can leave it as it is now — http://localhost:8080. Click on the Save and Finish button to complete the set up.

With this, the Jenkins installation and set up are complete; we are now ready to use Jenkins.

Welcome to Jenkins screen

Summary

Docker is the go-to tool for instantly spinning up a Jenkins instance. Using Docker Compose, we installed Jenkins successfully in just 5 simple steps. Once Jenkins is up and started, we can install the required plugin and set up CI/CD workflows as required.

Using Docker Volumes allows us to use Jenkins seamlessly, as it saves the instance data between restarts.

In the next tutorial, we will learn about installing and setting up Jenkins agents that will help us run the Jenkins jobs.

Docker (software) Jenkins (software) Continuous Integration/Deployment

Published at DZone with permission of Faisal Khatri. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Implementing CI/CD Pipelines With Jenkins and Docker
  • Java CI/CD: From Local Build to Jenkins Continuous Integration
  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Jenkins in the Age of Kubernetes: Strengths, Weaknesses, and Its Future in CI/CD

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: