Installing Jenkins with Docker

JenkinsJenkinsBeginner
Practice Now

Introduction

In this lab, we will explore the process of installing Jenkins using Docker. Jenkins is a widely used automation server for continuous integration and continuous delivery (CI/CD) processes. By the end of this lab, you will have hands-on experience setting up Jenkins with Docker, providing you with a solid foundation for future DevOps projects.

For beginners, it's important to understand that Docker is a platform that allows you to package and run applications in isolated environments called containers. This isolation ensures that your application works consistently across different systems. Jenkins, on the other hand, is a tool that helps automate parts of software development, particularly building, testing, and deploying code.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL jenkins(("`Jenkins`")) -.-> jenkins/UsingJenkinsGroup(["`Using Jenkins`"]) jenkins(("`Jenkins`")) -.-> jenkins/InstallingJenkinsGroup(["`Installing Jenkins`"]) jenkins/UsingJenkinsGroup -.-> jenkins/create_project("`Create Project`") jenkins/InstallingJenkinsGroup -.-> jenkins/docker_installation("`Use Docker Installation`") jenkins/InstallingJenkinsGroup -.-> jenkins/initial_settings("`Jenkins Initial Settings`") subgraph Lab Skills jenkins/create_project -.-> lab-391174{{"`Installing Jenkins with Docker`"}} jenkins/docker_installation -.-> lab-391174{{"`Installing Jenkins with Docker`"}} jenkins/initial_settings -.-> lab-391174{{"`Installing Jenkins with Docker`"}} end

Preparing the Docker Environment

In this step, we'll ensure that Docker is properly installed and running on our system. Docker is essential for this lab as it allows us to run Jenkins in a container, providing a consistent and isolated environment.

First, let's verify that Docker is installed and running. Open a terminal and execute the following command:

docker --version
alt text

This command should display the version of Docker installed on your system. You should see output similar to this:

Docker version 20.10.21, build 20.10.21-0ubuntu1~22.04.3

If you don't see a version number, it might mean Docker isn't installed or isn't in your system's PATH. Don't worry if this happens; we'll address it shortly.

Next, let's check if the Docker daemon is running:

sudo systemctl status docker

This command checks the status of the Docker service. You should see output indicating that the Docker service is active (running). It might look something like this:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-09-11 10:00:00 UTC; 2h 30min ago

If Docker isn't running, you'll see a message indicating that the service is inactive or failed.

Pulling the Jenkins Docker Image

In this step, we'll download the official Jenkins Docker image from Docker Hub. Docker Hub is a cloud-based repository where Docker images are stored and shared.

To pull the latest LTS (Long Term Support) version of Jenkins, run the following command:

docker pull jenkins/jenkins:lts

Let's break down this command:

  • docker pull tells Docker to download an image
  • jenkins/jenkins is the name of the image (created by the Jenkins project)
  • :lts is a tag that specifies we want the Long Term Support version, which is generally more stable

This command downloads the Jenkins image to your local machine. The download may take a few minutes depending on your internet speed. You'll see progress bars indicating the download status of various layers that make up the image.

After the download is complete, you can verify that the image is now available locally by running:

docker images

This command lists all Docker images on your system. You should see the Jenkins image listed in the output, similar to this:

REPOSITORY         TAG       IMAGE ID       CREATED        SIZE
jenkins/jenkins    lts       7a7add0bf3da   8 days ago     470MB

If you don't see the Jenkins image, it might mean the download failed. In this case, try running the docker pull command again. If issues persist, check your internet connection or try using a different network.

Creating a Docker Volume for Jenkins Data

In this step, we'll create a Docker volume to persistently store Jenkins data. This is a crucial step for maintaining your Jenkins configuration and build history.

Docker volumes provide a way to persist data generated by and used by Docker containers. This is particularly important for Jenkins, as we want to retain configuration, job data, and build artifacts even if the container is stopped or removed.

Create a new volume named jenkins-data by running:

docker volume create jenkins-data

This command tells Docker to create a new volume. Docker manages these volumes, storing them in a part of your host filesystem.

You can verify that the volume was created by listing all Docker volumes:

docker volume ls

You should see jenkins-data in the list of volumes, like this:

DRIVER    VOLUME NAME
local     jenkins-data

If you don't see the volume, try creating it again. If issues persist, ensure you have the necessary permissions to create Docker volumes (you might need to use sudo if your user isn't in the docker group).

Understanding Docker volumes is important because:

  1. They allow data to persist beyond the lifecycle of a container.
  2. They can be easily backed up and restored.
  3. They can be shared between containers if needed.

Running the Jenkins Docker Container

Now that we have the Jenkins image and a volume for data persistence, we can run the Jenkins container. This step will actually start Jenkins in a Docker container, making it accessible on your system.

We'll run the container with the following configurations:

  • Map container port 8080 to host port 8080 (for the web interface)
  • Map container port 50000 to host port 50000 (for agent connections)
  • Mount the jenkins-data volume to /var/jenkins_home in the container
  • Run the container in detached mode

Execute the following command to run the Jenkins container:

docker run -d -p 8080:8080 -p 50000:50000 -v jenkins-data:/var/jenkins_home --name jenkins-lts jenkins/jenkins:lts

Let's break down this command:

  • docker run creates and runs a new container
  • -d runs the container in detached mode (in the background)
  • -p 8080:8080 maps port 8080 in the container to port 8080 on your host
  • -p 50000:50000 maps port 50000 in the container to port 50000 on your host
  • -v jenkins-data:/var/jenkins_home mounts our Docker volume to the Jenkins home directory in the container
  • --name jenkins-lts gives our container a name for easy reference
  • jenkins/jenkins:lts specifies the image to use

After running this command, Docker will start the Jenkins container in the background. You can verify that the container is running by executing:

docker ps

This command lists all running Docker containers. You should see the Jenkins container listed in the output, similar to this:

CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                                                                                      NAMES
747b6dc7b1af   jenkins/jenkins:lts   "/usr/bin/tini -- /uâ€Ķ"   4 seconds ago   Up 3 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 0.0.0.0:50000->50000/tcp, :::50000->50000/tcp   jenkins-lts

If you don't see the Jenkins container, it might have failed to start. You can check for error messages using:

docker logs jenkins-lts

This will show the logs from the container, which can help diagnose any issues.

Accessing the Jenkins Web Interface

With the Jenkins container now running, we can access its web interface to complete the initial setup. This step will guide you through accessing Jenkins and performing the initial configuration.

There are two ways to access the Jenkins web interface:

  1. Click on the + symbol in the top tab of the virtual environment to create a new Web Service and enter port 8080.

    This method works within the lab environment, creating a new tab that displays the Jenkins interface.

  2. Open the Firefox browser in the VNC desktop and navigate to http://localhost:8080.

    alt text

You should see the Jenkins "Unlock Jenkins" page. This security measure ensures that only authorized users can set up Jenkins.

For this lab environment, the default username is admin, and the password is also admin.

Enter these credentials on the "Unlock Jenkins" page and click "Continue".

alt text

After unlocking Jenkins, you'll be guided through the initial setup process, including installing plugins and creating an admin user. For this lab, you can choose the "Install suggested plugins" option to get started quickly.

Summary

In this lab, we successfully installed and set up Jenkins using Docker. We covered essential steps including preparing the Docker environment, pulling the Jenkins image, creating a persistent volume for Jenkins data, running the Jenkins container, and accessing the Jenkins web interface for initial setup.

This hands-on experience has provided you with a solid foundation in using Docker to deploy Jenkins, a crucial skill in modern DevOps practices. You're now ready to explore Jenkins further, create build jobs, and integrate it into your development workflow.

Other Jenkins Tutorials you may like