Ubuntu

Install and Run Jenkins with Systemd and Docker

Install and Run Jenkins with Systemd and Docker

When you download and configure Jenkins using docker, you have to use manual command to manage the process. It becomes quite unsafe and complicated. So, we will manage it using systemd. Actually, it is the system and service manager for Linux operating systems. It features on-demand starting, enabling, restarting, and stopping of daemons, logging, etc.

In this article, I am going to download the Jenkins package from docker hub repository and manage it using systemd manager. I have used Ubuntu Linux, but you can do it in centos, Debian, Redhat etc which support systemd.

Prerequisites

  1. Latest version of docker installed
  2. Internet connection for downloading Jenkins image

Configuration

To run a process we need users. So, we create a system group, add a system user to the group. As you don’t need to login we provide no login shell. I have here given the name ‘devops’, you can give of your choice.

$ sudo groupadd --system devops
$ sudo useradd -s /sbin/nologin --system -g devops devops

During docker installation, a user docker was added by default. So to run our process inside docker, let’s add ‘devops’ user and your current login user to the docker group.

$ sudo usermod -aG docker devops
$ sudo usermod -aG docker $USER

We need a directory to map container volume to the host. So, let’s create a directory and give current user ownership in the directory.

$ sudo mkdir -p /data/jenkins
$ chown -R $USER:$USER /data/jenkins

You can now verify user ‘devops’ using the following command. Your output must be similar to the following.

$ id devops

Now it’s time to create a service. The service files are stored inside /etc/systemd/system/ So, create a file ending with .service as shown below.

$ vim /etc/systemd/system/docker-jenkins.service

Add following content in the file. The following configuration will create a service name docker-jenkins. It will simply pull the latest version of jenkins image from docker hub and run the container. It will also map port 8080 and 50000 in the host server, which are needed to access jenkins service. As defined in the Unit section below, it requires docker.service to be executed successfully. If you have different users, mount points change accordingly.

[Unit]

Description=My Jenkins Server

Documentation=https://jenkins.io/doc/

After=docker.service

Requires=docker.service

[Service]

Type=simple

User=devops

Group=devops

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

ExecStart=/usr/bin/docker run \

--name jenkins-server \

--publish 8080:8080 \

--publish 50000:50000 \

--volume /data/jenkins:/var/jenkins_home \

jenkins/jenkins

SyslogIdentifier=jenkin

ExecStop=/usr/bin/docker stop jenkins-server

[Install]

WantedBy=multi-user.target

Reload the service file in the systemd daemon, using the following command.

$ sudo systemctl daemon-reload

Start the docker service using the command below.

$ sudo systemctl start docker-jenkins

Systemd also provides default logging service in syslog. You can see the log in the following location.

$ sudo tail -f /var/log/syslog

Now, you can start the service after the server reboots using the ‘enabling’ feature of systemd manager.

$ sudo systemctl enable docker-jenkins

Also, check by restarting the service.

$ sudo systemctl restart docker-jenkins

Jenkins setup

To setup jenkins first visit http://yourserverip:8080 Then you need to enter the initial admin password, which is set in the following path. Copy the output and paste it in “Administrator password’. Then click on continue.

$ cat /data/jenkins/secrets/initialAdminPassword

You can install the required plugin and continue.

Conclusion

In this article, you learnt how to install the latest version of Jenkins using docker and configure the service to use it. Managing Jenkins with systemd makes the work simpler. Thank you for reading.

Similar Posts