Step-by-Step Guide to Setting Up Jenkins on Docker with Docker Agent-Based Builds
Tags: docker, jenkins, devops, tutorial
Published: 2024-01-15 13:07:47

## Part 1: Introduction to Docker and Jenkins, Setting Up Jenkins on Docker

---

## Introduction to Docker and Jenkins
Hello there! If you're stepping into the world of DevOps or are a Senior Software Engineer looking to dip your toes into automation, this section is a gentle introduction to two powerful tools: Docker and Jenkins.

**Docker - Simplifying Environments:** Docker is a tool that uses containerization to make it easier for you to develop, deploy, and run applications. Think of it as a virtual box where everything your application needs to run (code, runtime, system tools) is packaged together. This means no more 'it works on my machine' issues, as Docker ensures consistency across environments.

**Jenkins - Automating the Boring Stuff:** Jenkins is like a helpful robot for software teams. It automates parts of the software development process, particularly those repetitive tasks like building, testing, and deploying code. With Jenkins, you can focus more on writing great code and less on the process of getting that code into production.

**Why Are We Talking About Both?** Combining Docker and Jenkins can significantly streamline your development workflow. Docker provides a consistent environment for Jenkins to operate in, making your CI/CD pipelines more efficient and less prone to environment-related issues.

> Remember, everyone starts somewhere, and questions are a sign of a great learner. Whether you're a beginner in DevOps or an experienced engineer new to automation, this guide aims to make your journey into Docker and Jenkins as smooth as possible. Let's get started and unlock the potential of these tools together!

### Installing Docker and Jenkins
In this section, we'll cover the basic steps for installing Docker and setting up Jenkins using a Dockerfile and docker-compose.yml. This setup ensures a smooth and consistent environment for your Jenkins instance.

Installing Docker:
- Download Docker:

 * For Windows and Mac: Download Docker Desktop from the [official Docker website](https://www.docker.com/products/docker-desktop/).
 * For Linux: Follow the instructions specific to your Linux distribution on the [Docker installation guide](https://docs.docker.com/engine/install/).
- Install Docker:

Follow the installation guide for your operating system on the Docker website. This typically involves running an installer or executing a few commands in the terminal.
Verify Installation:

Open a terminal or command prompt and run `docker --version` to ensure Docker was installed successfully.

```
# docker --version
Docker version 24.0.7, build afdd53b
```

### Setting Up Jenkins in Docker:
For Jenkins, we will use a custom Dockerfile and docker-compose.yml to create a Docker image with Jenkins installed. Here's a step-by-step guide:

* Prepare the Dockerfile:

```
# Jenkins JDK 17, JDK 11 is being deprecated
FROM jenkins/jenkins:latest-jdk17

USER root

RUN apt-get update && apt-get install -y lsb-release

RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
  https://download.docker.com/linux/debian/gpg

RUN echo "deb [arch=$(dpkg --print-architecture) \
  signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
  https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list

RUN apt-get update && apt-get install -y docker-ce-cli

RUN apt update && apt install tzdata -y

ENV TZ="Asia/Dhaka"

USER jenkins
```

The provided `Dockerfile` sets up Jenkins with JDK 17 and installs Docker inside the Jenkins container. This setup allows Jenkins to run Docker commands, which is crucial for Docker Agent-based builds.

### Understanding the Dockerfile:

  * The Dockerfile begins with the latest Jenkins image with JDK 17.
  * It installs the necessary packages and Docker CLI inside the container.
  * The timezone is set to 'Asia/Dhaka', which you can adjust as per your requirement.

### Setting Up docker-compose:

```
version: '3.8'
services:
  jenkins:
    build: 
      context: .
      dockerfile: .docker/Jenkins/Dockerfile
    image: jenkins-jdk-17
    container_name: jenkins-jdk-17
    privileged: true
    user: root
    restart: always
    ports:
      - 8080:8080
      - 50000:50000
    volumes:
      - jenkins_home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker

volumes:
  jenkins_home:
    name: jenkins_home

```
  * The provided `docker-compose.yml` file defines the Jenkins service, using the image built from our Dockerfile.
  * It sets the container to run in privileged mode with root user, ensuring that Docker commands can be executed within Jenkins.
  * Ports 8080 and 50000 are exposed for Jenkins web UI and agent connections.
  * Volumes are used for persisting Jenkins data and for Docker socket binding, allowing Jenkins to control Docker on the host.

### Building and Running Jenkins Container:

  - Navigate to the directory containing your Dockerfile and docker-compose.yml.
  - Run docker-compose build to build your Jenkins image.
  - Once the build is complete, run docker-compose up to start Jenkins.
  - Jenkins should now be accessible at http://localhost:8080.

### Accessing Jenkins:

  * On your browser, go to `http://localhost:8080` to access the Jenkins web UI.

{% youtube aL-ZI7YILBw %}

  * Follow the initial setup wizard to configure Jenkins. You will need the initial admin password, which can be found in the Jenkins console logs or inside your Jenkins container at /var/jenkins_home/secrets/initialAdminPassword.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olg1tnl4yw55zgxpjial.png) Pic: collect initial admin password from console.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ectxhf25qkb9wto3yydz.png) Pic: Jenkins UI for initial admin password


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tmltp0bn5xn8emahp01.png) Pic: Plugins Selection page (we will move forward with defaults).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzqjhngo1prpygs5jv2q.png) Pic: Default plugins are being installed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5buj7u88a4dgx4qhgpu6.png) Pic: Setup Jenkins URL (This URL will be used to configure projects)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03jnu3x2iecpp40nptvh.png) Pic: admin user setup.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93k9wadjwpqu1imc4hfy.png) Pic: Jenkins Home page.

### Additional Resources:
- For a more detailed Docker installation guide, visit the [Docker documentation](https://docs.docker.com/engine/install/).
- To understand more about Jenkins and Docker, explore the [official Jenkins documentation](https://www.jenkins.io/doc/book/installing/docker/).

### GitHub Repository:
For the complete Dockerfile, docker-compose.yml, and additional configuration files, you can visit the GitHub repository: https://github.com/aburaihan-dev/jenkins-in-docker. This repository contains all the necessary files and instructions to get your Jenkins on Docker up and running.