How to set up Jenkins Docker environment?

JenkinsJenkinsBeginner
Practice Now

Introduction

Jenkins is a widely-adopted open-source automation server that plays a crucial role in modern software development workflows. By integrating Jenkins with Docker, you can create a robust and scalable environment for your continuous integration and deployment processes. This tutorial will guide you through the steps of setting up a Jenkins Docker environment, from preparing the Docker environment to configuring and using Jenkins within the container.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL jenkins(("`Jenkins`")) -.-> jenkins/PipelineGroup(["`Pipeline`"]) jenkins(("`Jenkins`")) -.-> jenkins/UsingJenkinsGroup(["`Using Jenkins`"]) jenkins(("`Jenkins`")) -.-> jenkins/BlueOceanGroup(["`Blue Ocean`"]) jenkins(("`Jenkins`")) -.-> jenkins/InstallingJenkinsGroup(["`Installing Jenkins`"]) jenkins/PipelineGroup -.-> jenkins/pipeline("`Pipeline`") jenkins/PipelineGroup -.-> jenkins/running_pipelines("`Running Pipelines`") jenkins/UsingJenkinsGroup -.-> jenkins/create_project("`Create Project`") jenkins/BlueOceanGroup -.-> jenkins/creating_a_pipeline("`Creating a Pipeline`") jenkins/InstallingJenkinsGroup -.-> jenkins/docker_installation("`Use Docker Installation`") jenkins/InstallingJenkinsGroup -.-> jenkins/war_files_installation("`Use War files installation`") subgraph Lab Skills jenkins/pipeline -.-> lab-414504{{"`How to set up Jenkins Docker environment?`"}} jenkins/running_pipelines -.-> lab-414504{{"`How to set up Jenkins Docker environment?`"}} jenkins/create_project -.-> lab-414504{{"`How to set up Jenkins Docker environment?`"}} jenkins/creating_a_pipeline -.-> lab-414504{{"`How to set up Jenkins Docker environment?`"}} jenkins/docker_installation -.-> lab-414504{{"`How to set up Jenkins Docker environment?`"}} jenkins/war_files_installation -.-> lab-414504{{"`How to set up Jenkins Docker environment?`"}} end

Understanding Jenkins and Docker

What is Jenkins?

Jenkins is an open-source automation server that helps automate the software development process. It is widely used for building, testing, and deploying applications. Jenkins provides a flexible and extensible platform that allows developers to integrate various tools and technologies into their software development workflow.

What is Docker?

Docker is an open-source platform that enables developers to build, deploy, and run applications in containers. Containers are lightweight, standalone, and executable software packages that include all the necessary dependencies, libraries, and configuration files to run an application. Docker allows developers to create, deploy, and manage these containers consistently across different environments.

Why Use Jenkins with Docker?

Integrating Jenkins with Docker can provide several benefits:

  1. Consistent Environments: Docker containers ensure that the development, testing, and production environments are consistent, reducing the risk of "works on my machine" issues.
  2. Scalability: Docker containers can be easily scaled up or down, allowing Jenkins to handle increased workloads during peak times.
  3. Reproducibility: Docker containers can be easily replicated, making it easier to set up and manage Jenkins instances across different environments.
  4. Isolation: Docker containers provide a high degree of isolation, ensuring that each Jenkins job runs in a clean and isolated environment, preventing conflicts between different projects or dependencies.

Docker Components for Jenkins

To set up a Jenkins environment using Docker, you'll need the following components:

  1. Jenkins Docker Image: The official Jenkins Docker image, which provides a pre-configured Jenkins server.
  2. Docker Engine: The Docker runtime that runs and manages the Jenkins Docker container.
  3. Docker Compose (optional): A tool for defining and running multi-container Docker applications, which can simplify the setup and management of the Jenkins environment.
graph TD A[Jenkins Docker Image] --> B[Docker Engine] B --> C[Docker Compose]

Advantages of Using Jenkins in Docker

Using Jenkins in a Docker environment offers several advantages:

  1. Portability: The Jenkins Docker image can be easily deployed on any system that supports Docker, making it highly portable.
  2. Isolation: Each Jenkins job runs in a separate Docker container, ensuring isolation and preventing conflicts between different projects or dependencies.
  3. Scalability: Jenkins can be easily scaled up or down by adding or removing Docker containers as needed.
  4. Reproducibility: The Jenkins Docker environment can be easily replicated, allowing for consistent and reliable deployments across different environments.
  5. Simplified Management: Docker Compose can be used to define and manage the entire Jenkins environment, simplifying the setup and maintenance process.

Preparing the Docker Environment for Jenkins

Installing Docker

Before setting up Jenkins in a Docker environment, you'll need to install Docker on your system. Here's how you can install Docker on Ubuntu 22.04:

## Update the package index
sudo apt-get update

## Install packages to allow apt to use a repository over HTTPS
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

## Add Docker's official GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

## Set up the Docker repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

## Install Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Verifying Docker Installation

After installing Docker, you can verify the installation by running the following command:

sudo docker run hello-world

This command will download a test image and run a container, which should output a message confirming that the Docker installation is working correctly.

Creating a Docker Network for Jenkins

To ensure that the Jenkins container can communicate with other containers (e.g., for building and testing applications), you should create a dedicated Docker network for Jenkins. You can do this using the following command:

sudo docker network create jenkins

This will create a new Docker network named "jenkins" that you can use when running the Jenkins container.

Preparing Persistent Storage for Jenkins

Jenkins requires persistent storage to store its configuration, plugins, and build artifacts. You can create a Docker volume to provide this persistent storage:

sudo docker volume create jenkins-data

This will create a Docker volume named "jenkins-data" that you can use when running the Jenkins container.

Configuring Docker Permissions

To allow Jenkins to interact with the Docker daemon, you'll need to grant the Jenkins user the necessary permissions. You can do this by adding the Jenkins user to the "docker" group:

sudo usermod -aG docker jenkins

After making this change, you'll need to restart the Jenkins service for the changes to take effect.

Configuring and Using Jenkins in Docker

Running the Jenkins Docker Container

With the necessary preparations complete, you can now run the Jenkins Docker container. Use the following command to start the Jenkins container:

sudo docker run -d \
  --name jenkins \
  --restart=unless-stopped \
  -p 8080:8080 \
  -p 50000:50000 \
  -v jenkins-data:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v $(which docker):/usr/bin/docker \
  --network jenkins \
  LabEx/jenkins

This command:

  • Runs the Jenkins container in detached mode (-d)
  • Names the container "jenkins"
  • Automatically restarts the container unless it is manually stopped (--restart=unless-stopped)
  • Maps the Jenkins web UI port (8080) and the Jenkins agent port (50000) to the host
  • Mounts the "jenkins-data" volume to the /var/jenkins_home directory inside the container
  • Mounts the Docker socket and the Docker binary to allow Jenkins to interact with the Docker daemon
  • Connects the Jenkins container to the "jenkins" Docker network

Accessing the Jenkins Web UI

After running the Jenkins container, you can access the Jenkins web UI by opening a web browser and navigating to http://localhost:8080. You should see the Jenkins setup wizard.

Completing the Jenkins Setup

Follow the on-screen instructions to complete the Jenkins setup process:

  1. Unlock Jenkins by entering the initial admin password, which can be found in the container logs or the "jenkins-data" volume.
  2. Install the suggested plugins or select the plugins you want to install.
  3. Create the first administrative user.
  4. Customize the Jenkins URL and other settings as needed.

Integrating Jenkins with Docker

To allow Jenkins to interact with Docker, you need to configure the Jenkins Docker plugin. Follow these steps:

  1. Install the "Docker" plugin in the Jenkins web UI.
  2. Configure the Docker plugin by providing the Docker host URL (e.g., unix:///var/run/docker.sock) and the Docker API version.
  3. Test the Docker plugin configuration by running a simple Docker command in a Jenkins job.

Using Jenkins in Docker

With Jenkins set up and configured, you can now start using it to automate your software development processes. Some common use cases include:

  • Building and testing your application code in Docker containers
  • Deploying your application to Docker containers
  • Integrating with other tools and services (e.g., source control, artifact repositories) using Jenkins plugins

By leveraging the power of Docker, you can ensure consistent and reproducible build and deployment environments, making your Jenkins-based workflows more reliable and scalable.

Summary

In this tutorial, you have learned how to set up a Jenkins Docker environment, enabling you to streamline your continuous integration and deployment processes. By leveraging the power of Docker, you can create a reliable and scalable Jenkins setup that can adapt to your evolving software development needs. With the knowledge gained from this guide, you can now confidently manage and maintain your Jenkins infrastructure within a Docker container, ensuring efficient and reliable software delivery.

Other Jenkins Tutorials you may like