Repetitive tasks are usually tedious and end up taking up a lot of your time and energy. Over time, multiple automation tools have been developed to help alleviate the hassle of executing repetitive jobs. One such automation tool is Jenkins. Jenkins is an opensource automation server that is designed to help software developers build, test and deploy applications and thereby streamline the continuous integration and delivery process. We have penned an article before on how to install Jenkins on CentOS 8/ RHEL 8. In this article, we will do things a little different and run the Jenkins server in a Docker container as a systemd service.
A few things are required before you continue.
- Docker installed on your Linux system.
- A regular user with sudo privileges.
Step 1) Install Docker Engine
To begin, you need to have Docker engine installed on your system. We have a detailed article on how to install Docker on CentOS 8 / RHEL 8. Run below docker command to display the docker version
$ sudo docker version
From the snippet above, we have confirmed that docker is installed and that we are running docker version 19.03.13.
Step 2) Create a Jenkins user
Next, we will create a ‘Jenkins’ system user that will manage Jenkins service. But first, create a system group for Jenkins:
$ sudo groupadd --system jenkins
Then Create Jenkins system user
$ sudo useradd -s /sbin/nologin --system -g jenkins jenkins
And finally add Jenkins user to docker group as shown:
$ sudo usermod -aG docker jenkins
To confirm that Jenkins user is added to the docker group, run the id command as shown
$ id jenkins
Output will be,
[[email protected] ~]$ id jenkins uid=991(jenkins) gid=986(jenkins) groups=986(jenkins),989(docker) [[email protected] ~]$
Fantastic! Let’s proceed and pull a Jenkins image.
Step 3) Pull Jenkins Image from Docker hub
Invoke the following command to pull the latest Jenkins image from Docker hub.
$ sudo docker pull jenkins/jenkins:lts
This usually takes a few seconds on a fairly stable internet connection. Once downloaded, verify that the Jenkins image is present by invoking the following command:
$ sudo docker images | grep jenkins
Output of above command would be:
[[email protected] ~]$ sudo docker images | grep jenkins jenkins/jenkins lts f669140ba6ec 6 days ago 711MB [[email protected] ~]$
Jenkins requires a persistent storage to store data and re-use even in the event of a container crash. Therefore, we will create a storage directory as shown.
$ sudo mkdir /var/jenkins $ sudo chown -R 1000:1000 /var/jenkins
Step 4) Create a systemd service for Jenkins
Using your preferred text editor, create a Jenkins systemd file as shown:
$ sudo vi /etc/systemd/system/jenkins-docker.service
Paste the following contents & save the file.
[Unit] Description=Jenkins Server Documentation=https://jenkins.io/doc/ After=docker.service Requires=docker.service [Service] Type=simple User=jenkins Group=jenkins TimeoutStartSec=0 Restart=on-failure RestartSec=30s ExecStartPre=-/usr/bin/docker kill jenkins-server ExecStartPre=-/usr/bin/docker rm jenkins-server ExecStartPre=/usr/bin/docker pull jenkins/jenkins:lts ExecStart=/usr/bin/docker run --name jenkins-server --publish 8080:8080 --publish 50000:50000 --volume /var/jenkins:/var/jenkins_home jenkins/jenkins:lts SyslogIdentifier=jenkins ExecStop=/usr/bin/docker stop jenkins-server [Install] WantedBy=multi-user.target
To start Jenkins service, reload systemd first and thereafter start Jenkins.
$ sudo systemctl daemon-reload $ sudo systemctl start jenkins-docker
Let’s now check if Jenkins is running. To do so, we will execute:
$ sudo systemctl status jenkins-docker
Great ! Jenkins is up and running as a systemd service. Since Jenkins will be running on port 8080, open the port on the firewall as shown:
$ sudo firewall-cmd --permanent --add-port=8080/tcp $ sudo firewall-cmd --reload
To set up Jenkins, simply browse the server’s URL as shown
You will get the ‘Unlock Jenkins’ page as shown. To proceed, you need to provide the password that is located in the file shown
[[email protected] ~]$ cat /var/jenkins/secrets/initialAdminPassword 9c61bd823a404056bf0a408f4622aafc [[email protected] ~]$
Once done, Click on ‘Continue’
Next, select ‘Install suggested plugins’ option as shown.
Thereafter, create an administrative user for the Jenkins account and click ‘Save and continue’
The installer will guide you through the remaining steps right to the very end. After successfull Installation, we will get the following Jenkins dashboard
And it’s a wrap. In this guide, you learned how to run Jenkins inside a docker container as a systemd service.
Also Read: How to Install and Configure Jenkins on Ubuntu 20.04