How to install Jenkins with Docker

Overview

This article will cover the steps how to Install Jenkins with Docker. We’ll be deploying the Jenkins LTS release on a single host machine and we’ll be using the Jenkins official Docker image from the Docker hub. The full Jenkins Docker documentation can be checked out on the official jenkins github.

Jenkins is a well-known and popular open source CI/CD automation tool. With many benefits it offers, it can sometimes be tedious to run maintenance on it, since it depends on Java to run. Luckily, we now have a Docker version of Jenkins which we can easily deploy and simplifies the maintenance since it has Java included as well.

Prerequisites:

Docker pre-installed and running. Docker can now be quickly installed with the official auto-install script from the Docker site on some major distributions(ubuntu, debian, rasbian etc…):

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Install Jenkins with Docker

To install Jenkins with Docker, the Jenkins documentation recommend the following Docker command arguments:

docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk11

This docker command will deploy Jenkins container with the LTS version of Jenkins, mount a /var/jenkins_home directory from the host machine(thus adding data persistence to Jenkins, meaning no data loss on container restart or update - this setting is highly recommended to add) and exposing ports so that Jenkins dashboard is available via web browser. The docker will take couple of minutes to install, deploy and run Jenkins.

With mounting the directory from the host server, that enables to backup all the Jenkins configuration, files and plugins(which is recommended) and also can migrate to another host as well.

Accessing and configuring Jenkins

After deploying the Jenkins docker container, Docker should start the Jenkins container right away automatically and on terminal, output should display the administrator unlock password right away upon starting the container. Picture bellow is an example:

Install Jenkins with Docker

If your Jenkins container doesn’t start, check if it runs with Docker and start. You can also acquire the administrator unlock password later.

docker ps -a

docker start ${CONTAINER_ID or CONTAINER_NAME}

To acquire the unlock password from the Jenkins docker container, run the command from bellow. With this command, we can print out the password without needing to exec into the container(or shell into the container):

sudo docker exec ${CONTAINER_ID or CONTAINER_NAME} cat /var/jenkins_home/secrets/initialAdminPassword

The first time accessing the dashboard, you’ll get the Unlock the Jenkins page which will show you were and how to acquire the administrator password and unlock the Jenkins admin dashboard.

Install Jenkins with Docker

Paste in the password on the unlock page and click on continue.

On the next screen will be the new admin user creation. It’s suggestible to create and use the new admin account.

Install Jenkins with Docker

The following screen - Instance configuration, on here you just need to either confirm or update the Jenkins dashboard URL.

Click save and finish and this will be the last step of configuring.

Install Jenkins with Docker

Firewall configuration

Just in case, If you have a firewall running(which is highly recommended), it’s necessary to have open ports for Jenkins, otherwise you’re not going to be able to access the Jenkins admin dashboard.

UFW

sudo ufw allow 2376
IPTABLES

sudo iptables -I INPUT -p tcp -m tcp --dport 2376 -j ACCEPT

or

sudo iptables -A INPUT -p tcp -m tcp --dport 2376 -j ACCEPT

Summary

To summarize, we showed the steps how to install Jenkins with Docker. Deploying the Jenkins with Docker is a good solution because it reduces the maintenance on the host level and also reduces the need to run maintenance on Java, since the Jenkins in Docker comes together with Java pre-installed and configured. On top of that, there’s the ease of deployment as well(rather than installing Java and Jenkins manually on a Linux host and updating it manually as well). Essentially we need one command to deploy Jenkins and we can work on it right away.

Even though, the solution is less demanding in maintenance but it can still be resource heavy(depending on the team size, number of pipelines and what kind of builds must be run). To run at least and use to learn and practice, it requires 2 GB of RAM from the host machine and for a small  dev team, to run properly and run pipelines and builds - it’s suggested to have at least a host with 4 GB of RAM and 2 core CPU.

Thank you for your time…