Installing Jenkins Using Docker

Learn How to Quickly Install and Configure Jenkins with Docker for Effective Automation.

Jorge Luis Castro Medina
6 min readSep 9, 2024

--

In today’s fast-paced development environments, automation is key to maintaining efficiency and delivering quality software. Jenkins, one of the most popular CI/CD tools, plays a crucial role in automating build, test, and deployment processes. However, setting up Jenkins can sometimes be time-consuming and complex. That’s where Docker comes in. 🐳

In this article, I will guide you through the process of installing Jenkins using Docker. By the end of this guide, you’ll have a fully operational Jenkins setup ready to enhance your CI/CD pipeline. 💡🔧

Note: This guide assumes that Jenkins is not already installed on your system. The following steps outline how to install Jenkins using Docker for a fresh setup. If you have Jenkins installed through other means, this guide may not apply directly.📦🆕

Let’s get started! 🚀

Docker Image for Jenkins 🐳

To start, we’ll need to pull the official Docker image for Jenkins. You can find the Jenkins Docker image on Docker Hub. This image will serve as the foundation for our Jenkins setup.

Next, we’re going to create a working directory where we’ll define two important files: Dockerfile and compose.yml. These files will help us configure Jenkins within a Docker environment efficiently.

Step 1: Create a Directory 🗂️

Let’s first create a directory on your machine where we will store the configuration files for our Jenkins setup. Open your terminal and run the following command:

mkdir jenkins-docker-setup
cd jenkins-docker-setup

Inside this directory, we’ll create the Dockerfile and compose.yml files.

Step 2: Create a Dockerfile 📝

Now, let’s create the Dockerfile. This file defines how Docker will build and run the Jenkins container. Inside the jenkins-docker-setup directory, create a new file named Dockerfile and add the following content:

Dockerfile

FROM jenkins/jenkins:lts

# Enable root user to install plugins and other dependencies.
USER root

# Update packages and clean up cache to keep the image slim
RUN apt-get update && \
apt-get upgrade -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Install Jenkin plugins
RUN jenkins-plugin-cli --plugins role-strategy

# Switch back to the Jenkins user
USER jenkins

In this Dockerfile, we:

  • Pull the latest stable Jenkins image (jenkins/jenkins:lts).
  • Temporarily switch to the root user to install any necessary plugins or dependencies.
  • Update all the installed packages to their latest versions and clean up cached files to keep the image as slim as possible.
  • Finally, we switch back to the Jenkins user to run Jenkins in a secure, non-root environment.

Jenkins Plugins 🔌

Step 3: Create a Docker Compose File 🛠️

Next, we’ll create a compose.yml file to orchestrate the container using Docker Compose. This file defines how Docker will manage Jenkins, including ports and volume mappings. In the same directory (jenkins-docker-setup), create a new file named compose.yml and add the following content:

compose.yml

services:
jenkins:
image: "jenkins/jenkins:lts"
build: .
container_name: jenkins-container
ports:
- "8080:8080"
- "50000:50000"
volumes:
- /your-home/data/jenkins:/var/jenkins_home
restart: unless-stopped

In this compose.yml file:

  • We define a service named jenkins, which builds the Jenkins image from the current directory (context: .).
  • The container is named jenkins-container for easy identification.
  • Ports are mapped: 8080 for Jenkins' web interface and 50000 for agent communication.
  • We mount a local directory (/your-home/data/jenkins) to /var/jenkins_home inside the container to persist Jenkins data across restarts.
  • We also use the restart: unless-stopped option to ensure the Jenkins container restarts automatically if it stops or if the host system is rebooted, unless manually stopped.

This configuration ensures that Jenkins will always be running unless you manually stop the container.

Running the Jenkins Container 🚀

Now that we have our Dockerfile and compose.yml files set up, it's time to run the container and get Jenkins up and running. Follow these steps to start your Jenkins container and access the Jenkins web interface.

Step 1: Build and Start the Container 🛠️

In the terminal, navigate to the directory where your compose.yml file is located (jenkins-docker-setup). To build and start the Jenkins container, execute the following command:

docker compose up -d

This command uses Docker Compose to build the Jenkins image from the context specified in your compose.yml file and start the container in detached mode (-d), which means it will run in the background.

Step 2: Access Jenkins Web Interface 🌐

Once the container is up and running, you can access the Jenkins web interface. Open your web browser and navigate to:

https://<your-docker-host>:8080

Replace <your-docker-host> with the IP address or hostname of your Docker host. If you are running Docker on your local machine, this could be localhost or 127.0.0.1.

Step 3: Unlock Jenkins 🔐

The first time you access Jenkins, you will be prompted to unlock it. To do this, you need to retrieve the initial admin password from the Jenkins container.

Run the following command to get the password:

# docker exec [OPTIONS] CONTAINER_ID_OR_NAME COMMAND [ARG...]
docker exec jenkins-container cat /var/jenkins_home/secrets/initialAdminPassword

Copy the password from the command output and paste it into the Jenkins web interface to unlock Jenkins.

Step 4: Install Suggested Plugins 🛠️

After unlocking Jenkins, you will be prompted to install plugins. Jenkins will offer to install a set of recommended plugins automatically. You can either:

  • Install Suggested Plugins: Follow the prompt to install these plugins, which will provide essential functionality for your Jenkins instance.
  • Select plugins to install: Alternatively, you can choose specific plugins based on your requirements.

Step 5: Create an Admin User 👤

You will be presented with a “Create First Admin User” screen. You have two options:

  • Skip and Continue as Admin: This option allows you to proceed to the Jenkins home page using the default credentials (admin/initialAdminPassword).
  • Create a New Admin User: For better security and management, it’s recommended to set up a new admin user. Enter your preferred Username, Password, Full name, and E-mail address in the form provided to create the new admin account.

Step 6: Configure Jenkins Instance Settings ⚙️

After creating the admin user, you’ll need to configure the Jenkins instance settings:

  • Instance Configuration: Set up the Jenkins URL and any other instance-specific settings as prompted. This configuration ensures that Jenkins is properly integrated with your network and accessible for your team.

Once these steps are complete, Jenkins will be fully set up and ready for use! 🎉

Bonus 🎁

  1. Run the command with sudo if you encounter a permission denied error.

2. Some useful commands for Docker compose (beginner level 😅)

# Build the image
docker compose build

# Start container
docker compose up -d

# Stop container
docker compose down

3. Pre-install Jenkins Plugins:

You can pre-install Jenkins plugins by adding commands to the Dockerfile. For example, use the jenkins-plugin-cli to install plugins directly:

USER root

# Install Jenkins plugins
RUN jenkins-plugin-cli --plugins \
role-strategy \
workflow-aggregator \
git \
docker-workflow

USER jenkins

This command ensures that your Jenkins instance is ready with essential tools right from the start.

Sources

If you like my content and want to support my work, you can give me a cup of coffee ☕️ 🥰

Follow me in

--

--

Jorge Luis Castro Medina
Jorge Luis Castro Medina

Written by Jorge Luis Castro Medina

I'm a Software Engineer passionate about mobile technologies, and I like everything related to software design and architecture